"幽灵"WinForms 中的 MouseMove 事件 [英] "Ghost" MouseMove event in WinForms

查看:32
本文介绍了"幽灵"WinForms 中的 MouseMove 事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义控件,它通过处理 MouseMove 事件用当前鼠标坐标更新两个 NumericUpDowns.

I have a custom control that updates two NumericUpDowns with the current mouse coordinates by handling the MouseMove event.

但是,我遇到了一个奇怪的情况,即使这不应该发生(我已经关闭了无线鼠标并禁用了触控板),也会触发 MouseMove 事件.所以,即使鼠标不动,MouseMove 也会发生.

However I have experienced a strange case where the MouseMove event is fired even when this should not happen (I have turned off my wireless mouse and disabled the trackpad). So, MouseMove happens even if the mouse does not move.

我做了一个简单的检查并验证了在这些幽灵"MouseMove 事件之间,鼠标指针的位置没有改变.我充分利用了这一点,以确保仅在指针实际移动时才调用处理事件的方法,因此我已经能够满足我的应用程序的要求.

I've made a simple check and verified that between these "ghost" MouseMove events the position of the mouse pointer does not change. I exploited this to my advantage to make sure that the method handling the event is only called when the pointer has actually moved, so I have been able to meet the requirements for my application.

然而,我仍然感到困惑,因为我不明白当所有鼠标/触控板都被禁用时,什么会触发 MouseMove 事件.查看 Visual Studio 中的调用堆栈,在我看来,MouseMove 事件的触发是真实的",就好像鼠标真的移动了一样,而不是被其他部分触发了应用程序(例如模拟 MouseMove).

However, I am still puzzled, as I don't understand what could fire MouseMove events when all mice/trackpads are disabled. Looking at the call stack in Visual Studio, it seems to me that the firing of the MouseMove event is "genuine", as if the mouse really moved, not as if it was fired by some other part of the application (e.g. to simulate a MouseMove).

关于可能是什么原因的任何建议?谢谢!

Any suggestions on what might be the reason for this? Thank you!

按照 King King 的建议,我修改了代码以在每次检测到 MouseMove 的消息时打印一个字符串.我注意到当鼠标关闭并且指针悬停在控件上时,当我 Alt-Tab 在应用程序之间切换时会打印字符串.也许这与重新绘制的应用程序有关?

Following King King's suggestion, I modified the code to print a string every time the message for MouseMove is detected. I've noticed that when the mouse is off and the pointer is hovering the control, the string is printed when I Alt-Tab to switch between application. Perhaps this has something to do with the application being redrawn?

推荐答案

Windows 有时会合成假的 WM_MOUSEMOVE 消息.这尤其会发生在从一个窗口到另一个窗口的焦点变化时.获得焦点的窗口也获得移动消息.当您使用 Alt+Tab 时看到这种情况是一个很好的线索,这确实是消息的来源.

Windows synthesizes a fake WM_MOUSEMOVE message sometimes. This in particular will happen on a focus change from one window to another. The window that gets the focus also gets the move message. Seeing this happen when you use Alt+Tab is an excellent lead that this is indeed the source of the message.

这完全是故意的.它确保 WM_MOUSEMOVE 触发的其他消息序列将会发生.特别是 WM_NCHITTEST 和 WM_SETCURSOR.以便显示正确的鼠标光标形状.否则会在 Winforms 中受到 Control.Cursor 和 Application.UseWaitCursor 属性的影响.

This is all entirely intentional. It ensures that the other train of messages triggered by WM_MOUSEMOVE will occur. In particular WM_NCHITTEST and WM_SETCURSOR. So that the correct mouse cursor shape is displayed. Otherwise affected in Winforms by the Control.Cursor and Application.UseWaitCursor properties.

你必须忍受这个,确保这永远不会成为问题.

You'll have to live with this, do make sure that this is never a problem.

这篇关于"幽灵"WinForms 中的 MouseMove 事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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