WinAPI - 钩子鼠标移动不起作用

时间:2014-06-07 17:52:31

标签: winapi visual-c++

我正在尝试挂钩所有鼠标移动事件,但我从未收到任何消息。这是完整的代码:

#include <Windows.h>
#include <windowsx.h>
#include <iostream>

LRESULT CALLBACK MouseHookProc(int nCode, WPARAM wParam, LPARAM lParam)
{
    if (wParam != WM_MOUSEMOVE)
    {
        return CallNextHookEx(NULL, nCode, wParam, lParam);
    }

    std::cout << GET_X_LPARAM(lParam) << std::endl;
    return 0;
}

LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch (uMsg)
    {
    case WM_DESTROY:
        PostQuitMessage(0);
        return 0;

    case WM_MOUSEMOVE:
        int xPos = GET_X_LPARAM(lParam);
        int yPos = GET_Y_LPARAM(lParam);
        std::cout << xPos << " - " << yPos << std::endl;
        return 0;
    }

    return DefWindowProc(hwnd, uMsg, wParam, lParam);
}

int __stdcall WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR lpCmdLine, INT nCmdShow)
{
    AllocConsole();

    HHOOK mouseHook = SetWindowsHookEx(WH_MOUSE_LL, MouseHookProc, hInstance, NULL);

    MSG msg;

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

    return 0;
}

3 个答案:

答案 0 :(得分:3)

   std::cout << GET_X_LPARAM(lParam) << std::endl;

钩子工作正常,你只是高估CRT在你没有控制台的情况下正确初始化stdout的能力。钩子回调代码通常不正确。你会照亮它:

LRESULT CALLBACK MouseHookProc(int nCode, WPARAM wParam, LPARAM lParam) {
    if (nCode >= 0 && wParam == WM_MOUSEMOVE) {
        MSLLHOOKSTRUCT* mh = (MSLLHOOKSTRUCT*)lParam;
        std::stringstream ss;
        ss << mh->pt.x << ", " << mh->pt.y << std::endl;
        OutputDebugStringA(ss.str().c_str());
    }
    return CallNextHookEx(NULL, nCode, wParam, lParam);
}

您将看到调试器中出现文本,对于Visual Studio,它将显示在“输出”窗口中。

另一种方法是简单地构建一个控制台模式应用程序,以便立即获得控制台,并且stdout将起作用。将WinMain()更改为main()。

或者通过重新打开标准输出,可能是你喜欢的。像这样:

  AllocConsole();
  freopen("CONOUT$", "wb", stdout);

答案 1 :(得分:1)

如果您尝试捕获所有进程的鼠标事件(您的NULL最后一个参数暗示了这一点),则需要在DLL中使用系统范围的挂钩。

我的网站上有一个例子here

答案 2 :(得分:0)

是的,就像@HansPassant所提到的那样std::cout不是一种有效的展示方式,除非你每次更新时都让你的控制台刷新。

我已经重新制作了整个程序,因为我也需要使用代码:阻止默认的win32模板并替换MouseHookProc:)

#include <windows.h>
#include <windowsx.h>
#include <iostream>
#include <sstream>

LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK MouseHookProc(int nCode, WPARAM wParam, LPARAM lParam);

char szClassName[ ] = "CodeBlocksWindowsApp";
HWND Label1, Label2; //adding 2 static controls to display things

int WINAPI WinMain (HINSTANCE hThisInstance,
                 HINSTANCE hPrevInstance,
                 LPSTR lpszArgument,
                 int nCmdShow)
{
// adding hook
HHOOK mouseHook = SetWindowsHookEx(WH_MOUSE_LL, MouseHookProc, hThisInstance, NULL);

HWND hwnd;
MSG messages;
WNDCLASSEX wincl;
wincl.hInstance = hThisInstance;
wincl.lpszClassName = szClassName;
wincl.lpfnWndProc = WindowProcedure;
wincl.style = CS_DBLCLKS;
wincl.cbSize = sizeof (WNDCLASSEX);
wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
wincl.lpszMenuName = NULL;
wincl.cbClsExtra = 0;
wincl.cbWndExtra = 0;
wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;

if (!RegisterClassEx (&wincl))
    return 0;

hwnd = CreateWindowEx (0, szClassName, "Code::Blocks Template Windows App", 
       WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 544, 375, 
       HWND_DESKTOP, NULL, hThisInstance, NULL);

ShowWindow (hwnd, nCmdShow);

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

return messages.wParam;
}


LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
    case WM_CREATE:
        //creating those 2 static controls
        Label1 = CreateWindow("STATIC", "Label1", WS_VISIBLE | WS_CHILD, 10,10,60,18, hwnd, NULL, NULL, NULL);
        Label2 = CreateWindow("STATIC", "Label2", WS_VISIBLE | WS_CHILD, 70,10,60,18, hwnd, NULL, NULL, NULL);
    break;
    //adding mousemove event
    case WM_MOUSEMOVE: {
        int xPos = GET_X_LPARAM(lParam);
        int yPos = GET_Y_LPARAM(lParam);
        //std::cout << xPos << " - " << yPos << std::endl;
        TCHAR Coords[20];
        wsprintf(Coords, "%i, %i", xPos, yPos);
        SetWindowText(Label1, Coords);
    return 0; }

    case WM_DESTROY:
        PostQuitMessage (0);
        break;
    default:
        return DefWindowProc (hwnd, message, wParam, lParam);
}
return 0;
}

//replacing MouseHookProc
LRESULT CALLBACK MouseHookProc(int nCode, WPARAM wParam, LPARAM lParam) {
    if (nCode >= 0 && wParam == WM_MOUSEMOVE) {
        MSLLHOOKSTRUCT* mh = (MSLLHOOKSTRUCT*)lParam;
        std::stringstream ss;
        ss << mh->pt.x << ", " << mh->pt.y << std::endl;
        //OutputDebugStringA(ss.str().c_str());
        SetWindowText(Label2, ss.str().c_str());
    }
    return CallNextHookEx(NULL, nCode, wParam, lParam);
}
相关问题