0

I am trying to intercept mouse wheel by child windows under cursor. But something is wrong. It seems like message sends many times. What did I do wrong?

LRESULT CALLBACK MouseProc(__in  int     nCode,
                           __in  WPARAM  wParam,
                           __in  LPARAM  lParam)
{
    LRESULT      ret = 0;
    static  BOOL b   = TRUE;

    if (wParam == WM_MOUSEWHEEL)
    {
        if (b)
        {
            MOUSEHOOKSTRUCTEX *pMhs = (MOUSEHOOKSTRUCTEX *)lParam;
            short zDelta            = HIWORD(pMhs->mouseData);
            POINT pt;
            GetCursorPos(&pt);
            LPARAM lParam = MAKELPARAM(pt.x, pt.y);

            HWND hWnd = WindowFromPoint(pt);

            b = FALSE;
            SendMessage(hWnd, WM_MOUSEWHEEL, zDelta, lParam);
        }
        else
        {
            b = TRUE;
        }

        ret = 1;
    }
    else
    {
        CallNextHookEx(0, nCode, wParam, lParam);
    }

    return ret;
}
Ufx
  • 2,595
  • 12
  • 44
  • 83
  • Aren't you sending message yourself when processing `WM_MOUSEWHEEL` : `SendMessage(hWnd, WM_MOUSEWHEEL, zDelta, lParam);`?.. I mean, once you got first message `WM_MOUSEWHEEL` placed by Windows to your message queue, you process it and send yet a new `WM_MOUSEWHEEL` message to your own queue. – lapk Aug 28 '13 at 18:59
  • hWnd must be top child window. Here is the parent. How can I get top child window under cursor? – Ufx Aug 28 '13 at 19:07
  • You are not checking *nCode*, that's wrong. Your 'b' variable attempts to avoid the re-entrancy problem. Looks okay but its better to set it back to TRUE after the SendMessage() call. Other than that, getting a lot of messages when you scroll the mouse wheel is normal. – Hans Passant Aug 28 '13 at 19:12
  • Can I get the window which was intercepted? – Ufx Aug 28 '13 at 20:12
  • The documentation for [`MOUSEHOOKSTRUCT`](http://msdn.microsoft.com/en-us/library/windows/desktop/ms644968.aspx) specifies the `hwnd` member: *A handle to the window that will receive the mouse message corresponding to the mouse event.* As an aside, a mouse message is not necessarily sent to the topmost window underneath the cursor. For example, if the topmost child window underneath the cursor is disabled the message will be sent to its parent. – IInspectable Aug 28 '13 at 21:51

0 Answers0