Tip: Painting the CToolbar’s Parent Window (AfxControlBar)

Using the Code

In your application, override OnNotify member function in your CMainFrame to handle the WM_NOTIFY message for painting the AfxControlBar.

In MainFrm.h file, declare the following member variable and member function:

class CMainFrame : public CMDIFrameWnd
{
   .....
   CBrush m_BrushDocBar;

   BOOL OnNotify(WPARAM wParam, LPARAM lParam, LRESULT* pResult);
   .....
}

In MainFrm.cpp, in constructor create a solid brush as follows:

CMainFrame::CMainFrame()
{
   m_BrushDocBar.CreateSolidBrush(RGB(0, 255, 255));
}

In MainFrm.cpp, provide the definition of OnNotify() function as follows:

BOOL CMainFrame::OnNotify(WPARAM wParam,
                          LPARAM lParam,
                          LRESULT* pResult)
{
   LPNMHDR pnmh = (LPNMHDR) lParam;
   if(pnmh->hwndFrom == m_wndToolBar.m_hWnd)
   {
      LPNMTBCUSTOMDRAW lpNMCustomDraw = (LPNMTBCUSTOMDRAW) lParam;
      CRect rect;
      CWnd* pWnd = m_wndToolBar.GetParent();
      TCHAR szClassName[200];
      GetClassName(pWnd->m_hWnd, szClassName, 200);
      CString strTemp = szClassName;
      if(strTemp.Find(_T("AfxControlBar")) >= 0)
      {
         SetClassLong(pWnd->m_hWnd,
                      GCL_HBRBACKGROUND,
                      (LONG)m_BrushDocBar.GetSafeHandle());
      }
   }
   return CMDIFrameWnd::OnNotify(wParam, lParam, pResult);
}

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read