-1

I have a code to display a part of screen when mouse moves. But the WH_MOUSE doesn't work. I need to change GetModuleHandle(0), 0 to hInst, GetCurrentThreadId().

But then the application will work only when the mouse is over the application itself.

I want it global and I tried WH_MOUSE_LL, it is slower then WH_MOUSE.

Is that possible to use WH_MOUSE globally without DLL?

void SetHook()
{
    gMouseHook = SetWindowsHookEx(WH_MOUSE, MouseProc, GetModuleHandle(0), 0);
}   

//================================================================================
// Mouse Hook

static LRESULT CALLBACK MouseProc(int nCode, WPARAM wParam, LPARAM lParam)
{
    if (nCode < 0) {
    return CallNextHookEx(gMouseHook, nCode, wParam, lParam);
    }

    if (wParam == WM_MOUSEMOVE) {

    MOUSEHOOKSTRUCT *mouseInfo = (MOUSEHOOKSTRUCT*)lParam;

    int x = mouseInfo->pt.x;
    int y = mouseInfo->pt.y;

    PrintScreen(x, y);
    }

    return CallNextHookEx(gMouseHook, nCode, wParam, lParam);
}
user565739
  • 1,302
  • 4
  • 23
  • 46

1 Answers1

2

Is that possible to use WH_MOUSE globally without DLL?

No, the hook procedure needs to be in a DLL so that it can be injected into other processes.

I tried WH_MOUSE_LL, it is slower then WH_MOUSE.

That probably means your hook procedure is slow.

casablanca
  • 69,683
  • 7
  • 133
  • 150
  • Yes, because it GetDIBits to capture the screen each time the mouse is moved and print it to my window. Do you know another way to do such task efficiently? – user565739 Sep 03 '12 at 22:22
  • 5
    @user565739: You shouldn't do that directly in your hook procedure. You could set a flag in the hook procedure, then have a timer in your window that checks for the flag and captures the screen. In fact, if you use a timer, you may not even need a hook -- you can simply check if the mouse has moved from the previous position. – casablanca Sep 03 '12 at 22:25