在VB中如何实现程序的暂停

在VB中如何实现程序的暂停,第1张

使用Timer控件不好,因为间隔设置太长不能及时停止,太短影响程序运行。最好的方法是设置一个变量来指示暂停标志。如下程序,在窗体中放置两个Command按钮,一个Label1标签,单击Command1,标签值从1~1000000开始变化,随时按Command2计划停止变化,再按继续。

Dim Pause As Boolean, I As Long

Private Sub Command1_Click()

For I = 1 To 1000000

DoEvents

Do While Pause = True

DoEvents

Loop

Label1 = I

Next

End Sub

Private Sub Command2_Click()

Pause = Not Pause

End Sub

Private Sub Form_Load()

Pause = False

End Sub

你试下,简单的例子

Form1Line (1, 1)-(2000, 2000)--------------画线

Form1Line (1, 1)-(2000, 2000), , B---------画空心矩形

Form1Line (1, 1)-(2000, 2000), , BF-------画实心矩形

line 方法

在对象上画直线和矩形。

语法

objectLine [Step] (x1, 1) [Step] (x2, y2), [color], [B][F]

Line 方法的语法有以下对象限定符和部分:

部分 描述

object 可选的。 对象表达式,其值为“应用于”列表中的对象。如果object 省略,具有焦点的窗体作为object。

Step 可选的。关键字,指定起点坐标,它们相对于由 CurrentX 和 CurrentY 属性提供的当前图形位置。

(x1, y1) 可选的。Single (单精度浮点数),直线或矩形的起点坐标。ScaleMode 属性决定了使用的度量单位。如果省略,线起始于由 CurrentX 和 CurrentY 指示的位置。

Step 可选的。关键字,指定相对于线的起点的终点坐标。

(x2, y2) 必需的。Single (单精度浮点数),直线或矩形的终点坐标。

color 可选的。Long (长整型数),画线时用的 RGB 颜色。如果它被省略,则使用 ForeColor 属性值。可用 RGB 函数或 QBColor 函数指定颜色。

B 可选的。如果包括,则利用对角坐标画出矩形。

F 可选的。如果使用了 B 选项,则 F 选项规定矩形以矩形边框的颜色填充。不能不用 B 而用 F。如果不用 F 光用 B,则矩形用当前的 FillColor 和 FillStyle 填充。FillStyle 的缺省值为 transparent。

说明

画联结的线时,前一条线的终点就是后一条线的起点。

线的宽度取决于 DrawWidth 属性值。在背景上画线和矩形的方法取决于 DrawMode 和 DrawStyle 属性值。

执行 Line 方法时, CurrentX 和 CurrentY 属性被参数设置为终点。

这个方法不能用于WithEnd With 语句块。

程序并没有实现,显示中绿灯和黄灯会闪的功能,本人初学者,以后多交流。

设计如下图,两个计时器(enabled为false),三个标签,两个文本框,一个按钮

运行效果:

代码如下:

Dim n As Integer '控制哪个灯亮

Dim bool1 As Boolean    '两个变量一起控制是否开始运行程序

Dim bool2 As Boolean

Dim b As Integer

Private Sub Command1_Click()    '只有两个文本框同时满足输入的时间在1-10秒范围内,才开始运行

    If IsNumeric(Text1Text) Then

            bool1 = True

    Else

        MsgBox "输入的不是数字,重新输入红灯的时间"

        Text1Text = ""

    End If

    If IsNumeric(Text2Text) Then

            bool2 = True

    Else

        MsgBox "输入的不是数字,重新输入绿灯时间"

        Text2Text = ""

    End If

    If bool1 And bool2 Then

        Timer1Enabled = True

        Timer1Interval = 1

    End If

End Sub

Private Sub Form_Load() '重新定义窗体坐标,画出三个灯都没有亮的状态下的红绿灯

    bool1 = False

    bool2 = False

    AutoRedraw = True

    Form1Cls

    Form1Scale (0, 0)-(100, 100)

    Form1FillStyle = 0

    n = 1

    Form1FillColor = RGB(0, 0, 0)

    Form1Line (2, 2)-(18, 48), , B

    Form1FillColor = RGB(160, 0, 0)

    Form1Circle (10, 10), 5, RGB(160, 0, 0)

    Form1FillColor = RGB(0, 160, 0)

    Form1Circle (10, 40), 5, RGB(0, 160, 0)

    Form1FillColor = RGB(160, 160, 0)

    Form1Circle (10, 25), 5, RGB(160, 160, 0)

    

End Sub

Private Sub Timer1_Timer()

    n = n Mod 3

    If n = 1 Then   '红色高亮显示,其他变暗

        Form1Cls

        Form1FillColor = RGB(0, 0, 0)

        Form1Line (2, 2)-(18, 48), , B

        Form1FillColor = RGB(255, 0, 0)

        Form1Circle (10, 10), 5, RGB(255, 0, 0)

        Form1FillColor = RGB(0, 160, 0)

        Form1Circle (10, 40), 5, RGB(0, 160, 0)

        Form1FillColor = RGB(160, 160, 0)

        Form1Circle (10, 25), 5, RGB(160, 160, 0)

        Timer1Interval = Val(Text1Text)  1000

        b = Val(Text1Text)

        Timer2Enabled = True

    ElseIf n = 0 Then   '**高亮显示,其他变暗

        Form1Cls

        Form1FillColor = RGB(0, 0, 0)

        Form1Line (2, 2)-(18, 48), , B

        Form1FillColor = RGB(255, 255, 0)

        Form1Circle (10, 25), 5, RGB(255, 255, 0)

        Form1FillColor = RGB(0, 160, 0)

        Form1Circle (10, 40), 5, RGB(0, 160, 0)

        Form1FillColor = RGB(160, 0, 0)

        Form1Circle (10, 10), 5, RGB(160, 0, 0)

        Timer1Interval = 3000

        b = 3

        Timer2Enabled = True

    Else                '绿色高亮显示,其他变暗

        Form1Cls

        Form1FillColor = RGB(0, 0, 0)

        Form1Line (2, 2)-(18, 48), , B

        Form1FillColor = RGB(0, 255, 0)

        Form1Circle (10, 40), 5, RGB(0, 255, 0)

        Form1FillColor = RGB(160, 0, 0)

        Form1Circle (10, 10), 5, RGB(160, 0, 0)

        Form1FillColor = RGB(160, 160, 0)

        Form1Circle (10, 25), 5, RGB(160, 160, 0)

        Timer1Interval = Val(Text2Text)  1000

        b = Val(Text2Text)

        Timer2Enabled = True

    End If

    n = n + 1

End Sub

Private Sub Timer2_Timer()

    b = b - 1

    If b <= 10 Then

        Label3Caption = b

    End If

    If b = 0 Then

        Timer2Enabled = False

        Label3Caption = ""

    End If

    

End Sub

以上就是关于在VB中如何实现程序的暂停全部的内容,包括:在VB中如何实现程序的暂停、在VB程序设计中正方形的常量和变量用line打代码怎么打、VB程序编写等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zz/10074410.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-05
下一篇2023-05-05

发表评论

登录后才能评论

评论列表(0条)

    保存