使用钩子阻止鼠标消息

Blocking mouse messages with hook

如何 'block' 触发 WM_LBUTTONDOWN 消息?

该函数在 dll 中,我也尝试使用 LowLevelMouseProc 但它无法使用错误代码:1429,这意味着“仅全局挂钩”。

我尝试在下面的代码中 return 一个 WM_NULL,但它也不起作用,我还能尝试什么?

extern "C" __declspec(dllexport) LRESULT CALLBACK mouseHookProc(int nCode, WPARAM wParam, LPARAM lParam)
    {
        MSLLHOOKSTRUCT* mhs          = nullptr;
        int x = 0;
        int y = 0;
    
        std::wstringstream ss;
        std::wstringstream ss2;
    
        if (nCode == HC_ACTION)
        {
            switch (wParam)
            {
            case WM_LBUTTONDOWN:
                ss << L"\nWM_LBUTTONDOWN " << wParam;
                wParam = WM_NULL;
                break;
            case WM_LBUTTONUP:
                ss << L"\nWM_LBUTTONUP " << wParam;
                break;
            case WM_MOUSEMOVE:
                break;
            case WM_RBUTTONDOWN:
                ss << L"\nWM_RBUTTONDOWN: " << wParam;
                break;
            case WM_RBUTTONUP:
                ss << L"\nWM_RBUTTONUP: " << wParam;
                break;
            default:
                ss << L"\nUnknown msg: " << wParam;
                break;
            }
        }
    
        OutputDebugString(ss.str().c_str());
    
        mhs = reinterpret_cast<MSLLHOOKSTRUCT*>(lParam);
        x = mhs->pt.x;
        y = mhs->pt.y;
    
        ss2 <<  L"\nx: " << x << L" y: " << y;
        OutputDebugString(ss2.str().c_str());
    
        return CallNextHookEx(NULL, nCode, wParam, lParam);

根据 MouseProc callback function 文档:

If nCode is greater than or equal to zero, and the hook procedure did not process the message, it is highly recommended that you call CallNextHookEx and return the value it returns; otherwise, other applications that have installed WH_MOUSE hooks will not receive hook notifications and may behave incorrectly as a result. If the hook procedure processed the message, it may return a nonzero value to prevent the system from passing the message to the target window procedure.

至于您的 WH_MOUSE_LL 错误,您不能在特定于线程的基础上安装该挂钩,只能全局安装。