如何捕获面板外的鼠标点击
如何捕获 Windows 窗体应用程序中面板外的鼠标单击?
How can I capture a mouse click outside of a panel in an Windows Form application?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
如何捕获 Windows 窗体应用程序中面板外的鼠标单击?
How can I capture a mouse click outside of a panel in an Windows Form application?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(3)
要捕获全局鼠标和键盘事件,您需要捕获 Windows 消息 WM_MOUSE_LL 和 WM_KEYBOARD_LL,而不仅仅是 WM_MOUSE 和 WM_KEYBOARD。这些事件只能在 NT/2000/XP 中捕获。在更高版本的操作系统中,这是不可能的(我认为出于相当明显的安全原因)。
如果您使用的是 NT/2000/XP,这里有一些示例代码:
http://www. codeproject.com/KB/cs/globalhook.aspx
To capture global mouse and keyboard events you need to capture the windows messages WM_MOUSE_LL and WM_KEYBOARD_LL, not just WM_MOUSE and WM_KEYBOARD. These events can only be captured in NT/2000/XP. In later OS versions this is not possible (for fairly obvious security reasons I assume).
If you are using NT/2000/XP here is some example code:
http://www.codeproject.com/KB/cs/globalhook.aspx
答案就在你的问题中,将面板的 Capture 属性设置为 true 。所有鼠标输入事件现在都定向到面板,即使鼠标位于面板窗口之外也是如此。然而,这是一个临时条件(应该是这样),单击按钮将在单击传递到面板后取消捕获。 MouseCaptureChanged 事件让您知道发生的情况。无条件捕获鼠标不是一个选项,例如键入 Ctrl+Esc 将始终取消它。
The answer is in your question, set the panel's Capture property to true. All mouse input events are now directed to the panel, even if the mouse is outside of the panel window. This is however a temporary conditions (as it should be), a button click is going to cancel the capture after the click is delivered to the panel. The MouseCaptureChanged event lets you know when that happened. Unconditionally capturing the mouse is not an option, typing Ctrl+Esc for example will always cancel it.
您可以处理 MouseClick 事件表格本身。
You can handle the MouseClick event of the form itself.