简体   繁体   English

如何为某些用户界面(例如“桌面”)安装全局鼠标挂钩

[英]how to Install global mouse Hook for certain UI like “desktop”

the code so far installs a hook to detect the mouse activity but what i want is either to filter the activity for certain UI or to detect where the clicks has occurred (on which hwnd ) exactly ' Desktop ' is there a way ? 到目前为止的代码安装了一个钩子来检测鼠标活动,但是我想要的是为某些UI过滤活动检测单击发生在何处(在哪个hwnd上),确切地说是“ 桌面 ”有没有办法?

this is the code i've used it's from Microsoft website here : How to set a Windows hook in Visual C# .NET 这是我使用过的代码,来自Microsoft网站,网址为: 如何在Visual C#.NET中设置Windows钩子

EDIT : i found that he code provided by is not global so for global hook check link in the answer the answer ,, 编辑:我发现他提供的代码不是全局的,因此在答案的答案中有全局钩子检查链接,

First about the hook i found that it's not global and i found open-source global hook Here so whenever the mouse click occurs an event will fire and there i run simple callback code that checks if the desktop is the active control 首先关于钩子,我发现它不是全局的,并在这里找到了开源的全局钩子,因此,每当鼠标单击发生时,都会触发一个事件,并在此运行简单的回调代码,检查桌面是否为活动控件

[DllImport("user32.dll")]
static extern int GetForegroundWindow();

[DllImport("user32.dll")]
static extern int GetClassName(int hWnd, StringBuilder lpClassName, int nMaxCount);

public void GetActiveWindow() {
const int maxChars = 256;
int handle = 0;
StringBuilder className = new StringBuilder(maxChars);

handle = GetForegroundWindow();

if (GetClassName(handle, className, maxChars) > 0) {
    string cName = className.ToString();
    if (cName == "Progman" || cName == "WorkerW") {
        // desktop is active
    } else {
        // desktop is not active
    }
}

} }

DONE ! 完成

special thanks to Micky Duncaand and AJKenny84 特别感谢Micky Duncaand和AJKenny84

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM