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

When I moving my window WPF with "this.DragMove();" method into around desktop space, after it "Body_MouseLeftButtonUp" event don't occur, why?

Notice: The Body is a ModelUIElement3D and some window properties set to Background="{x:Null}", AllowsTransparency="True" and WindowStyle="None".
C#
public partial class myWindow: Window
{
    public myWindow()
    {
        this.InitializeComponent();

        Body3D.MouseLeftButtonDown+=new MouseButtonEventHandler(Body3D_MouseLeftButtonDown);
        Body3D.MouseLeftButtonUp+=new MouseButtonEventHandler(Body3D_MouseLeftButtonUp);
    }

    private void Body_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
    {
        this.DragMove();
    }

    private void Body3D_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
    {
        //After 'Body_MouseLeftButtonDown', not occur this event!
    }
}


Any help pls...

Thanks in advance.
Posted
Updated 3-Nov-11 19:50pm
v8

The event doesn't occur because DragMove is a blocking method until the button is released. Unlikely it's going to work on another thread.

I personally would roll my own using the MouseLeftButtonDown MouseMove MouseLeftButtonUp events if I needed a non-blocking DragMove.
 
Share this answer
 
Comments
hzawary 3-Nov-11 10:36am    
Thanks Mark, means you to write a DragMove code? how do write a DragMove() easily and high performance as well as DragMove WPF method?
Mark Salsbery 3-Nov-11 11:18am    
Performance shouldn't be an issue. The entire Windows and WPF system uses Direct3D on the Windows Display Driver model these days.

You can do the usual drag code - on mousebuttondown set a InDrag flag and save the coordinates. On mousemove if InDrag is true get the current cursor coordinates and adjust the window position using the delta from the previously saved coordinates. Save the new current coordinates. On mouseup set InDrag to false.
hzawary 3-Nov-11 12:08pm    
Thanks again, have you this code in ready? I want save time;)
Sergey Alexandrovich Kryukov 3-Nov-11 22:37pm    
Please see my solution -- all works, no blocking; tested.
--SA
I cannot see any sense in setting up event handlers. In your Windowystem.Windows.Window anyway, so it's much better to write overloaded methods.

I checked up that the following works correctly:
C#
public partial class MyWindow : Window {
    public MyWindow() {
        InitializeComponent();
        // you could add anonymous event handlers here but there is no need to do it
        // for Window events, see below
    }

    protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e) {
        base.OnMouseLeftButtonDown(e);
        isMouseDown = true;
        this.DragMove();
    }
    protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e) {
        base.OnMouseLeftButtonUp(e);
        isMouseDown = false;
    }

    bool isMouseDown; //do you use it?

} //class MyWindow


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.

Good luck,
—SA
 
Share this answer
 
Comments
hzawary 3-Nov-11 22:45pm    
Oh sorry! I did copy&paste it from this:

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;
}

Tried this, But While Moving it, the window do skipping to around!
Did you test above code?! Please test it and help me:)

http://www.codeproject.com/Questions/277732/How-do-I-moving-an-irregularly-wpf-window-in-deskt
Sergey Alexandrovich Kryukov 3-Nov-11 23:30pm    
Please stop it. 1) Explain the purpose. Is it only dragging? Then you don't need it. 2) Replace is with the code I provided and add whatever you need except dragging. 3) Please stop commenting on unrelated pages.
--SA
Sergey Alexandrovich Kryukov 3-Nov-11 23:58pm    
No need to test it. You ignore my advice. Re-write it with overridden methods and Window.DragMove. Add something else if you need to -- now see next issue:

Also, you don't show the purpose of your event handlers beyond dragging. But dragging is already solved using Window.DragMove. So, you also ignored my question: do you need any handling except dragging?
--SA
hzawary 4-Nov-11 0:13am    
Actually, I need to (Body3D_MouseLeftButtonUp) event after (Body_MouseLeftButtonDown) event, but don't occur (Body3D_MouseLeftButtonUp) event after (Body_MouseLeftButtonDown) event, when I removing the DragMove method from (Body_MouseLeftButtonDown) event, then occur (Body_MouseLeftButtonDown) event.
I also don't want moving window by (MouseLeftButtonDown) event of MyWindow, I need moving the window by (MouseLeftButtonDown) event of ModelUIElement3D.

So sorry for commenting...
Sergey Alexandrovich Kryukov 4-Nov-11 1:26am    
Look, first of all, Body3D_MouseLeftButtonUp and Body_MouseLeftButtonDown are not events at all, do you understand it? To show what happens with the events, you should show all "+=" operators.

Secondly, you never mentioned ModelUIElement3D, and these problems are related. Now the names of the methods in your question suggest that you try to handle "MouseDown" in the event of one UIElement and "MouseUp" in another one. No wonder it could not work. Please check it up.

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