locked
A question about mouse move event RRS feed

  • Question

  • Hello.

    I have a mouse move event that drags a label.

    If I drag the label too fast, the event stops until I return the mouse back to the label.

    bool dragging=false;
            double clickOffsetX;
            double clickOffsetY;
            void y_MouseMove(object sender, MouseEventArgs e)
            {
                if (dragging)
                {
                    Canvas.SetLeft(sender as UIElement, Mouse.GetPosition(null).X);
                    Canvas.SetTop(sender as UIElement, Mouse.GetPosition(null).Y);
                }
            }
    
            void y_MouseUp(object sender, MouseButtonEventArgs e)
            {
                dragging = false;
                
            }
            void y_MouseDown(object sender, MouseButtonEventArgs e)
            {
                dragging = true;
                clickOffsetX = e.GetPosition(null).X;
                clickOffsetY = e.GetPosition(null).Y;
    
                
            

    How can i make it work?

    Is there something that makes the label drag too slow or is there something wrong with the code?

    Thanks.


    • Moved by Leo Liu - MSFT Monday, February 27, 2012 6:55 AM Moved for better support. (From:Visual C# General)
    Saturday, February 25, 2012 5:22 PM

Answers

  • Here I have found two more fruitful links for you.

    A similar problem resolved here using Mouse.Capture method. http://social.msdn.microsoft.com/forums/en-US/wpf/thread/0222a4db-616c-4a9a-afa3-16382fcca253/

    Mouse.Capture on Bol: http://msdn.microsoft.com/en-us/library/cc189029%28v=vs.95%29.aspx#mouse_capture

    Excerpt:

    "You can use the CaptureMouse and ReleaseMouseCapture methods to enable and disable mouse capture. When an object has captured the mouse, it receives mouse input regardless of whether the mouse pointer is within its bounds.

    For example, if an object retains mouse capture and the mouse pointer is moved outside the object's bounds, the object holding capture will continue to receive MouseMove events. The object that the mouse pointer is apparently over during this capture does not receive MouseMove events.

    Mouse capture requires that the mouse button (the left mouse button in cases where there are left and right mouse buttons) is down during the entire time that the object intends to hold mouse capture. The moment the mouse button is released, mouse capture is lost by that object, and the mouse is not considered captured by any object. This condition raises the LostMouseCapture event on the object that originally held capture.

    A major scenario for capturing the mouse is to implement visual positioning "drag-and-drop" operations, or variations thereof. For more information about capturing the mouse, see How to: Drag and Drop Objects in UI Layout."

    Using these event's you can solve your problem. I believe this is your best bet.


    Welcome to MSDN Forums.
    Feel free to ask your questions and Please Note to Vote helpful topics and Mark answering posts.

    You Value Community's efforts and Community values your faith.
    - Arun Kumar Allu

    • Marked as answer by Igor_abc Monday, February 27, 2012 12:19 PM
    Saturday, February 25, 2012 7:20 PM
  • Once you mentioned Mouse.Capture() i started testing stuff with it(I didn't know of its existence) and found the solution !

    Here is the complete code of my dragging:

            bool dragging=false;
            void y_MouseMove(object sender, MouseEventArgs e)
            {
                if (dragging && sender is Label)//The Label can be changed into any control you want
                {
                    Mouse.Capture(sender as UIElement);
                    Canvas.SetLeft(sender as UIElement, Mouse.GetPosition(null).X-15);
                    Canvas.SetTop(sender as UIElement, Mouse.GetPosition(null).Y-15);
                }
            }
    
            void y_MouseUp(object sender, MouseButtonEventArgs e)
            {
                dragging = false;
                Mouse.Capture(null);
                
            }
            void y_MouseDown(object sender, MouseButtonEventArgs e)
            {
                dragging = true; 
            }
    It's not perfect but it allows dragging the object around.



    • Marked as answer by Igor_abc Monday, February 27, 2012 12:19 PM
    • Edited by Igor_abc Monday, February 27, 2012 12:22 PM
    Monday, February 27, 2012 12:18 PM
  • Check this article for how to work on mouse.capture method.

    How do I use CaptureMouse or Mouse.Capture in my C# WPF application? on stackoverflow

    For the above said problem, this is an article i found. You might look at this even for an option. This following solution spans 3 mouse events MouseEnter, MouseMove, MouseLeave.

    http://stackoverflow.com/questions/2268114/mouse-moves-too-fast-to-capture-events

    I would suggest you go for Mouse.Capture and it will solve ur issue. If not this above option.

    Let me know if you still face any issues.


    Welcome to MSDN Forums.
    Feel free to ask your questions and Please Note to Vote all helpful topics and Mark all answering posts.

    You Value Community's efforts and Community values your faith.
    - Arun Kumar Allu


    • Edited by arun.passioniway Saturday, February 25, 2012 7:36 PM typo edit
    • Proposed as answer by arun.passioniway Monday, February 27, 2012 1:44 PM
    • Marked as answer by Igor_abc Wednesday, February 29, 2012 7:53 AM
    Saturday, February 25, 2012 7:29 PM

All replies

  • Whn you move the mouse, .NET executes the event handler which does take some time. during that time the UI becomes non-responsive.

    When you move the mouse too fast, then the application, I believe, loses the mouse on top of it. And the mouse move event might ot be tracked.

    What I would suggest to figure out the problem.

    • Add a breakpoint to mouse up and see if it is fired and your dragging is set to false on fast movements.
    • Bring back the mouse on top of the label after you lost the effect and check your dragging value.
    • this way you can find out if you the event firing is stopped or any other issue.

    If you see, click on a button and holding the mouse down, you move the cursor away from the button and release the mouse, the button click event won't fire. Coz the mouse is not over the button. I see that relation in here.

    One other thing:

    Check this: Double buffered graphics @ http://msdn.microsoft.com/en-us/library/b367a457.aspx

    It is about flicker in UI app's. Here the UI drawing is delayed and changes are kept in memory buffer and made to UI later. This reduces flicker and in your case too, this will help.

    Hope this helps. Let me know if still any issues.


    Welcome to MSDN Forums.
    Feel free to ask your questions and Please Note to Vote helpful topics and Mark answering posts.

    You Value Community's efforts and Community values your faith.
    - Arun Kumar Allu

    Saturday, February 25, 2012 6:07 PM
  • You are right, the mouse up even wasn't fired(it did fire once i let go of the mouse on the label).

    Is there a way i can continue to drag the object?

    Thanks.

    Saturday, February 25, 2012 6:14 PM
  • Hmmm... so the mouse move even doesn't fire after your app loses focus.

    I cant say for sure if there is much that can be done here.

    Is your app WPF or winforms. WPF app's are faster than winforms and this effect is less there.

    You might consider using double buffered graphics. That can sort out the problem for you.

    Lemme check the internet and codeproj for any app logic that can sort out the issue for you.


    Welcome to MSDN Forums.
    Feel free to ask your questions and Please Note to Vote helpful topics and Mark answering posts.

    You Value Community's efforts and Community values your faith.
    - Arun Kumar Allu

    Saturday, February 25, 2012 6:36 PM
  • I am on WPF.

    How do i enable that double buffering in WPF?

    Thanks.

    Saturday, February 25, 2012 6:40 PM
  • Here I have found two more fruitful links for you.

    A similar problem resolved here using Mouse.Capture method. http://social.msdn.microsoft.com/forums/en-US/wpf/thread/0222a4db-616c-4a9a-afa3-16382fcca253/

    Mouse.Capture on Bol: http://msdn.microsoft.com/en-us/library/cc189029%28v=vs.95%29.aspx#mouse_capture

    Excerpt:

    "You can use the CaptureMouse and ReleaseMouseCapture methods to enable and disable mouse capture. When an object has captured the mouse, it receives mouse input regardless of whether the mouse pointer is within its bounds.

    For example, if an object retains mouse capture and the mouse pointer is moved outside the object's bounds, the object holding capture will continue to receive MouseMove events. The object that the mouse pointer is apparently over during this capture does not receive MouseMove events.

    Mouse capture requires that the mouse button (the left mouse button in cases where there are left and right mouse buttons) is down during the entire time that the object intends to hold mouse capture. The moment the mouse button is released, mouse capture is lost by that object, and the mouse is not considered captured by any object. This condition raises the LostMouseCapture event on the object that originally held capture.

    A major scenario for capturing the mouse is to implement visual positioning "drag-and-drop" operations, or variations thereof. For more information about capturing the mouse, see How to: Drag and Drop Objects in UI Layout."

    Using these event's you can solve your problem. I believe this is your best bet.


    Welcome to MSDN Forums.
    Feel free to ask your questions and Please Note to Vote helpful topics and Mark answering posts.

    You Value Community's efforts and Community values your faith.
    - Arun Kumar Allu

    • Marked as answer by Igor_abc Monday, February 27, 2012 12:19 PM
    Saturday, February 25, 2012 7:20 PM
  • Check this article for how to work on mouse.capture method.

    How do I use CaptureMouse or Mouse.Capture in my C# WPF application? on stackoverflow

    For the above said problem, this is an article i found. You might look at this even for an option. This following solution spans 3 mouse events MouseEnter, MouseMove, MouseLeave.

    http://stackoverflow.com/questions/2268114/mouse-moves-too-fast-to-capture-events

    I would suggest you go for Mouse.Capture and it will solve ur issue. If not this above option.

    Let me know if you still face any issues.


    Welcome to MSDN Forums.
    Feel free to ask your questions and Please Note to Vote all helpful topics and Mark all answering posts.

    You Value Community's efforts and Community values your faith.
    - Arun Kumar Allu


    • Edited by arun.passioniway Saturday, February 25, 2012 7:36 PM typo edit
    • Proposed as answer by arun.passioniway Monday, February 27, 2012 1:44 PM
    • Marked as answer by Igor_abc Wednesday, February 29, 2012 7:53 AM
    Saturday, February 25, 2012 7:29 PM
  • Thanks, I'll check those out !
    Saturday, February 25, 2012 7:50 PM
  • Thanks, I'll check those out !

    Go ahead and when solved please do post the solution you chose and how you did it.


    Welcome to MSDN Forums.
    Feel free to ask your questions and Please Note to Vote all helpful topics and Mark all answering posts.

    You Value Community's efforts and Community values your faith.
    - Arun Kumar Allu

    Saturday, February 25, 2012 8:02 PM
  • Once you mentioned Mouse.Capture() i started testing stuff with it(I didn't know of its existence) and found the solution !

    Here is the complete code of my dragging:

            bool dragging=false;
            void y_MouseMove(object sender, MouseEventArgs e)
            {
                if (dragging && sender is Label)//The Label can be changed into any control you want
                {
                    Mouse.Capture(sender as UIElement);
                    Canvas.SetLeft(sender as UIElement, Mouse.GetPosition(null).X-15);
                    Canvas.SetTop(sender as UIElement, Mouse.GetPosition(null).Y-15);
                }
            }
    
            void y_MouseUp(object sender, MouseButtonEventArgs e)
            {
                dragging = false;
                Mouse.Capture(null);
                
            }
            void y_MouseDown(object sender, MouseButtonEventArgs e)
            {
                dragging = true; 
            }
    It's not perfect but it allows dragging the object around.



    • Marked as answer by Igor_abc Monday, February 27, 2012 12:19 PM
    • Edited by Igor_abc Monday, February 27, 2012 12:22 PM
    Monday, February 27, 2012 12:18 PM
  • Wow Igor, Looks good and solved in a couple of lines of code.

    Thanx for posting the solution, this will help future users to find the solution.


    Welcome to MSDN Forums.
    Feel free to ask your questions and Please Note to Vote all helpful topics and Mark all answering posts.

    You Value Community's efforts and Community values your faith.
    - Arun Kumar Allu

    Monday, February 27, 2012 12:40 PM
  • No problem, thanks for helping out.
    Monday, February 27, 2012 12:41 PM