El evento mouseleave y mouseenter de C # no se activará si se hace clic en el botón del mouse

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 hojas or entra the panel while the left button is clicked

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

Al problema es:

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

preguntado el 27 de agosto de 11 a las 23:08

This is a side-effect to the Control base class setting the Capture property to true on the mouse down event. This does no need fixing, drawing outside of the control Bounds doesn't show up anyway. -

2 Respuestas

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.

Respondido 29 ago 11, 04:08

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

I donnt think [mousedraw] could do well

click = mouseDown + mouseUp 

so ,

When mouseDown :  mousedraw = false;

When mouseUp :    mousedraw = true;

Respondido 28 ago 11, 03:08

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

I don't get how this should be used, maybe you should add some explanation - Breeze

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.