如何检测鼠标是向左还是向右移动?

时间:2014-02-12 16:56:02

标签: vb.net

我正在尝试检测控件的左右鼠标移动 - 就像您可以使用delta进行上/下移动。有人能帮忙吗?感谢。

If e.x > 0 Then 'moved right
 msgbox("Moved right!")
else 'moved left
 msgbox("Moved left!")
End If

3 个答案:

答案 0 :(得分:1)

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添加一个测试,这样你就不会误报第一个mousemove。或者尝试将其初始化为Cursor.Position

开头

答案 1 :(得分:0)

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

答案 2 :(得分:0)

我正在使用Timer,我得到了一个好结果

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