Click here to Skip to main content
15,891,423 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi everybody,

I have a WPF window with Background="{x:Null}", AllowsTransparency="True" and WindowStyle="None" properties and there is an object within it, I want moving this irregular window by cursor mouse in desktop space with below code, but not work correctly, is there any idea?
C#
bool inDrag;
Point dragPoint;

private void Body3D_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
    dragPoint = e.GetPosition(this);
    inDrag = true;
}

private void Body3D_MouseMove(object sender, 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.Top = pointMoveTo.X;
        this.Left = pointMoveTo.Y;
    }
}

private void Body3D_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
    inDrag = false;
}


Any help pls...

Thanks in advance.
Posted
Updated 3-Nov-11 13:44pm
v2
Comments
Sergey Alexandrovich Kryukov 3-Nov-11 20:09pm    
What exactly is not working?
--SA
hzawary 3-Nov-11 22:02pm    
Did you test above code?!
While Moving it, the window do skipping to around!
Please test it and help me:)
Sergey Alexandrovich Kryukov 3-Nov-11 23:05pm    
I don't see a reason to test it. Make it the way I demonstrated in my answer and then let's discuss if you still have any problems.
--SA
hzawary 3-Nov-11 23:13pm    
I understand your answer -=I don't see if you use isMouseDown for anything? Do you really need this status for something else, because for dragging it's not needed? I hope you do.=-

But I update the http://www.codeproject.com/Questions/277094/Why-DragMove-hold-up-some-of-other-event

and explain it for you...
Sergey Alexandrovich Kryukov 4-Nov-11 1:27am    
No you did not. See my comments of that page.
--SA

1 solution

You not gonna believe that, but this is all you need :-):

C#
public partial class MyWindow : Window {

    //...

    protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e) {
        base.OnMouseLeftButtonDown(e);
        this.DragMove(); //begin dragging mode
    } //...

} //class MyWindow


That is, it's already done for you. See http://msdn.microsoft.com/en-us/library/system.windows.window.dragmove.aspx[^].

—SA
 
Share this answer
 
v2
Comments
hzawary 3-Nov-11 20:15pm    
You consider issue that I posted several hour ago please 'Why "this.DragMove()" hold up some of other "events"? Not Solved Yet!'

http://www.codeproject.com/Questions/277094/Why-DragMove-hold-up-some-of-other-event
Sergey Alexandrovich Kryukov 3-Nov-11 23:04pm    
I answered that question, too, please see.
--SA

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