程序并没有实现,显示中绿灯和黄灯会闪的功能,本人初学者,以后多交流。
设计如下图,两个计时器(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(Text1.Text) Then
bool1 = True
Else
MsgBox "输入的不是数字,重新输入红灯的时间"
Text1.Text = ""
End If
If IsNumeric(Text2.Text) Then
bool2 = True
Else
MsgBox "输入的不是数字,重新输入绿灯时间"
Text2.Text = ""
End If
If bool1 And bool2 Then
Timer1.Enabled = True
Timer1.Interval = 1
End If
End Sub
Private Sub Form_Load() '重新定义窗体坐标,画出三个灯都没有亮的状态下的红绿灯
bool1 = False
bool2 = False
AutoRedraw = True
Form1.Cls
Form1.Scale (0, 0)-(100, 100)
Form1.FillStyle = 0
n = 1
Form1.FillColor = RGB(0, 0, 0)
Form1.Line (2, 2)-(18, 48), , B
Form1.FillColor = RGB(160, 0, 0)
Form1.Circle (10, 10), 5, RGB(160, 0, 0)
Form1.FillColor = RGB(0, 160, 0)
Form1.Circle (10, 40), 5, RGB(0, 160, 0)
Form1.FillColor = RGB(160, 160, 0)
Form1.Circle (10, 25), 5, RGB(160, 160, 0)
End Sub
Private Sub Timer1_Timer()
n = n Mod 3
If n = 1 Then '红色高亮显示,其他变暗
Form1.Cls
Form1.FillColor = RGB(0, 0, 0)
Form1.Line (2, 2)-(18, 48), , B
Form1.FillColor = RGB(255, 0, 0)
Form1.Circle (10, 10), 5, RGB(255, 0, 0)
Form1.FillColor = RGB(0, 160, 0)
Form1.Circle (10, 40), 5, RGB(0, 160, 0)
Form1.FillColor = RGB(160, 160, 0)
Form1.Circle (10, 25), 5, RGB(160, 160, 0)
Timer1.Interval = Val(Text1.Text) * 1000
b = Val(Text1.Text)
Timer2.Enabled = True
ElseIf n = 0 Then '黄色高亮显示,其他变暗
Form1.Cls
Form1.FillColor = RGB(0, 0, 0)
Form1.Line (2, 2)-(18, 48), , B
Form1.FillColor = RGB(255, 255, 0)
Form1.Circle (10, 25), 5, RGB(255, 255, 0)
Form1.FillColor = RGB(0, 160, 0)
Form1.Circle (10, 40), 5, RGB(0, 160, 0)
Form1.FillColor = RGB(160, 0, 0)
Form1.Circle (10, 10), 5, RGB(160, 0, 0)
Timer1.Interval = 3000
b = 3
Timer2.Enabled = True
Else '绿色高亮显示,其他变暗
Form1.Cls
Form1.FillColor = RGB(0, 0, 0)
Form1.Line (2, 2)-(18, 48), , B
Form1.FillColor = RGB(0, 255, 0)
Form1.Circle (10, 40), 5, RGB(0, 255, 0)
Form1.FillColor = RGB(160, 0, 0)
Form1.Circle (10, 10), 5, RGB(160, 0, 0)
Form1.FillColor = RGB(160, 160, 0)
Form1.Circle (10, 25), 5, RGB(160, 160, 0)
Timer1.Interval = Val(Text2.Text) * 1000
b = Val(Text2.Text)
Timer2.Enabled = True
End If
n = n + 1
End Sub
Private Sub Timer2_Timer()
b = b - 1
If b <= 10 Then
Label3.Caption = b
End If
If b = 0 Then
Timer2.Enabled = False
Label3.Caption = ""
End If
End Sub