c ++:如何捕捉鼠标点击,无论何时发生 [英] c++: How to catch mouse clicks wherever they happen

查看:177
本文介绍了c ++:如何捕捉鼠标点击,无论何时发生的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个我正在编写的应用程序,我需要监视鼠标点击。



点击可能发生在屏幕上的任何地方,而不是在应用程序窗口



我环顾四周,阅读了一些建议,例如使用



LRESULT CALLBACK WndProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam)

但我未成功。



有没有人知道如何实现我需要的?

解决方案

您需要设置一个鼠标钩子,如 MSDN



注意,在你的情况下,钩子需要是全局的。这意味着您需要在DLL中实现一个处理函数,它将被加载到系统中接收鼠标消息的所有进程中。这样的DLL将使用一些进程间通信(IPC)机制(如共享内存)或通过从DLL到主应用程序(未发送)的Windows消息与主应用程序通信。



您可以使用此CodeProject文章作为指南。



更新:根据Chris的更正,适用于同步的常规鼠标钩。低级钩子不需要位于DLL中,但它具有一些限制,这些限制在相应的MSDN文章


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)

but I was unsuccessful.

Does anyone have an idea on how to implement what I need?

解决方案

You need to set a mouse hook as described in MSDN.

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.

You can use the source code from this CodeProject article as a guide.

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.

这篇关于c ++:如何捕捉鼠标点击,无论何时发生的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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