即使按住鼠标按钮,C# 也会触发 MouseEnter [英] C# triggering MouseEnter even even if mouse button is held down

查看:33
本文介绍了即使按住鼠标按钮,C# 也会触发 MouseEnter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个问题,即在按住鼠标按钮时不会触发名为MouseEnter"的事件.我该如何解决?

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

推荐答案

这是设计使然.你可以通过使用 MouseMove 来解决这个问题:

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
        }
    }
}

但这并不理想 - 如果按下鼠标按钮当鼠标悬停在表单上的另一个控件上时,那么它不会出现在按下按钮的 MouseMove 事件中(正如@Hans 指出的,另一个控件捕获"了 MouseDown).如果这是一个问题,那么在 MouseMove 中结合命中测试,同时在表单上单独跟踪 MouseDown 和 MouseUp 应该可以工作.

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.

这篇关于即使按住鼠标按钮,C# 也会触发 MouseEnter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆