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

查看:104
本文介绍了“幽灵" 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天全站免登陆