【问题标题】:Cancel textbox from sending MouseLeave Messages取消发送 MouseLeave 消息的文本框
【发布时间】:2011-10-23 07:19:41
【问题描述】:

有没有办法可以取消文本框中的WM_MOUSELEAVE 消息?

我在它上面直接有另一个控件(我正在尝试获取 Windows 绘制的边框)。我在该控件的MouseMove 事件中手动调用WM_MOUSEMOVE,以使文本框周围的Aero 蓝色边框亮起。使用 Spy++,我看到它正在触发 WM_MOUSELEAVE,即使我仍在它的范围内。这会导致蓝色边框闪烁消失/重新出现。

edit 我尝试了@Jeroen 的回答,它减少了闪烁,但我仍然无法保持发光或者它停留的时间太长。

        if (m.Msg == (int)Win32Api.WindowsMessages.MouseLeave)
        {
            var mousePosition = PointToClient(MousePosition);
            if (mousePosition.X < 0 || mousePosition.X > Width ||
                mousePosition.Y < 0 || mousePosition.Y > Height)
                base.WndProc(ref m);
            return;
        }

        base.WndProc(ref m);

【问题讨论】:

  • 你钩入了消息循环(WndProc)吗?
  • 设置控件的焦点能达到同样的效果吗?
  • @Mehran - 我已经设置了焦点。这是用户将鼠标悬停在文本框上,但没有聚焦它的时候。
  • @Jeroen - 我如何为文本框做到这一点?
  • 我希望我能帮助你更多,但我不明白 2 个控件在彼此之上以及窗口绘制的边框是如何相互关联的。

标签: c# .net winforms winapi pinvoke


【解决方案1】:

也许这就是您要找的。它需要您将 TextBox 定义替换为 tb 但也许有更优雅的方式。

    public class tb : TextBox
            {

                private const int  WM_MOUSELEAVE = 0x02A3;

                protected override void WndProc(ref Message m)
                {
                    if (m.Msg == WM_MOUSELEAVE)
                    {
                        // not passing the message on, so does nothing.
                        // handle it yourself here or leave empty.
                    }
                    else
                    {
                        // otherwise let windows handle the message
                        base.WndProc(ref m);
                    }
                }
            }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多