简体   繁体   English

C#低级鼠标挂钩和表单事件处理

[英]C# low level mouse hook and form event handling

I'm using a simple form generated by VS 2010 which contains 2 buttons, start and stop. 我正在使用VS 2010生成的简单表单,其中包含2个按钮,即启动和停止。 Start triggers WH_MOUSE_LL using SetWindowsHookEx, and stop stops the hook. 使用SetWindowsHookEx启动触发器WH_MOUSE_LL,并停止挂钩。 The hook works fine and I mange to "replace" middle mouse button click with double click, the only problem I have is clicking on Minimize/Maximize/Close buttons of the form, it seems that there is some sort of "event race" between the hook and events called by buttons mentioned above. 钩子工作正常,我选择“替换”鼠标中键单击双击,我唯一的问题是点击表单的最小化/最大化/关闭按钮,似乎有某种“事件竞赛”之间上面提到的按钮调用的钩子和事件。 It reflects on the fact that when you press one of those buttons it keeps being "pressed" for a while before it completes it's action (eg form minimizes). 它反映了这样一个事实:当你按下其中一个按钮时,它会在完成它的动作之前保持“按下”一段时间(例如,形式最小化)。 When I right click the form and select one of those actions it responds immediately same thing goes when I stop the hook and press one of the buttons mentioned above. 当我右键单击表单并选择其中一个操作时,它会立即响应同样的事情,当我停止挂钩并按下上面提到的其中一个按钮时。 Have anyone encountered such behavior ? 有没有人遇到过这种行为?

I have experienced this before as well. 我以前也经历过这个。 I'm not sure as to the exact cause, but I have always solved the problem by listening for events that are fired when the mouse enters and leaves the client area of the form, window, whatever. 我不确定确切的原因,但我总是通过监听鼠标进入和离开窗体,窗口的客户区域时触发的事件来解决问题。 You can use those events to hook and unhook the mouse, and then you will get the normal behaviour. 您可以使用这些事件来挂钩和取消挂钩鼠标,然后您将获得正常的行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM