首先,在窗体上一个Timer控件Timer1与一个Image控件Image1.
Private Sub Form_Click() '用窗体的鼠标单击决定物体是否运动
If Timer1.Enabled = False Then
Timer1.Enabled = True
Else
Timer1.Enabled = False
End If
End Sub
End Sub
End Sub
最后调试 运行
然后,设置Picture为Pic.jpg
接着编写代码
'TopBottom(上下),LeftRight(左右)用来区分物体运动的方向
Public TopBottom, LeftRight As String
Public TopBottom, LeftRight As String
Private Sub Form_Click() '用窗体的鼠标单击决定物体是否运动
If Timer1.Enabled = False Then
Timer1.Enabled = True
Else
Timer1.Enabled = False
End If
End Sub
Private Sub Form_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
'键值KeyAscii值为61对应减号(-)键,减速
Case 61
Timer1.Interval = 10
'键值KeyAscii值为45对应减号(+)键,加速
Case 45
Timer1.Interval = 100
End Select
End Sub
Select Case KeyAscii
'键值KeyAscii值为61对应减号(-)键,减速
Case 61
Timer1.Interval = 10
'键值KeyAscii值为45对应减号(+)键,加速
Case 45
Timer1.Interval = 100
End Select
End Sub
Private Sub Form_Load()
'定义物体初始运动方向为左上
TopBottom = "0"
LeftRight = "0"
'定义物体初始运动方向为左上
TopBottom = "0"
LeftRight = "0"
End Sub
Private Sub Timer1_Timer() '由计时器控制物体的运动
Select Case TopBottom & LeftRight
Case "00" '物体运动方向为左上
Form1.Image1.Top = Form1.Image1.Top - 10
Form1.Image1.Left = Form1.Image1.Left - 10
'续行
Form1.Image1.Move _
Form1.Image1.Left - 10, Form1.Image1.Top - 10
'若物体到达上边界,则转向下运动
If Form1.Image1.Left <= 0 Then
TopBottom = "1"
End If
'若物体到达左边界,则转向右运动
If Form1.Image1.Left <= 0 Then
LeftRight = "1"
End If
Case "10" '运动方向为左下
Form1.Image1.Top = Form1.Image1.Top + 10
Form1.Image1.Left = Form1.Image1.Left - 10
'续行
If Form1.Image1.Top >= _
Form1.ScaleHeight - Form1.Image1.Height Then
TopBottom = "0"
End If
If Form1.Image1.Left <= 0 Then
LeftRight = "1"
End If
Case "01" '运动方向为右上
Form1.Image1.Top = Form1.Image1.Top - 10
Form1.Image1.Left = Form1.Image1.Left + 10
If Form1.Image1.Top <= 0 Then
TopBottom = "1"
End If
'续行
If Form1.Image1.Left >= _
Form1.ScaleWidth - Form1.Image1.Width Then
LeftRight = "0"
End If
Case "11" '运动方向为右下
Form1.Image1.Top = Form1.Image.Top + 10
Form1.Image1.Left = Form1.Image1.Left + 10
'续行
If Form1.Image1.Top >= _
Form1.ScaleHeight - Form1.Image1.Height Then
TopBottom = "0"
End If
'续行
If Form1.Image1.Left >= _
Form1.ScaleWidth - Form1.Image1.Width Then
LeftRight = "0"
End If
End Select
Select Case TopBottom & LeftRight
Case "00" '物体运动方向为左上
Form1.Image1.Top = Form1.Image1.Top - 10
Form1.Image1.Left = Form1.Image1.Left - 10
'续行
Form1.Image1.Move _
Form1.Image1.Left - 10, Form1.Image1.Top - 10
'若物体到达上边界,则转向下运动
If Form1.Image1.Left <= 0 Then
TopBottom = "1"
End If
'若物体到达左边界,则转向右运动
If Form1.Image1.Left <= 0 Then
LeftRight = "1"
End If
Case "10" '运动方向为左下
Form1.Image1.Top = Form1.Image1.Top + 10
Form1.Image1.Left = Form1.Image1.Left - 10
'续行
If Form1.Image1.Top >= _
Form1.ScaleHeight - Form1.Image1.Height Then
TopBottom = "0"
End If
If Form1.Image1.Left <= 0 Then
LeftRight = "1"
End If
Case "01" '运动方向为右上
Form1.Image1.Top = Form1.Image1.Top - 10
Form1.Image1.Left = Form1.Image1.Left + 10
If Form1.Image1.Top <= 0 Then
TopBottom = "1"
End If
'续行
If Form1.Image1.Left >= _
Form1.ScaleWidth - Form1.Image1.Width Then
LeftRight = "0"
End If
Case "11" '运动方向为右下
Form1.Image1.Top = Form1.Image.Top + 10
Form1.Image1.Left = Form1.Image1.Left + 10
'续行
If Form1.Image1.Top >= _
Form1.ScaleHeight - Form1.Image1.Height Then
TopBottom = "0"
End If
'续行
If Form1.Image1.Left >= _
Form1.ScaleWidth - Form1.Image1.Width Then
LeftRight = "0"
End If
End Select
End Sub
最后调试 运行
在工具栏中单击“启动” ,,进入运行状态。单击窗体,并按下“+” “-” 观察物体的运动情况