今天所交的加分作業……
全篇的重點在Form1_KeyDown部分!
設置物件有:
四個Timer,一個HScrollBar1_Scroll(橫向捲軸),一張圖片(picMouse)
Public Class Form1
Dim x, y As Integer
Private Sub tmrRight_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles tmrRight.Tick
x = picMouse.Left
y = picMouse.Top
If x <= (Me.Width - picMouse.Width) Then
picMouse.Location = New Point(x + 2, y)
Label1.Text = "(" & x + 2 & "," & y & ")"
Else
tmrRight.Enabled = False
tmrLeft.Enabled = True
End If
End Sub
Private Sub tmrLeft_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles tmrLeft.Tick
x = picMouse.Left
y = picMouse.Top
If (x > 0) Then
picMouse.Location = New Point(x - 2, y)
Label1.Text = "(" & x - 2 & "," & y & ")"
Else
tmrLeft.Enabled = False
tmrRight.Enabled = True
End If
End Sub
Private Sub HScrollBar1_Scroll(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ScrollEventArgs) Handles HScrollBar1.Scroll
tmrRight.Interval = HScrollBar1.Value
tmrLeft.Interval = HScrollBar1.Value
End Sub
Private Sub tmrDown_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles tmrDown.Tick
x = picMouse.Left
y = picMouse.Top
If y <= (Me.Height - picMouse.Height - 40) Then
picMouse.Location = New Point(x, y + 2)
Label1.Text = "(" & x & "," & y + 2 & ")"
Else
tmrUp.Enabled = True
tmrDown.Enabled = False
End If
End Sub
Private Sub tmrUp_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles tmrUp.Tick
x = picMouse.Left
y = picMouse.Top
If y > 0 Then
picMouse.Location = New Point(x, y - 2)
Label1.Text = "(" & x & "," & y - 2 & ")"
Else
tmrUp.Enabled = False
tmrDown.Enabled = True
End If
End Sub
Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
Select Case e.KeyCode
Case Keys.Left
tmrLeft.Enabled = True : tmrRight.Enabled = False : tmrUp.Enabled = False : tmrDown.Enabled = False
tmrLeft_Tick(sender, e)
Case Keys.Right
tmrLeft.Enabled = False : tmrRight.Enabled = True : tmrUp.Enabled = False : tmrDown.Enabled = False
tmrRight_Tick(sender, e)
Case Keys.Up
tmrLeft.Enabled = False : tmrRight.Enabled = False : tmrUp.Enabled = True : tmrDown.Enabled = False
tmrUp_Tick(sender, e)
Case Keys.Down
tmrLeft.Enabled = False : tmrRight.Enabled = False : tmrUp.Enabled = False : tmrDown.Enabled = True
tmrDown_Tick(sender, e)
End Select
End Sub
End Class