Click here to Skip to main content
15,884,177 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi,

Ok, my problem is about DragMove() method in WPF, when I use this method on MouseLeftButtonDown event, after it don't work other commands OnMouseLeftButtonUp event.

Please tell me how to show the message after DragMove.
C#
public partial class myWindow : Window
{
    public myWindow()
    {
        this.InitializeComponent();
    }

    protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
    {
        DragMove();
        base.OnMouseLeftButtonDown(e);
    }

    protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)
    {
        MessageBox.Show("this is a test");
        base.OnMouseLeftButtonUp(e);
    }
}


In addition, I tried to simulate the DragMove, but it doesn't work correctly! actually, when I move the window to around quickly, it don't remain with cursor!
C#
public partial class myWindow : Window
{
    public myWindow()
    {
        this.InitializeComponent();

        inDrag = false;
    }

    bool inDrag;
    Point dragPoint;

    protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
    {
        dragPoint = e.GetPosition(this);
        inDrag = true;
    }

    protected override void OnMouseMove(MouseEventArgs e)
    {
        if (inDrag)
        {
            Point pointMoveTo;

            // Find the current mouse position in screen coordinates.
            pointMoveTo = this.PointToScreen(e.GetPosition(this));

            // Compensate for the position the control was clicked.
            pointMoveTo.Offset(-dragPoint.X, -dragPoint.Y);

            // Compensate for the non-client region (title bar).
            // This code is not necessary if you explicitly hide the title bar
            //  by setting the form's BorderStyle to None.
            //pointMoveTo.Offset(0, -25);

            // Move the window.
            this.Left = pointMoveTo.X;
            this.Top = pointMoveTo.Y;
        }
        base.OnMouseMove(e);
    }

    protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)
    {
        inDrag = false;

        MessageBox.Show("this is a test");
        base.OnMouseLeftButtonUp(e);
    }
}


Any help is appreciated.

Thanks in advance.
Posted
Updated 16-Nov-11 4:00am
v2
Comments
Mark Salsbery 16-Nov-11 11:09am    
Yes DragMove blocks and consumes the mouseup event. Yes you are still calculating coordinates incorrectly as I stated repeatedly in your last thread on this topic :)
hzawary 16-Nov-11 11:14am    
please, you tell me about this, what should to do?
hzawary 16-Nov-11 11:20am    
In this simple Dragmove, What's your solution?
hzawary 16-Nov-11 11:22am    
You can write this simple dragmove for me?
hzawary 16-Nov-11 11:31am    
Do you test this codes?!, If the coordinates are incorrect, you write it correctly please?

Here's an example I ripped from my examples posted here[^] and here[^]...maybe it will work.

C#
public partial class myWindow : Window
{
    public myWindow()
    {
        this.InitializeComponent();
    }

    bool inDrag = false;
    Point anchorPoint;

    protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
    {
        anchorPoint = PointToScreen(e.GetPosition(this));
        inDrag = true;
        CaptureMouse();
        e.Handled = true;
    }

    protected override void OnMouseMove(MouseEventArgs e)
    {
        if (inDrag)
        {
            Point currentPoint = PointToScreen(e.GetPosition(this));
            this.Left = this.Left + currentPoint.X - anchorPoint.X;
            this.Top = this.Top + currentPoint.Y - anchorPoint.Y;
            anchorPoint = currentPoint;
        }
    }

    protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)
    {
        if (inDrag)
        {
            ReleaseMouseCapture();
            inDrag = false;
            e.Handled = true;
        }
    }
}
 
Share this answer
 
v5
Comments
hzawary 16-Nov-11 13:11pm    
Thanks Mark, I tried above code, but the window shaking and jumping when dragging to around:(
Mark Salsbery 16-Nov-11 13:30pm    
Did you try replacing the two e.GetPosition(null) calls with the code I noted above?

PointToScreen(e.GetPosition(this))

*edit* never mind - I see you did!
hzawary 16-Nov-11 13:29pm    
Oh! very very nice! I tried 'PointToScreen(e.GetPosition(this))' instead 'e.GetPosition(null)' and solved it!
Since I've bother you, I apologize.
Finally after a long time the issue was resolved!
Thanks again, good luck! My 5 and accept!
Mark Salsbery 16-Nov-11 13:31pm    
Oh cool! I'll change the code above. Glad you finally got it working!
Wayne Gaylard 17-Nov-11 0:52am    
+5 for your patience. Excellent!
This is a solution in addition to Mark Salsbery's. His code works, but when you have a control on the area that your window is dragged when you perform a mousedown, it's events don't work because the CaptureMouse() function blocks it.

For example, I have a "close" button (which is actually a label), but it's mousedown event won't work because the CaptureMouse() function somehow blocks it.

Solution is to use capturemouse function in the "OnMouseMove" event. So if mouse doesn't move after mousedown event, capturemouse will not block other controls' events.

C#
private bool inDrag = false;
private Point anchorPoint;

private bool iscaptured = false;

protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
{
	anchorPoint = PointToScreen(e.GetPosition(this));
	inDrag = true;
}

protected override void OnMouseMove(MouseEventArgs e)
{
	if (inDrag) {
		if (!iscaptured) {
			CaptureMouse();
			iscaptured = true;
		}
		Point currentPoint = PointToScreen(e.GetPosition(this));
		this.Left = this.Left + currentPoint.X - anchorPoint.X;
		this.Top = this.Top + currentPoint.Y - anchorPoint.Y;
		anchorPoint = currentPoint;
	}
}

protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)
{
	if (inDrag) {
		inDrag = false;
		iscaptured = false;
		ReleaseMouseCapture();
	}
}
 
Share this answer
 
This might be a little old but I ran into a similar issue recently and adapted a solution on a similar issue to enable this to work without a jerky motion. Using the PreviewMouseLeftButtonDown and PreviewMouseMove events, you compare the initial point and current point and only call DragMove when the mouse actually starts moving.
C#
private Point startPoint;
private void Window_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    startPoint = e.GetPosition(this);
}

private void Window_PreviewMouseMove(object sender, MouseEventArgs e)
{
    Point newPoint = e.GetPosition(this);
    if (e.LeftButton == MouseButtonState.Pressed && (Math.Abs(newPoint.X - startPoint.X) > SystemParameters.MinimumHorizontalDragDistance ||
        Math.Abs(newPoint.Y - startPoint.Y) > SystemParameters.MinimumVerticalDragDistance))
    {
        this.DragMove();
    }
}
 
Share this answer
 
Comments
hzawary 23-Feb-14 5:27am    
Oh, yes it is old story! thanks for your solution, but now I try below code and it is in correct. Perhaps, because I having use VisualStudio2012 in Windows8.1. Maybe Microsoft solved the bug, I don't know, just I know that now is work correctly and suggestion for you this simple code to drag the window.

public partial class myWindow : Window
{
public myWindow()
{
this.InitializeComponent();
}

protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
{
DragMove();
base.OnMouseLeftButtonDown(e);
}

protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)
{
MessageBox.Show("this is a test");
base.OnMouseLeftButtonUp(e);
}
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900