MouseLeave and MouseEnter wont fire if mouse button is down (Windows Forms)
Asked Answered
R

3

7

The titles says it all. I have a panel that acts as a white board. On mouse move draw the mouse track.. works fine, but if the mouse leaves the edges of the panel, i want to call the mouse up event and mouse down event if the the mouse leaves or enters the panel while the left button is down

private void panel2_MouseLeave(object sender, EventArgs e)
{
    if (mousedraw == true)
    {
        panel2_MouseUp(sender, new MouseEventArgs(System.Windows.Forms.MouseButtons.Left, 0, MousePosition.X, MousePosition.Y, 0));
    }
}

private void panel2_MouseEnter(object sender, EventArgs e)
{
    if (mousedraw == true)
    {
        panel2_MouseDown(sender, new MouseEventArgs(System.Windows.Forms.MouseButtons.Left, 0, MousePosition.X, MousePosition.Y, 0));
    }
}

mousedraw is a bool to know if the left button is down.

The Problem:

The leave and enter events will not fire if the mouse button is down.

Responsory answered 27/8, 2011 at 23:29 Comment(1)
This is a side-effect to the Control base class setting the Capture property to true on the mouse down event. This does not need fixing, drawing outside of the control Bounds doesn't show up anyway.Reign
N
8

MouseEnter and mouseLeave do not fire while a button is pressed. However, when the button is eventually released, the appropriate mouseEnter or mouseLeave event fires if the mouse has moved in or out of the panel while the button was down. As long as the button is pressed, the mouseMove event will continue to fire, even outside the bounds of the panel. This allows the mouse to continue dragging or whatever even after it passes outside the control's boundary, and is the way most Windows applications work.

If you can use this behavior in your application, it will be a more "standard" user interface.

If you definitely need to fire a mouseUp when the mouse leaves the panel, you can check the mouse location in the mouseMove event and call mouseUp whenever it is outside the panel and the button is pressed. In the MouseMove handler you can use e.X and e.Y for the location, and e.Button for the button status.

When the mousebutton is pressed outside the control and moved inside, then the panel has no jurisdiction over the mouse, because the mouse is considered to be moving in the form or whatever control it was in when the button was pressed. So you might have trouble getting mouseDown to fire when the mouse button is pressed outside the panel and then moved inside the panel.

Nasal answered 28/8, 2011 at 2:52 Comment(1)
thank you, that was informative and helpful, i see now that the solution im seeking is not as simple as i thought it would, if (when) i handle this i will post the solution here for others to comment and benefit from.Responsory
Z
0

I donnt think [mousedraw] could do well

click = mouseDown + mouseUp 

so ,

When mouseDown :  mousedraw = false;

When mouseUp :    mousedraw = true;
Zymosis answered 27/8, 2011 at 23:47 Comment(2)
maybe i didnt explain enough, or i dont understand you. [mousedraw] is a bool that gets changed whenever a mouse down or mouse up events is hit, so in mouse down i set mousedraw to true, and on mouse up i set it to false.Responsory
I don't get how this should be used, maybe you should add some explanationDoehne
R
0

13 years later...

On the MouseDown event, set Capture to false.

private void panel2_MouseDown(object sender, MouseEventArgs e)
{
    panel2.Capture = false;
}

The mouse behavior will not be "standard" as already motioned in the answer by xpda, but if you are not preforming dragging, it seems it will have no undesirable effects and enable the MouseLeave event (and MouseEnter of other controls) to fire immediately.

From Control.Capture documentation:

When a control has captured the mouse, it receives mouse input whether or not the cursor is within its borders. The mouse is typically only captured during drag operations.

Reserpine answered 26/9 at 10:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.