鼠标钩子和消息框问题
我试图钩住鼠标;我的 MouseProc 是
{
if(nCode==HC_ACTION)
{
if(wParam==WM_LBUTTONDOWN)
{
MessageBox(NULL,L"",L"",MB_OK);
return TRUE;
}
}
return CallNextHookEx(hHook,nCode,wParam,lParam);
}
我不知道为什么它会全局钩住我的鼠标,然后只需单击鼠标左键就会生成至少 10 个消息框。为什么错了?另外我想知道如何仅挂钩任务管理器中存在的特定进程而不是挂钩所有现有窗口?为此,我尝试将 SetWindowsHookEx() 的第四个参数设置为指定线程的 id,但似乎没有任何实际挂钩。 (虽然hHook返回的是非NULL)
谢谢。
I try to hook the mouse; and my MouseProc is
{
if(nCode==HC_ACTION)
{
if(wParam==WM_LBUTTONDOWN)
{
MessageBox(NULL,L"",L"",MB_OK);
return TRUE;
}
}
return CallNextHookEx(hHook,nCode,wParam,lParam);
}
I don't know why it gobal-hooks my mouse then generates at lease 10 message boxes for just one leftmouse button click. Why is it wrong ? Also I would like to know how can I hook just a particular process existing in the taskmanager rather than to hook all existing windows ? For this, I have tried to set SetWindowsHookEx()'s fourth parameter the id of the specified thread but it seems nothing actually hooked. (although hHook returned is non-NUll)
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如何挂钩特定进程?鼠标点击是全局的。您的意思是您想要挂钩某个窗口,您可以通过注入的 DLL 来实现,您可以在其中 子类表示窗口。
至于为什么每次点击你的钩子都会被调用几次, MSDN 是这么说的:
“每当应用程序调用 GetMessage 或 PeekMessage 函数并且有鼠标消息需要处理时,系统都会调用此函数。”
How can you hook a particular process? Mouse clicks are global. You mean you want to hook a certain window, which you can do via an injected DLL in which you subclass said window.
As for why your hook is called several times per click, MSDN says this:
"The system calls this function whenever an application calls the GetMessage or PeekMessage function and there is a mouse message to be processed."
对于初学者来说,您的钩子过程需要存在于 DLL 内。这是全局钩子的要求。您可以在线程 ID 上安装钩子,这是最好的方法,但如果需要,您也可以在钩子中进行一些进程 ID 检查
Your hook procedure needs to exist inside a DLL for starters. That's a requirement for a global hook. You can install the hook on a thread ID which is the best way, but you can also do some process ID inspection in your hook if you require