C++:如何捕捉鼠标点击发生的任何地方

c++: How to catch mouse clicks wherever they happen(C++:如何捕捉鼠标点击发生的任何地方)
本文介绍了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.

这篇关于C++:如何捕捉鼠标点击发生的任何地方的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Prevent class inheritance in C++(防止 C++ 中的类继承)
Why should I declare a virtual destructor for an abstract class in C++?(为什么要在 C++ 中为抽象类声明虚拟析构函数?)
Why is Default constructor called in virtual inheritance?(为什么在虚拟继承中调用默认构造函数?)
C++ cast to derived class(C++ 转换为派生类)
C++ virtual function return type(C++虚函数返回类型)
Is there any real risk to deriving from the C++ STL containers?(从 C++ STL 容器派生是否有任何真正的风险?)