简体   繁体   中英

C# triggering MouseEnter even even if mouse button is held down

I have this problem that event called "MouseEnter" does not fire when mouse button is held down. How could i fix it?

That's by design. You can work around it by using, say, MouseMove:

private void Form1_MouseMove(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Left)
    {
        Point pt = TargetControl.PointToClient(Cursor.Position);
        Rectangle rc = TargetControl.ClientRectangle;
        if (rc.Contains(pt))
        {
            // do what would be done on MouseEnter
        }
    }
}

This is not ideal, though - if the mouse button is pressed when the mouse is hovering over another control on the form , then it doesn't appear in the MouseMove event that the button is pressed (as @Hans pointed out, the other control 'Captures' the MouseDown). If that's a problem, then combining the hit test in MouseMove while separately tracking MouseDown and MouseUp on the form should work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM