开发者

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

开发者 https://www.devze.com 2023-01-11 06:24 出处:网络
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:

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消