Posts Tagged ‘MFC’

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.

CMap: Mapping CString to CStringArray

Monday, August 9th, 2010

Just a simple MFC trick. To map from CString to CStringArray use:

CMap<CString, LPCTSTR, CStringArrayX, CStringArrayX&>

where CStringArrayX is defined as:

class CStringArrayX : public CStringArray
{
 public:
 CStringArrayX() {}
 CStringArrayX(const CStringArrayX &qSource);
 CStringArrayX &operator = (const CStringArrayX &qSource);
};
CStringArrayX::CStringArrayX (const CStringArrayX &qSource)
{
 SetSize (qSource.GetSize());

 for (long c=qSource.GetSize()-1; c>=0; c--)
   (*this)[c]=qSource[c];
}
CStringArrayX &CStringArrayX::operator = (const CStringArrayX &qSource)
{
 SetSize (qSource.GetSize());
 for (long c=qSource.GetSize()-1; c>=0; c--)
   (*this)[c]=qSource[c];
 return (*this);
}

Hopefully this post keeps some people from searching as long as I did… 😉