DoEvents()什莫意思?

stanely 2000-08-08 07:41:00
各位大侠不是只否能告诉我DoEvents()干什莫用?
最好是有程序代码的例子,
小弟将感激不尽!以我仅有的10分奉上!
...全文
158 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
stanely 2000-08-09
  • 打赏
  • 举报
回复
thanks!
xiaohuilau 2000-08-08
  • 打赏
  • 举报
回复
DoEvents 是令程序在忙于执行指令时抽出少许时间让给别的程序。最实用就是若你编了一个死循环的程序,若在循环体中加了 DoEvents 就有时间跳出死循环。如:
dim i,j
for i=1 to 10000
j=j+1
doEvents
next i
这样你试下在程序执行时去运行别的程序。再去掉DoEnents去拟行别的程序就一目了然了。
forise 2000-08-08
  • 打赏
  • 举报
回复
This example uses the DoEvents function to cause execution to yield to the operating system once every 1000 iterations of the loop. DoEvents returns the number of open Visual Basic forms, but only when the host application is Visual Basic.

' Create a variable to hold number of Visual Basic forms loaded
' and visible.
Dim I, OpenForms
For I = 1 To 150000 ' Start loop.
If I Mod 1000 = 0 Then ' If loop has repeated 1000 times.
OpenForms = DoEvents ' Yield to operating system.
End If
Next I ' Increment loop counter.

前 言 visual basic继承了basic语言易学易用的特点,特别适合于初学者学习windows系统编程。随着21世纪信息社会的到来,计算机在人们的工作和生活中的深入,要求我们越来越多地与计算机打交道,为了使用户在繁忙的日程工作中得到放松,于是出现了各种各样的休闲软件,如聊天工具,游戏等等。于是我们小组着手设计开始一个这样的游戏软件。通过这学期来Visual Basic的学习,我初步掌握了Visual Basic语言的最基本的知识,于是在化希耀张兵等老师的指导下动手用Visual Basic编写俄罗斯方块游戏。 无可争议,《俄罗斯方块》是有史以来最伟大的游戏之一。它是永恒的娱乐经典,但它实际上又和那些传统的经典娱乐方式不同,因为它的本质是电子化的,所以它的确属于现代产物。《俄罗斯方块》举世闻名的游戏性,在该游戏新鲜出炉时就显得非常直观。某些与坠落的玩具碎片和它们的形状有关的东西,使得哪怕新手也会很自然地企图把它们排列起来,并加以适当组合,就好似《俄罗斯方块》触动了我们某些内在的感官,使得哪怕是我们当中最杂乱无章的人也要把事情整理妥当。 在曾经发布过的所有游戏中,《俄罗斯方块》还被认为是仅有的一个能够真正吸引广泛人群的作品。某些批评家也许会声称,《俄罗斯方块》要比过去二十年间出现的任何东西都要浪费人们的时间。至于我们,则要欣然提名它为GameSpot评选出的历史上最伟大游戏之一。 为了怀念经典,也为了能够给大多的计算机用户在工作之余找到一个休闲、娱乐的一个方式,我们小组开始着手用VB语言开发一个经典的俄罗斯方块游戏。 工程概况 1.1 项目名称 俄罗斯方块游戏 1.2 设计平台 VB 全称Visual Basic,它是以Basic语言作为其基本语言的一种可视化编程工具。 Vb是microsoft公司于1991年退出的windows应用程序开发工具visual意思是“可视化的”。在它刚推出来时,自身还存在一些缺陷,功能也相对少一些。但是经过多年的开发研究。最近microsoft公司又推出了VB6.0版本 VB6.0运行环境:硬件,要求486以上的处理器、16MB以上内存,50MB 以上的硬盘,cd-rom驱动器,鼠标。软件:要求windows 95以上版本。 1.3程序设计思想 游戏是用来给大家娱乐的,所以要能在使用的过程中给大家带来快乐,消除大家的疲劳,所以我们在游戏中添加了漂亮的场景和动听的音乐,设置了过关升级的功能,激发大家的娱乐激情。 从游戏的基本玩法出发,主要就是俄罗斯方块的形状和旋转,我们在设计中在一个图片框中构造了一个4*4的网状小块,由这些小块组合成新的形状,每四个小块连接在一起就可以构造出一种造型,因此我们总共设计了7中造型,每种造型又可以通过旋转而变化出2到4种形状,利用随机函数在一个预览窗体中提前展示形状供用户参考,然后将展示的形状复制到游戏窗体中进行摆放,在游戏窗体中用户就可以使用键盘的方向键来控制方块的运动,然后利用递归语句对每一行进行判断,如果有某行的方块是满的,则消除这行的方块,并且使上面的方块自由下落,其中,方块向下的速度是有时钟控件控制的,在游戏中,用户也可以使用向下键加快下落速度,定义一个变量,对消除的函数进行记录,最后就可以得出用户的分数,用if 语句对分数判断,达到一定的积分就可以升级到下一个档次。 俄罗斯方块游戏设计的主要步骤为以下10个方面: (1)游戏界面的设计。 (2)俄罗斯方块的造型。 (3)俄罗斯方块的旋转。 (4)俄罗斯方块的运动情况(包括向左,向右和向下)。 (5)俄罗斯方块的自动消行功能。 (6)游戏级别的自由选择。 (7)游戏速度的自由选择。 (8)游戏得分的计算。 (9)游戏菜单选项的设计及功能实现。 (10)游戏的背景音乐。 1.4运用的控件和主要对象 我们在设计过程中主要用到的控件有:command控件,image控件,picture控件,label控件,timer控件,text控件,windows media player控件等等。 1.5主要实现的功能 我们开发的俄罗斯方块游戏,主要实现了以下几种功能: 1.可以灵活控制方块在图形框中运动。 2.游戏过程中方块可以自由旋转。 3.当某一行的方块排列满时,将自动将这一行方块消除,然后将上面所有方块向下移动,可以支持连续消行。 4.游戏前可以选择游戏的速度和游戏的等级,游戏速度既为方块下落速度,游戏等级为初始游戏时在基层随机生成一定行数的无规律方块,生成的行数由你来选择,每行至少产生5个以上的无规律方块,这样增加了游戏难度,对于游戏高手来说,无疑不是一个新的挑战。 5.游戏的得分支持积分,并且按照公式: 得分 = 原来分数+ 100 * (2 ^ 同时消除的行数-1) 这样,你同一时间消除
Dim filem As String '保存文件名 Dim str As String Dim msg Dim bu As Boolean '用于判断文本框中的内容是否改变 Public Nexts As Double '用于存储查找末字符的位置 Public Sv, mo As Double 'sv用于存储查的下一个字符的位置,mo用于存储查找第一次查找字符的位置 Dim Cmt(3) As Single, Cml(3) As Single, Cmw(3) As Single, Cmh(3) As Single Dim Tt(1) As Single, Tl(1) As Single, Tw(1) As Single, Th(1) As Single Dim cht As Single, chl As Single, chw As Single, chh As Single Private Sub Check1_Click() If Check1.Value = 1 Then Label1.Caption = "要连接的末字符" Else Label1.Caption = "要查找的首字符" End If End Sub Private Sub Command1_Click() Dim Ts As String Dim a As Long a = 1048576 cmo1.Filter = "*.txt" cmo1.ShowOpen filem = cmo1.FileName If cmo1.FileName = "" Then Exit Sub End If Ts = Right(cmo1.FileName, 4) If Ts <> ".txt" Then MsgBox "您打开的非文本文档文件", , "提示" Exit Sub End If If FileLen(filem) \ a > 10 Then MsgBox "您打开的文件已超过10M", , "提示" Exit Sub End If RichTextBox1.FileName = cmo1.FileName bu = False Label3.Caption = "你打开的文件名为:" & filem If FileLen(filem) \ 1024 <= 0 Then '判断文件是否有1KB Label4.Caption = FileLen(filem) & "字节" ElseIf FileLen(filem) \ 1024 >= 1 And FileLen(filem) / 1024 <= 1024 Then '文件有1KB和文件小于1M时。 Label4.Caption = "文件大小为" & Left(FileLen(filem) / 1024, 5) & "KB" ElseIf FileLen(filem) \ a > 0 Then '文件有1M时 Label4.Caption = "文件大小为:" & Left(FileLen(filem) / a, 4) & "M" End If Exit Sub End Sub Private Sub Command2_Click() If Label1.Caption = "要查找的首字符" Then If Text1.Text = "" Then MsgBox "请输入要查找的内容" Exit Sub End If Timer3.Enabled = True Command1.Enabled = False Command2.Enabled = False '防止在程序忙时,用户点击其它按扭,导致程序崩溃。 Command3.Enabled = False Label2.Visible = True mo = InStr(RichTextBox1.Text, Text1.Text) Command1.Enabled = True Command2.Enabled = True Command3.Enabled = True Timer3.Enabled = False Label2.Visible = False Sv = mo RichTextBox1.SetFocus DoEvents RichTextBox1.SelStart = mo - 1 RichTextBox1.SelLength = Len(Text1.Text) Label1.Caption = "要查找的末字符" Text1.Text = "" '查找首字符 ElseIf Label1.Caption = "要查找的末字符" Then If Text1.Text = "" Then MsgBox "请输入要查找的内容" Exit Sub End If Timer3.Enabled = True Command1.Enabled = False Command2.Enabled = False Command3.Enabled = False Label2.Visible = True Nexts = InStr(Sv + Len(Text1.Text), RichTextBox1.Text, Text1.Text) 'sv后面加上文本长度是为了从字符后面位置开始查找,否则只会从已查找到字符位置前查找。这样会重复 Command1.Enabled = True Command2.Enabled = True Command3.Enabled = True Timer3.Enabled = False Label2.Visible = False If Nexts = 0 Then MsgBox "未查找到内容", , "提示" Exit Sub End If Sv = Nexts RichTextBox1.SetFocus RichTextBox1.SelStart = Nexts - 1 RichTextBox1.SelLength = Len(Text1.Text) Command2.Caption = "查找下一个" Command2.Enabled = False Timer1.Enabled = True '查找末字符,并把选中的代码交给时间控件 ElseIf Label1.Caption = "要连接的末字符" Then Timer3.Enabled = True Command1.Enabled = False Command2.Enabled = False Command3.Enabled = False Label2.Visible = True mo = InStr(Sv + Len(Text1.Text), RichTextBox1.Text, Text1.Text) Command1.Enabled = True Command2.Enabled = True Command3.Enabled = True Timer3.Enabled = False Label2.Visible = False If mo = 0 Then MsgBox "找不到" Exit Sub End If Sv = mo DoEvents RichTextBox1.SetFocus RichTextBox1.SelStart = mo - 1 RichTextBox1.SelLength = Len(Text1.Text) Command2.Caption = " 查找下一个" Command2.Enabled = False Timer2.Enabled = True '选中文本代码交给时间控件2完成 End If End Sub Private Sub Command3_Click() Call save MsgBox "保存成功" End Sub Private Sub Command4_Click() msg = MsgBox("是否删除选中的字符", vbYesNo + 64, "询问") If msg = vbYes Then str = Replace(RichTextBox1.Text, RichTextBox1.SelText, "") RichTextBox1.SelText = str End If End Sub Sub save() Open filem For Output As #1 Print #1, RichTextBox1.Text Close #1 End Sub Private Sub Form_Load() menpaste.Enabled = False Tt(0) = Text1.Top Tl(0) = Text1.Left Tw(0) = Text1.Width Th(0) = Text1.Height Tt(1) = RichTextBox1.Top Tl(1) = RichTextBox1.Left Tw(1) = RichTextBox1.Width Th(1) = RichTextBox1.Height '保存Combo1控件的Top、Left、Width和Height属性 Cmt(0) = Command1.Top Cml(0) = Command1.Left Cmw(0) = Command1.Width Cmh(0) = Command1.Height End Sub Private Sub Form_Unload(Cancel As Integer) If filem <> "" And bu = True Then msg = MsgBox("是否保存文件", vbYesNo + 64, "询问") If msg = vbYes Then Command1.Enabled = True Command2.Enabled = True Command3.Enabled = True Timer3.Enabled = False Label2.Visible = False Call save Command1.Enabled = True Command2.Enabled = True Command3.Enabled = True Timer3.Enabled = False Label2.Visible = False MsgBox "保存成功" End If End If End Sub Private Sub mencopy_Click() Clipboard.SetText RichTextBox1.SelText menpaste.Enabled = True End Sub Private Sub menexit_Click() End Sub Private Sub menpaste_Click() RichTextBox1.SelText = Clipboard.GetText End Sub Private Sub RichTextBox1_Change() bu = True If bu = True And filem <> "" Then Command3.Enabled = True End If End Sub Private Sub RichTextBox1_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single) If Button = vbRightButton Then PopupMenu menfile End If End Sub Private Sub Timer1_Timer() Static a As Integer a = a + 1 If a = 3 Then Command2.Enabled = True Timer1.Enabled = False '到时间后停止时间控件,以防多次运行 a = 0 msg = MsgBox("是否要将首末字符全部选中", vbYesNo + 64, "询问") '选中所查找的下一个字符 If msg = vbYes Then Timer3.Enabled = True Command1.Enabled = False Command2.Enabled = False Command3.Enabled = False Label2.Visible = True RichTextBox1.SetFocus RichTextBox1.SelStart = mo - 1 RichTextBox1.SelLength = Nexts Command1.Enabled = True Command2.Enabled = True Command3.Enabled = True Timer3.Enabled = False Label2.Visible = False Label1.Caption = "要查找的首字符" Command4.Enabled = True Command2.Caption = "开始查找" End If End If End Sub Private Sub Timer2_Timer() Static b As Integer b = b + 1 If b = 3 Then Timer2.Enabled = False Command2.Enabled = True b = 0 msg = MsgBox("是否从第一个文字到些内容位置全选中", vbYesNo + 64, "询问") If msg = vbYes Then Timer3.Enabled = True Command1.Enabled = False Command2.Enabled = False Command3.Enabled = False Label2.Visible = True RichTextBox1.SetFocus RichTextBox1.SelStart = 1 RichTextBox1.SelLength = mo Command1.Enabled = True Command2.Enabled = True Command3.Enabled = True Timer3.Enabled = False Label2.Visible = False Command4.Enabled = True End If End If End Sub Private Sub Timer3_Timer() Static s As Integer s = s + 1 prog1.Value = s If s Mod 2 = 0 Then DoEvents End If End Sub

7,763

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧