none
DragMove() with right mouse button? RRS feed

  • Question

  • Hey,

    We're having an issue and we don't know how to solve it. We have a form that requires use of the left mouse button. The form does not have a title bar and does not have any area that can be used for dragging. Now, we want to make the form draggable so we figured we could use the right mouse button.

    This would have worked, if DragMove() allowed it. Apparently DragMove() works only for the left mouse button. How can we move the WPF form when the user presses and holds the right mouse button instead of the left?

    Any help would be greatly appreciated, thanks!! :)
    Wednesday, August 20, 2008 10:07 PM

Answers

  • DragMove() will send WM_SYSCOMMAND to the window with wParam set to SC_MOVE+HTCAPTION, in my personal understanding, this will effectively achieve the same effect as moving the window by left clicking the window title bar, much like what WM_NCHITTEST message does which apparently is not the message get you when righting click the non client area of the window.

    The following code could achieve the same effect as what the DragMove() does:

    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
            Point startPosition = new Point();
            this.PreviewMouseRightButtonDown += (sender, e) =>
            {
                startPosition = e.GetPosition(this);
            };

            this.PreviewMouseMove += (sender, e) =>
            {
                if (e.RightButton == MouseButtonState.Pressed)
                {
                    Point endPosition = e.GetPosition(this);
                    Vector vector = endPosition - startPosition;
                    this.Left += vector.X;
                    this.Top += vector.Y;
                }
            };
        }
    }

    Hope this helps
    • Marked as answer by Marco Zhou Wednesday, August 27, 2008 10:24 AM
    Tuesday, August 26, 2008 8:30 AM