C#wndproc覆盖无法在表单外部工作 [英] C# wndproc override not working outside form

查看:168
本文介绍了C#wndproc覆盖无法在表单外部工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我一直在尝试在Windows窗体中实现wndproc重写以检测在窗体外的鼠标单击.

我在论坛上到处都有阅读,但我不明白为什么我的代码无法正常工作.它应该很简单.

这是我的代码:

Hello,

I have been trying to implement wndproc override in windows form to detect a mouse click outside the form.

I''ve been reading everywhere on forums and I dont understand why my code doesnt work. Its supposed to be simple.

this is my code:

protected override void WndProc(ref Message m)
        {
            if (m.Msg == WM_LBUTTONDOWN)
            {
                MessageBox.Show("Mouse Clicked!");
            }
            base.WndProc(ref m);
        }



我正在使用Visual Studio 2008和Windows 7 OS.

谢谢您的帮助.



I''m using Visual Studio 2008 and Windows 7 OS.

Thank you for your help.

推荐答案

正如Pete所说,鼠标必须位于与窗口消息队列关联的窗口内,WndProc才能获取消息.如果需要查看外部鼠标事件,则需要捕获鼠标或安装Windows挂钩.有很多选择(和陷阱).尝试这篇文章:

在C#中处理全局鼠标和键盘挂钩 [
As Pete said, the mouse has to be inside the window associated with the window message queue for the WndProc to get the messages. If you need to see external mouse events, you need to capture the mouse or install a Windows hook. There are a lot of options (and pitfalls.) Try this article:

Processing Global Mouse and Keyboard Hooks in C#[^]


根据您对我的其他帖子的回复:

在此处接收消息的窗口是桌面.单击桌面不会导致您的表单收到消息.请记住,我说的是WndProc是每个窗口的窗口-您的窗口不是接收消息的窗口,因此它不会传播此消息.
Based on your reply to my other post:

The window that receives the message here is the desktop. Clicking the desktop will not result in your form receiving the message. Remember that I said that the WndProc is per window - your window isn''t the one that''s receiving the message, so it won''t propagate this.


哪种表格具有您将此代码添加到了吗?消息被泵送到对它们感兴趣的窗口,因此您需要确保已将此功能添加到了您希望接收点击的表单中,而不是要排除的表单中.
Which form have you added this code to? Messages are pumped to the window that is interested in them, so you would need to ensure that you have added this function to the form that you were interested in receiving the click in, rather than the form you want to exclude.


这篇关于C#wndproc覆盖无法在表单外部工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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