C++ 为 WH_MOUSE 设置 WindowsHookEx

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11180773/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 14:55:19  来源:igfitidea点击:

SetWindowsHookEx for WH_MOUSE

c++mousehook

提问by tobi

I've got some code into my hands that prints coordinates of the mouse globally (using WH_MOUSE_LL). My target is to use WH_MOUSE instead of WH_MOUSE_LL because (from what I've read) it is faster. I've read over the forum that when using WH_MOUSE it needs to be declared in DLL to achieve global effect, but still, when used in the program it should work over that application where it was declared, but it doesn't work (it prints nothing) when I just change the WH_MOUSE_LL to WH_MOUSE. This is the code:

我有一些代码可以全局打印鼠标的坐标(使用 WH_MOUSE_LL)。我的目标是使用 WH_MOUSE 而不是 WH_MOUSE_LL 因为(根据我的阅读)它更快。我在论坛上读到,当使用 WH_MOUSE 时,它需要在 DLL 中声明以实现全局效果,但是,当在程序中使用时,它应该在声明它的应用程序上工作,但它不起作用(它不打印任何内容)当我将 WH_MOUSE_LL 更改为 WH_MOUSE 时。这是代码:

#define _WIN32_WINNT 0x0400
#pragma comment( lib, "user32.lib" )

#include <windows.h>
#include <stdio.h>

HHOOK hMouseHook;

LRESULT CALLBACK mouseProc (int nCode, WPARAM wParam, LPARAM lParam)
{
    MOUSEHOOKSTRUCT * pMouseStruct = (MOUSEHOOKSTRUCT *)lParam;
    if (pMouseStruct != NULL){
        if(wParam == WM_LBUTTONDOWN)
        {
            printf( "clicked" ); 
        }
        printf("Mouse position X = %d  Mouse Position Y = %d\n", pMouseStruct->pt.x,pMouseStruct->pt.y);
    }
    return CallNextHookEx(hMouseHook, nCode, wParam, lParam);
}

DWORD WINAPI MyMouseLogger(LPVOID lpParm)
{
    HINSTANCE hInstance = GetModuleHandle(NULL);

    // here I put WH_MOUSE instead of WH_MOUSE_LL
    hMouseHook = SetWindowsHookEx( WH_MOUSE_LL, mouseProc, hInstance, NULL );

    MSG message;
    while (GetMessage(&message,NULL,0,0)) {
        TranslateMessage( &message );
        DispatchMessage( &message );
    }

    UnhookWindowsHookEx(hMouseHook);
    return 0;
}

int main(int argc, char** argv)
{
    HANDLE hThread;
    DWORD dwThread;

    hThread = CreateThread(NULL,NULL,(LPTHREAD_START_ROUTINE)MyMouseLogger, (LPVOID) argv[0], NULL, &dwThread);
    if (hThread)
        return WaitForSingleObject(hThread,INFINITE);
    else
        return 1;

}

回答by Yuhong Bao

// here I put WH_MOUSE instead of WH_MOUSE_LL
hMouseHook = SetWindowsHookEx( WH_MOUSE_LL, mouseProc, hInstance, NULL );

Fourth param must also be changed to GetCurrentThreadId() to make it local.

第四个参数也必须更改为 GetCurrentThreadId() 以使其成为本地。

回答by rogerdpack

since you have a "main" in there, my guess is that you'd need to make it into a dll for it to work for messages other than the *_LL type

因为你在那里有一个“主要”,我的猜测是你需要把它变成一个 dll 以便它可以处理 *_LL 类型以外的消息

Understanding the low-level mouse and keyboard hook (win32)

了解低级键鼠钩子(win32)

http://developer-resource.blogspot.com/2008/07/setwindowshookex-example.htmlhas a dll example

http://developer-resource.blogspot.com/2008/07/setwindowshookex-example.html有一个 dll 示例