添加Shape1控件数组3个0-2,(先在工具箱添加一个Shape,然后复制,再粘帖2次,这样就有一组3个的控件数组),再加2个timer控件timer1和timer2,属性默认就可以,在load过程设置,代码如下,已测试
Option Explicit
Dim X, Y As Integer
Private Sub Form_Load()
Dim I As Integer
For I = 0 To 2
Shape1(I)BackStyle = 1
Shape1(I)Shape = 2
Shape1(I)BackColor = &HFFFFFF
Next I
Timer1Enabled = True
Timer1Interval = 5000
Timer2Enabled = False
Timer2Interval = 300
Shape1(0)BackColor = &HFF&
X = 0
Y = 2
End Sub
Private Sub Timer1_Timer()
Timer2Enabled = True
Timer1Enabled = False
End Sub
Private Sub Timer2_Timer()
If Y Mod 2 <> 0 Then
If X = 0 Then
Shape1(X)BackColor = &HFF&
End If
If X = 1 Then
Shape1(X)BackColor = &HFF00&
End If
If X = 2 Then
Shape1(X)BackColor = &HFFFF&
End If
Else
Shape1(X)BackColor = &HFFFFFF
End If
Y = Y + 1
If Y = 15 Then
Shape1(X)BackColor = &HFFFFFF
Y = 2
X = X + 1
If X > 2 Then X = 0
Timer1Enabled = True
Timer2Enabled = False
If X = 0 Then
Shape1(X)BackColor = &HFF&
End If
If X = 1 Then
Shape1(X)BackColor = &HFF00&
End If
If X = 2 Then
Shape1(X)BackColor = &HFFFF&
End If
End If
End Sub
如图在窗体上添加:
1、四个shape控件,作为灯框、红灯、黄灯、绿灯,分别命名为:LampBox、RedLamp、YellowLamp、GreenLamp
2、一个命令按钮,命名为:cmdRun,标题为“启动”
3、三个文本框控件,来设置红灯、黄灯、绿灯亮的时间(秒),分别命名为:txtRedTime、txtYellowTime、txtGreenTime
4、四个时钟控件,作为红灯、黄灯、绿灯和闪烁时间控制,分别命名为:tmrRed、tmrYellow、tmrGreen、tmrChange
5、其它控件和布局见图
程序如下:
Private Red As Long
Private Yellow As Long
Private Green As Long
Private Black As Long
Private LampNo As Integer
Private Sub cmdRun_Click()
If cmdRunCaption = "启动" Then
txtRedTimeEnabled = False
txtYellowTimeEnabled = False
txtGreenTimeEnabled = False
cmdRunCaption = "停止"
tmrRedInterval = Val(txtRedTimeText) 1000
tmrYellowInterval = Val(txtYellowTimeText) 1000
tmrGreenInterval = Val(txtGreenTimeText) 1000
LampNo = 0
tmrChangeEnabled = True
Else
tmrRedEnabled = False
tmrYellowEnabled = False
tmrGreenEnabled = False
tmrChangeEnabled = False
RedLampFillColor = Black
YellowLampFillColor = Black
GreenLampFillColor = Black
txtRedTimeEnabled = True
txtYellowTimeEnabled = True
txtGreenTimeEnabled = True
cmdRunCaption = "启动"
End If
End Sub
Private Sub Form_Load()
tmrRedEnabled = False
tmrYellowEnabled = False
tmrGreenEnabled = False
tmrChangeEnabled = False
Red = &HFF&
Yellow = &HFFFF&
Green = &HFF00&
Black = &H0&
LampBoxFillColor = Black
RedLampFillColor = Black
YellowLampFillColor = Black
GreenLampFillColor = Black
tmrChangeInterval = 300
txtRedTimeText = 6
txtYellowTimeText = 1
txtGreenTimeText = 8
End Sub
Private Sub tmrChange_Timer()
Static n As Integer
n = n + 1
Select Case LampNo
Case 0
If RedLampFillColor = Black Then
RedLampFillColor = Red
YellowLampFillColor = Yellow
GreenLampFillColor = Green
Else
RedLampFillColor = Black
YellowLampFillColor = Black
GreenLampFillColor = Black
End If
Case 1
If RedLampFillColor = Black Then
RedLampFillColor = Red
Else
RedLampFillColor = Black
End If
Case 2
YellowLampFillColor = Yellow
Case 3
If GreenLampFillColor = Black Then
GreenLampFillColor = Green
Else
GreenLampFillColor = Black
End If
End Select
If n = 4 Then
n = 0
LampNo = LampNo + 1
If LampNo = 5 Then LampNo = 1
Select Case LampNo
Case 1
RedLampFillColor = Red
YellowLampFillColor = Black
GreenLampFillColor = Black
tmrRedEnabled = True
Case 2, 4
RedLampFillColor = Black
YellowLampFillColor = Yellow
GreenLampFillColor = Black
tmrYellowEnabled = True
Case 3
RedLampFillColor = Black
YellowLampFillColor = Black
GreenLampFillColor = Green
tmrGreenEnabled = True
End Select
tmrChangeEnabled = False
End If
End Sub
Private Sub tmrGreen_Timer()
tmrChangeEnabled = True
tmrGreenEnabled = False
End Sub
Private Sub tmrRed_Timer()
tmrChangeEnabled = True
tmrRedEnabled = False
End Sub
Private Sub tmrYellow_Timer()
tmrChangeEnabled = True
tmrYellowEnabled = False
End Sub
不管有没有分,我都会试着帮一把:
这个程序的原理是利用Timer每1秒更换shape的FillColor:
我正在上电脑课,怕被老师发现,就只写了最简单的:
画一个shape,timer,然后填入下列代码就Ok了:
Dim a As Integer
Private Sub Form_Load()
a = "0"
With Shape1
Shape = 3
FillColor = vbRed
End With
Timer1Interval = 1000
Timer1Enabled = True
End Sub
Private Sub Timer1_Timer()
If Shape1FillColor = vbRed Then
Shape1FillColor = vbGreen
ElseIf Shape1FillColor = vbGreen Then
Shape1FillColor = vbYellow
ElseIf Shape1FillColor = vbYellow Then
Shape1FillColor = vbRed
End If
Exit Sub
End Sub
可使用FillColor属性改变Shape控件填充形状的颜色。
当灯亮时显示灯亮的色彩。
当灯灭时显示窗体的BackColor背景颜色。
BackColor属性,BackColor-返回或设置对象的背景颜色。
用一个Timer控件的Timer事件来判别条件的变化。
以下是实施代码。
Option ExplicitDim zt_dm1
Private Sub Command1_Click()
zt_dm1 = 1
End Sub
Private Sub Command2_Click()
zt_dm1 = 0
End Sub
Private Sub Form_Load()
zt_dm1 = 0
Timer1Interval = 200
Timer1Enabled = True
End Sub
Private Sub Timer1_Timer()
If zt_dm1 = 1 Then
Shape1FillStyle = 0
Shape1FillColor = vbGreen
Shape2FillStyle = 0
Shape2FillColor = MeBackColor
Else
Shape1FillStyle = 0
Shape1FillColor = MeBackColor
Shape2FillStyle = 0
Shape2FillColor = vbRed
End If
效果:
Dim r As Integer, g As Integer, y As Integer
Private Sub Form_Load()
Timer1Enabled = True
Timer2Enabled = False
Timer3Enabled = False
Label1Caption = 0
Label2Caption = 0
Label3Caption = 0
Image2Picture = LoadResPicture(1000, vbResBitmap)
Image1Picture = LoadResPicture(1000, vbResBitmap)
Image3Picture = LoadResPicture(1000, vbResBitmap)
g = 60 + 1
End Sub
Private Sub Timer1_Timer()
Image1Picture = LoadResPicture(1001, vbResBitmap)
Image2Picture = LoadResPicture(1000, vbResBitmap)
Image3Picture = LoadResPicture(1000, vbResBitmap)
g = g - 1
Label1Caption = g
If g = 0 Then
y = 3 + 1
Timer1Enabled = False
Timer2Enabled = True
End If
End Sub
Private Sub Timer2_Timer()
Image2Picture = LoadResPicture(1003, vbResBitmap)
Image1Picture = LoadResPicture(1000, vbResBitmap)
Image3Picture = LoadResPicture(1000, vbResBitmap)
y = y - 1
Label2Caption = y
If y = 0 Then
r = 60 + 1
Timer2Enabled = False
Timer3Enabled = True
End If
End Sub
Private Sub Timer3_Timer()
Image3Picture = LoadResPicture(1002, vbResBitmap)
Image2Picture = LoadResPicture(1000, vbResBitmap)
Image1Picture = LoadResPicture(1000, vbResBitmap)
r = r - 1
Label3Caption = r
If r = 0 Then
g = 60 + 1
Timer3Enabled = False
Timer1Enabled = True
End If
End Sub
以上就是关于跪求VB红黄绿灯程序代码,越简单越好!全部的内容,包括:跪求VB红黄绿灯程序代码,越简单越好!、VB编程设计题:红绿灯、求用VB做一个模拟交通灯程序,要简单点的,别太复杂,做好帮我运行一遍,前提必须运行正确。谢谢~~~~等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)