Viktor Szekeress
Viktor Szekeress

Reputation: 171

WPF: DoDragDrop getting interrupted when clicking Right/Middle Mouse button

I have extented Canvas {System.Windows.Controls} and items that are draggable into the Canvas. During dragging, I have OnDragOver event where I do panning when user clicks and holds Middle mouse button.

On the Item site : DoDragDrop - common drag functionality
On the Canvas site : OnDragOver - panning the canvas

So user can drag and pan at the same time.

Everything worked fine until I moved to new laptop (Lenovo) and Visual Studio 2012 (2010 before). Now when I press middle (or right) mouse button, event OnMouseMove of the Canvas is immediately fired. After that dragging is immediately stopped and no panning as well.

My co-worker tried to run the same code from Visual Studio 2010 and it worked OK. He made setup of his version so I tried it and the result was the same - on my laptop I cannot pan during dragging..

Does anybody have some idea whats the problem? HW, SW, Lenovo, Windows?

project info: WPF, DevExpress 12.1, .NET 4, Windows 7 Proffesional, VS 2012

Pls keep in mind that Im still new in WPF :)

Upvotes: 3

Views: 1237

Answers (1)

Viktor Szekeress
Viktor Szekeress

Reputation: 171

Just to answer my own question, maybe it will be useful for someone.

I didn't find out where is the problem, but my conclusion is that on some PCs DragDrop may get interrupted when user press middle/right mouse button while dragging.

To override this behavior, you need to add QueryContinueDragHandler to DragDrop. Then in your own handler use your logic to respond on mouse/keyboard inputs.

So my code looks like this:

DragDrop.AddQueryContinueDragHandler(this, QueryContinueDragHandler);
DragDrop.DoDragDrop(this, dataObject, DragDropEffects.Copy);
DragDrop.RemoveQueryContinueDragHandler(this, QueryContinueDragHandler);

and custom handler:

/// <summary>
/// Own handler. This event is raised when something happens during DragDrop operation (user presses Mouse button or Keyboard button...)
/// Necessary to avoid canceling DragDrop on MouseMiddleButon on certain PCs.
/// Overrides default handler, that interrupts DragDrop on MouseMiddleButon or MouseRightButton down.
/// </summary>
/// <param name="source"></param>
/// <param name="e"></param>
private void QueryContinueDragHandler(Object source, QueryContinueDragEventArgs e)
{
    e.Handled = true;

        // if ESC
        if (e.EscapePressed)
        {
            //  -->  cancel DragDrop
            e.Action = DragAction.Cancel;

            return;
        }

        // if LB
        if (e.KeyStates.HasFlag(DragDropKeyStates.LeftMouseButton))
        {
            //  -->  continue dragging
            e.Action = DragAction.Continue;
        }
        // if !LB (user released LeftMouseButton)
        else
        {
            // and if mouse is inside canvas
            if (_isMouseOverCanvas)
            {
                //  -->  execute Drop
                e.Action = DragAction.Drop;
            }
            else
            {
                //  -->  cancel Drop 
                e.Action = DragAction.Cancel;                    
            }

            return;
        }

        // if MB
        if (e.KeyStates.HasFlag(DragDropKeyStates.MiddleMouseButton))
        {
            //  -->  continue dragging
            e.Action = DragAction.Continue;
        }    
}

Upvotes: 3

Related Questions