我正在嘗試檢測控件的左右鼠標移動 - 就像您可以使用delta來上/下移動一樣。有人能幫忙嗎?謝謝。如何檢測鼠標是否移動到左側或右側?
If e.x > 0 Then 'moved right
msgbox("Moved right!")
else 'moved left
msgbox("Moved left!")
End If
我正在嘗試檢測控件的左右鼠標移動 - 就像您可以使用delta來上/下移動一樣。有人能幫忙嗎?謝謝。如何檢測鼠標是否移動到左側或右側?
If e.x > 0 Then 'moved right
msgbox("Moved right!")
else 'moved left
msgbox("Moved left!")
End If
Private oldXY As Point = Point.Empty
Private Sub Form1_MouseMove(sender As Object,
e As MouseEventArgs) Handles Me.MouseMove
If e.X < oldXY.X Then
' ....
ElseIf e.X > oldXY.X Then
' ...
End If
oldXY.X = e.X
oldXY.Y = e.Y
End Sub
你可能會想,這樣你不誤報第一鼠標移動到增加一個測試Point.Empty。或嘗試將其初始化爲Cursor.Position
下手
這工作非常感謝。 – dave88
Private firstTime As Boolean = False
Private oldX As Integer
Private Sub Button1_MouseEnter(sender As System.Object, e As System.EventArgs) Handles Button1.MouseEnter
firstTime = True
End Sub
Private Sub Button1_MouseMove(sender As System.Object, e As System.Windows.Forms.MouseEventArgs) Handles Button1.MouseMove
If firstTime = True Then
firstTime = False
Else
If e.X > oldX Then
'moves right
ElseIf e.X < oldX Then
'moves left
End If
End If
oldX = e.X
End Sub
謝謝你的建議,但我無法得到這個工作。 – dave88
@ dave88你必須有一個按鈕才能工作。 –
我使用定時器,我也得到了良好的效果
Dim lx As Integer = 0 ' last x position
Dim ly As Integer = 0 ' last y position
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Dim x As Integer = MousePosition.X
Dim y As Integer = MousePosition.Y
Dim s As String = ""
If x > lx Then
s &= "Right,"
ElseIf x < lx Then
s &= "Left,"
ElseIf x = lx Then
s &= "No Change,"
End If
If y > ly Then
s &= "Down"
ElseIf y < ly Then
s &= "Top"
ElseIf y = ly Then
s &= "No Change"
End If
lx = x
ly = y
Label1.Text = s
End Sub
感謝這一點,但我想盡可能避免使用計時器。 – dave88
其中的代碼你試過嗎?你在消費什麼事件? – Plutonix
e.x總是正數(除非按住鼠標) –
e - MouseEvent參數顯示**當前**位置。將e.x和e.y保存到模塊級變量中,以便將e.X與oldLocation.X進行比較。 – Plutonix