Osama
Osama

Reputation: 335

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

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.

Upvotes: 7

Views: 7533

Answers (3)

Eliahu Aaron
Eliahu Aaron

Reputation: 4542

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.

Upvotes: 0

xpda
xpda

Reputation: 15813

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.

Upvotes: 8

shenhengbin
shenhengbin

Reputation: 4294

I donnt think [mousedraw] could do well

click = mouseDown + mouseUp 

so ,

When mouseDown :  mousedraw = false;

When mouseUp :    mousedraw = true;

Upvotes: 0

Related Questions