C++:如何捕捉鼠标点击发生的任何地方
我被我正在编写的应用程序困住了,我需要监视鼠标点击.
I am stuck with an application I am writing where I need to monitor for mouse clicks.
点击可能发生在屏幕上的任何地方,而不是在应用程序窗口内,每次点击我都必须传递一条消息(执行一个操作或某事).
The clicks may happen anywhere on the screen and not inside the app window, and for each click I must pass a message (perform an action or something).
我环顾四周并阅读了一些建议,例如使用
I looked around and read some suggestions like using
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
但我没有成功.
有人知道如何实现我需要的东西吗?
Does anyone have an idea on how to implement what I need?
推荐答案
您需要按照 MSDN.
请注意,在您的情况下,钩子需要是全局的.这意味着您需要在 DLL 中实现一个处理函数,该函数将被加载到系统中所有接收鼠标消息的进程中.此类 DLL 将使用某些进程间通信 (IPC) 机制(如共享内存)或通过从 DLL 向主应用程序发布(未发送)的 Windows 消息与您的主应用程序通信.
Note that in your case the hook would need to be global. This means that you need to implement a handler function in a DLL, which will be loaded into all processes in the system which receive mouse message. Such DLL will communicate with your main application using some interprocess communication (IPC) mechanism like shared memory or via Windows messages posted (not sent) from the DLL to the main application.
您可以使用 this 中的源代码CodeProject 文章作为指南.
You can use the source code from this CodeProject article as a guide.
更新:根据 Chris 的更正,我应该注意到以上适用于同步的常规"鼠标钩子.低级钩子不需要位于 DLL 中,但它具有某些限制,在 相应的 MSDN 文章.
Update: as per Chris' correction, I should note that above applies to "regular" mouse hook which is synchronous. Low-level hook doesn't need to be located in the DLL, but it has certain limitations which are described in the corresponding MSDN article.
相关文章