到目前为止的代码安装了一个钩子来检测鼠标活动,但我想要的是过滤某些 UI 的活动或检测点击发生的位置(在哪个 hwnd 上)正是“桌面”有没有办法?
这是我使用的代码,来自微软网站:How to set a Windows hook in Visual C# .NET
编辑:我发现他提供的代码不是全局的,因此对于答案中的全局挂钩检查链接,,
到目前为止的代码安装了一个钩子来检测鼠标活动,但我想要的是过滤某些 UI 的活动或检测点击发生的位置(在哪个 hwnd 上)正是“桌面”有没有办法?
这是我使用的代码,来自微软网站:How to set a Windows hook in Visual C# .NET
编辑:我发现他提供的代码不是全局的,因此对于答案中的全局挂钩检查链接,,
首先关于钩子,我发现它不是全局的,我在这里找到了开源的全局钩子,所以每当鼠标点击发生时,就会触发一个事件,并且我运行简单的回调代码来检查桌面是否是活动控件
[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
}
}
}
完成!
特别感谢 Micky Duncaand 和 AJKenny84