Posts Tagged ‘OnMouseWheel’

MFC: Redirecting OnMouseWheel messages to the window under cursor

Wednesday, June 6th, 2012

By default MFC applies the MouseWheel message to the window that has the focus. Even though most users expect it to apply to the window under the cursor. (And even Microsoft’s style guide recommends that.)

To fix this, override the program’s PreTranslateMessage  function and add this:

 if (pMsg->message==WM_MOUSEWHEEL)
 {
    CPoint point;
    GetCursorPos (&point);

    HWND hWnd = ::WindowFromPoint (point);

    if (hWnd && pMsg->hwnd!=hWnd)
    {
       ::PostMessage(hWnd, pMsg->message, pMsg->wParam, pMsg->lParam);
       return(true);
    }
 }

Please note that you have to add this  before calling the default implementation of PreTranslateMessage.