因为楼上说没法消除停顿感,我特此作了一个试验,窗体上放置一个picturebox,置form的keypreview为true 大家试试看,到底有没有停顿感(这种方法可以不使用GetAsyncKeyState,我采取了一种变通的办法)Dim movingleft As Boolean Dim movingright As Boolean Private Sub Picture1_KeyDown(KeyCode As Integer, Shift As Integer) Select Case KeyCode Case 37: movingleft = True movingright = False Case 39: movingleft = False movingright = True End Select End SubPrivate Sub Picture1_KeyUp(KeyCode As Integer, Shift As Integer) movingleft = False movingright = False End SubPrivate Sub Timer1_Timer() If movingleft = True Then Picture1.Left = Picture1.Left - 15 End If If movingright = True Then Picture1.Left = Picture1.Left + 15 End If End Sub
下面函数改进一下Private Sub Picture1_KeyUp(KeyCode As Integer, Shift As Integer) Select Case KeyCode Case 37: movingleft = False Case 39: movingright = False End Select End Sub
将Form1的AutoRedraw设为TRUE。
在while循环里面加入它就可以了 ,例如检测向下按钮:
GetAsyncKeyState(VK_DOWN)
[email protected]
大家试试看,到底有没有停顿感(这种方法可以不使用GetAsyncKeyState,我采取了一种变通的办法)Dim movingleft As Boolean
Dim movingright As Boolean
Private Sub Picture1_KeyDown(KeyCode As Integer, Shift As Integer)
Select Case KeyCode
Case 37:
movingleft = True
movingright = False
Case 39:
movingleft = False
movingright = True
End Select
End SubPrivate Sub Picture1_KeyUp(KeyCode As Integer, Shift As Integer)
movingleft = False
movingright = False
End SubPrivate Sub Timer1_Timer()
If movingleft = True Then
Picture1.Left = Picture1.Left - 15
End If
If movingright = True Then
Picture1.Left = Picture1.Left + 15
End If
End Sub
Select Case KeyCode
Case 37:
movingleft = False
Case 39:
movingright = False
End Select
End Sub