首先打开VB程序,在对象视图的窗口中画一个commandbutton,将其caption属性改为“开始运动”,然后在对象视图窗口插入一个timer,将其interval属性改为1000,enabled属性改为false,之后再用“shape”画一个圆于对象视图窗口,这样视图窗口的操作就完成了,之后是编辑代码,实现能动的图形。
将对象窗口改为代码窗口,输入代码,
If x < 0 Or x > (Me.ScaleWidth - Shape1.Width) Then
g = Not g
End If
Dim g As Boolean
Dim x As Long
Dim x As Long
Private Sub Command1_Click()
Timer1.Enabled = True
End Sub
Timer1.Enabled = True
End Sub
Private Sub Form_Load()
g = True
x = Shape1.Left
End Sub
g = True
x = Shape1.Left
End Sub
Private Sub Timer1_Timer()
If g Then
x = x - 300
Else
x = x + 300
End If
If g Then
x = x - 300
Else
x = x + 300
End If
If x < 0 Or x > (Me.ScaleWidth - Shape1.Width) Then
g = Not g
End If
Shape1.Move x
End Sub
这些都做完后,单击运行按钮,即可实现会动的图形。