我如何判断一个拖放中的WinForms已经结束?已经结束、拖放、如何判断、WinForms

2023-09-03 00:56:14 作者:听海的哭泣

如何判断是一个拖放已经结束的WinForms .NET。我需要从刷新它的视图的数据时,拖拽过程中停止我的一部分。

How do I tell is a Drag Drop has ended WinForms .net. I need to stop part of my form from refreshing it's view of data when a drag drop is in progress.

我用一个标志都试过,但我似乎没有被捕获,我需要保持国旗的同步与拖放进度的事件​​。具体来说,我不能当拖放操作,而不完成拖拽,即终止当用户将一个控制该项目与允许压降= false,或者当用户presses ESC键告诉

I have tried using a flag but I don't seem to be capturing all the events I need to to keep the flag in sync with the progress of drag drop. Specifically I can't tell when the drag drop operation ends without a drag drop being completed, i.e. when the user drops the item on a control with allow drop = false, or when the user presses the ESC key.

我已经看到了这个问题: -

I have seen this question:-

检查拖动和放大器;下降过程中

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

But it doesn't address my issue satisfactorily (if someone gives me the answer to this question I'll answer that one with the answer together with what I already have).

推荐答案

我没有考生最终想通了这一点。

I got no takers and eventually figured this out.

答案是监控QueryContinueDrag事件。此事件拖放操作过程中不断触发。该QueryContinueDragEventArgs包含枚举类型DragAction,它可以是DragAction.Cancel,DragAction.Drop或DragAction.Continue的操作属性。这是一个读/为了让你改变标准行为写属性(我们不需要这个)。

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).

这个例子code假定DragDropInProgress标志被设置在一个拖放启动和复位时,拖放成功完成。它捕获一个的DragDrop结束,因为用户已经放开鼠标而不通过拖放目标(拖放目标是MyControl1和MyControl2)或取消拖拽。如果你不介意的DragDropInProgressFlag重置您的DragDrop事件触发之前,你可以点击测试免除,只是复位标志。

This example code assumes DragDropInProgress flag is set at the start of a drag drop and reset when a drag drop is completed successfully. It catches a DragDrop ending because the user has let go of the mouse without being over a drag drop target (the drag drop targets are MyControl1 and MyControl2) or cancels the drag drop. If you don't care if the DragDropInProgressFlag is reset before your DragDrop events fires you can dispense with the hit test and just reset the flag.

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

在这个例子中的HitTest是rountine,需要一个鼠标位置(屏幕坐标)和控件数组,并进行筛选,通过阵列传递真,如果鼠标位置在任何的控制矩形。

In this example HitTest is a rountine that takes a Mouse position (screen co-ordinate) and an array of controls and sifts through the array passing True if the mouse position is in any of the controls rectangles.