如何判断Drag Drop是否已在Winforms中结束?

Chr*_*rds 8 .net winforms

我怎么知道Drag Drop已经结束了WinForms .net.当拖拽正在进行时,我需要停止部分表单刷新数据视图.

我试过使用一个标志,但我似乎没有抓住我需要的所有事件来保持标志与拖放进度同步.具体来说,我无法判断拖拽操作何时结束而没有拖拽完成,即当用户将项目放在具有allow drop = false的控件上时,或者当用户按下ESC键时.

我见过这个问题: -

检查拖放是否正在进行中

但它没有令人满意地解决我的问题(如果有人给我这个问题的答案,我会回答那个答案和我已经有的答案).

Chr*_*rds 18

我没有接受者,最终想出来了.

The answer is to monitor the QueryContinueDrag event. This event fires continually during drag drop operation. The QueryContinueDragEventArgs contain an Action property of type enum DragAction, which is either DragAction.Cancel, DragAction.Drop or DragAction.Continue. It is a read/write property in order to allow you to change the standard behaviour (we don't need this).

此示例代码假定DragDropInProgress标志在拖放开始时设置,并在拖放成功完成时重置.它捕获DragDrop结束,因为用户放弃了鼠标而没有超过拖放目标(拖放目标是MyControl1和MyControl2)或取消拖放.如果您不关心DragDropInProgressFlag是否在DragDrop事件触发之前重置,您可以省略命中测试并重置标志.

Private Sub MyControl_QueryContinueDrag(ByVal sender As Object, ByVal e As System.Windows.Forms.QueryContinueDragEventArgs) Handles MyControl.QueryContinueDrag

    Dim MousePointerLocation As Point = MousePosition

    If e.Action = DragAction.Cancel Then '' User pressed the Escape button
        DragDropInProgressFlag = False
    End If

    If e.Action = DragAction.Drop Then
        If Not HitTest(new {MyControl1, MyControl2}, MousePointerLocation) Then
            DragDropInProgressFlag = False
        End If
    End If

End Sub

Private Function HitTest(ByVal ctls() As Control, ByVal p As Point) As Boolean

    HitTest = False

    For Each ctl In ctls
        Dim ClientPoint As Point = ctl.PointToClient(p)
        HitTest = HitTest Or (ClientPoint.X >= 0 AndAlso ClientPoint.Y >= 0 AndAlso ClientPoint.X <= ctl.Width AndAlso ClientPoint.Y <= ctl.Height)
        If HitTest Then Exit For
    Next

End Function
Run Code Online (Sandbox Code Playgroud)

在这个例子中,如果鼠标位置在任何控件矩形中,HitTest是一个rountine,它接受一个鼠标位置(屏幕坐标)和一组控件并通过数组筛选传递True.