如何捕获面板外的鼠标点击

发布于 2024-11-03 03:53:25 字数 36 浏览 4 评论 0原文

如何捕获 Windows 窗体应用程序中面板外的鼠标单击?

How can I capture a mouse click outside of a panel in an Windows Form application?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

缪败 2024-11-10 03:53:25

要捕获全局鼠标和键盘事件,您需要捕获 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

夏日浅笑〃 2024-11-10 03:53:25

答案就在你的问题中,将面板的 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.

浪漫人生路 2024-11-10 03:53:25

您可以处理 MouseClick 事件表格本身。

class YourForm : Form
{
    protected override void OnMouseClick(MouseEventArgs e)
    {
        base.OnMouseClick(e);

        // Do something.
    }
}

You can handle the MouseClick event of the form itself.

class YourForm : Form
{
    protected override void OnMouseClick(MouseEventArgs e)
    {
        base.OnMouseClick(e);

        // Do something.
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文