MouseMove事件的行为不符合预期

时间:2014-09-09 12:22:08

标签: wpf vb.net mousemove

我正在编写我的第一个代码来处理WPF / VB.Net中的拖放事件。

作为学习练习,我在按钮控件上按下左键时尝试启动DoDragDrop。我认为这将实现如下:

Private Sub ButtonYield_MouseMove(sender As Object, e As MouseEventArgs) Handles ButtonYield.MouseMove

    If e.LeftButton = MouseButtonState.Pressed Then
        'Drag Drop Code Here
        MsgBox("Event Called")
    End If

End Sub

但实际上,只有在鼠标移动到按钮控件上时按之前 ,才会调用此事件。

我在这里缺少一些基本的东西吗?

提前致谢!

1 个答案:

答案 0 :(得分:1)

这样做符合预期,当您单击左键时,您的代码正在检查鼠标移动。您需要跟踪鼠标何时单击按钮,然后在鼠标移动时进行拖放操作。沿着这些方向的东西(未经测试):

Private _mouseDownOverButton As Boolean = False

Private Sub ButtonYield_MouseLeave(sender As Object, e As MouseEventArgs) Handles ButtonYield.MouseLeave
    _mouseDownOverButton = False
End Sub

Private Sub ButtonYield_MouseLeftButtonDown(sender As Object, e As MouseButtonEventArgs) Handles ButtonYield.MouseLeftButtonDown
    _mouseDownOverButton = True
End Sub

Private Sub ButtonYield_MouseLeftButtonUp(sender As Object, e As MouseButtonEventArgs) Handles ButtonYield.MouseLeftButtonUp
    _mouseDownOverButton = False
End Sub

Private Sub ButtonYield_MouseMove(sender As Object, e As MouseEventArgs) Handles ButtonYield.MouseMove
    If _mouseDownOverButton Then
        'drag drop code here
    End If
End Sub