Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
555 views
in Technique[技术] by (71.8m points)

.net - How do I tell if a Drag Drop has ended in Winforms?

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.

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:-

Check if a drag&drop is in progress

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I got no takers and eventually figured this out.

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

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

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...