vb写一个简单红绿灯程序代码,希望能注释和思路写清楚一点,我菜鸟!谢谢

2025-05-05 15:51:23
推荐回答(3个)
回答1:

'ZT是当前状态记录,Qzt是前状态记录,SJ是时间记录
Dim ZT As Integer, Qzt As Integer, Sj As Integer
Private Sub Command1_Click()
If Command1.Caption = "开始" Then
Command1.Caption = "20停止"
Timer1.Enabled = True
Shape3.FillColor = &HFF&
ZT = 3
Qzt = 3
Else
Command1.Caption = "开始"
Timer1.Enabled = False
Shape1.FillColor = &H4000&
Shape2.FillColor = &H8080&
Shape3.FillColor = &H40&
ZT = 0
End If

End Sub

Private Sub Form_Load()
Shape1.FillStyle = 0 '绿灯
Shape2.FillStyle = 0 '黄灯
Shape3.FillStyle = 0 '红灯

Shape1.FillColor = &H4000& '暗绿
Shape2.FillColor = &H8080& '暗黄
Shape3.FillColor = &H40& '暗红

Shape1.Shape = 3 '圆形
Shape2.Shape = 3
Shape3.Shape = 3

Command1.Caption = "开始"
Timer1.Interval = 1000 '一秒一次加入倒计时
Timer1.Enabled = False
End Sub

Private Sub Timer1_Timer()
Sj = Sj + 1
If ZT = 1 Or ZT = 3 Then Command1.Caption = (20 - Sj) & "停止"
If ZT = 2 Then Command1.Caption = (5 - Sj) & "停止"
If Sj = 20 And (ZT = 1 Or ZT = 3) Then
Shape1.FillColor = &H4000&
Shape2.FillColor = &H8080&
Shape3.FillColor = &H40&

Select Case ZT

Case 1

Shape2.FillColor = &HFFFF&
ZT = 2
Qzt = 1

Case 3

Shape2.FillColor = &HFFFF&
ZT = 2
Qzt = 3
End Select
Sj = 0
End If
If Sj = 5 And ZT = 2 Then
Shape1.FillColor = &H4000&
Shape2.FillColor = &H8080&
Shape3.FillColor = &H40&

If Qzt = 3 Then
Shape1.FillColor = &HFF00&
ZT = 1
Else
Shape3.FillColor = &HFF&
ZT = 3
End If
Sj = 0
End If

End Sub

回答2:

窗体上随意放一个计时器(Timer)和三个Shape控件,还有一个按钮:

Private Sub Command1_Click()
Timer1.Enabled = True
Timer1_Timer
End Sub

Private Sub Form_Load()
Shape1.Shape = 3
Shape1.FillColor = vbGrayText
Shape1.FillStyle = 0
Shape2.Shape = 3
Shape2.FillColor = vbGrayText
Shape2.FillStyle = 0
Shape3.Shape = 3
Shape3.FillColor = vbGrayText
Shape3.FillStyle = 0
Shape1.Move Me.Width / 2 - 500, Me.Height / 6 - 500, 1000, 1000
Shape2.Move Me.Width / 2 - 500, Me.Height / 3 - 500, 1000, 1000
Shape3.Move Me.Width / 2 - 500, Me.Height / 2 - 500, 1000, 1000
Timer1.Enabled = False
Timer1.Interval = 1000
End Sub

Private Sub Timer1_Timer()
Static n As Integer
n = n + 1
If n > 45 Then n = 1
Select Case n
Case 1 To 20
Me.Caption = n
Shape1.FillColor = vbRed
Shape2.FillColor = vbGrayText
Shape3.FillColor = vbGrayText
Case 21 To 25
Me.Caption = n - 20
Shape1.FillColor = vbGrayText
Shape2.FillColor = vbYellow
Shape3.FillColor = vbGrayText
Case 26 To 45
Me.Caption = n - 25
Shape1.FillColor = vbGrayText
Shape2.FillColor = vbGrayText
Shape3.FillColor = vbGreen
End Select
End Sub

回答3:

Private Sub Command1_Click()
Timer1.Enabled = False
End Sub

Private Sub Form_Load()
Shape1.BorderColor = &HFF00&
Shape1.FillColor = &HFF00&
Command1.Caption = "停止"
Timer1.Enabled = True
Timer1.Interval = 1
End Sub

Private Sub Timer1_Timer()
If Shape1.BorderColor = 255 Then
Timer1.Interval = 5000
Shape1.BorderColor = 65535
Shape1.FillColor = 65535
Exit Sub
End If
If Shape1.BorderColor = 65280 Then
Timer1.Interval = 20000
Shape1.BorderColor = 255
Shape1.FillColor = 255
Exit Sub
End If
If Shape1.BorderColor = 65535 Then
Timer1.Interval = 20000
Shape1.BorderColor = 65280
Shape1.FillColor = 65280
Exit Sub
End If
End Sub