Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I detect a held down mouse button over a PictureBox?

I need to fire an event when the mouse is above a PictureBox with the mouse button already clicked and held down.

Problems:

The MouseDown and MouseEnter event handlers do not work together very well.

For instance once a mouse button is clicked and held down, C# will fire the MouseDown event handler, but when the cursor moves over the PictureBox the MouseEnter event does not fire, until the mouse button is realeased.

like image 938
Justin Tanner Avatar asked Oct 04 '08 02:10

Justin Tanner


People also ask

What is the difference between Mousedown and click?

Note: This differs from the click event in that click is fired after a full click action occurs; that is, the mouse button is pressed and released while the pointer remains inside the same element. mousedown is fired the moment the button is initially pressed.

How can I tell if my mouse is down?

We can check if a mouse button is kept down by listening to the mousedown and mouseup events and mount how many times the mouse button is pressed down or lifted up.

What is Mousedown event in C#?

Occurs when the mouse pointer is over the control and a mouse button is pressed. public: event System::Windows::Forms::MouseEventHandler ^ MouseDown; C# Copy.


2 Answers

When the mouse is pressed down most controls will then Control.Capture the mouse input. This means that all MouseMove events are sent to the original control that captured rather than the control the mouse happens to be over. This continues until the mouse loses capture which typically happens on the mouse up.

If you really need to know when the mouse is over your control even when another control has captured mouse input then you only really have one way. You need to snoop the windows messages destined for other controls inside your application. To do that you need add a message filter ...

Application.AddMessageFilter(myFilterClassInstance);

Then you need to implement the IMessageFilter on a suitable class...

public class MyFilterClass : IMessageFilter
{
    public bool PreFilterMessage(ref Message m)
    {
        if (m.Msg == WM_MOUSEMOVE)
            // Check if mouse is over my picture box!

        return false;
    }
}

Then you watch for mouse move events and check if they are over your picture box and do whatever it is you want to do.

like image 144
Phil Wright Avatar answered Sep 21 '22 07:09

Phil Wright


Mouse events

Use the MouseDown event to just detect a down press of a mouse button and set this.Capture to true so that you then get other mouse events, even when the mouse leaves the control (i.e. you won't get a MouseLeave event because you captured the mouse). Release capture by setting this.Capture to false when MouseUp occurs.

Just checking the state of the mouse

This may not be relevant, but you can check System.Windows.Control.MousePosition and see if it is in the PictureBox.ClientRectangle, then check the Control.MouseButtons static property for which buttons might be down at any time.

As in:

if  (pictureBox.ClientRectangle.Contains(pictureBox.PointToClient(Control.MousePosition)))
{
   if ((Control.MouseButtons & MouseButtons.Left) != 0)
   {
     // Left button is down.
   }
}
like image 32
Jeff Yates Avatar answered Sep 21 '22 07:09

Jeff Yates