Click here to Skip to main content
15,892,746 members
Articles / Desktop Programming / MFC
Article

Painting of CToolbar's parent window (AfxControlBar).

Rate me:
Please Sign up or sign in to vote.
2.38/5 (11 votes)
27 Mar 2008CPOL 50.7K   913   11   10
Background painting of CToolbar's parent window (AfxControlBar).

Introduction

I was trying to paint the background color (paint entire window) of CToolbar. In that, I could paint only the toolbar window and not the entire area of the toolbar window which is "AfxControlBar" (CToolbar's parent window). Then I found the solution for painting the "AfxControBar", and I thought I should share the solution with you all.

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:

C++
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,

C++
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);
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questionit didn't work when I rebuild the project in my computer with vs2008 Pin
shangcangriluo20-Aug-13 16:23
shangcangriluo20-Aug-13 16:23 
AnswerRe: it didn't work when I rebuild the project in my computer with vs2008 Pin
Paresh Chitte18-Sep-13 1:17
Paresh Chitte18-Sep-13 1:17 
Questionhow to limited drawing area?? Pin
chenyuntian5-Sep-09 5:15
chenyuntian5-Sep-09 5:15 
Generalpainting Pin
Shorzy5-Aug-09 4:22
Shorzy5-Aug-09 4:22 
GeneralRe: painting Pin
Paresh Chitte5-Aug-09 18:44
Paresh Chitte5-Aug-09 18:44 
GeneralThanks a lot! Pin
LoveEmpty12-Aug-08 16:45
LoveEmpty12-Aug-08 16:45 
GeneralRe: Thanks a lot! Pin
Paresh Chitte12-Aug-08 18:32
Paresh Chitte12-Aug-08 18:32 
Hi,

Thank you very much for the reply.

If possible, could you please rate the article ?

Regards,
Paresh.
GeneralCorrect way to accomplish this Pin
adzm10-Apr-08 15:03
adzm10-Apr-08 15:03 
GeneralRe: Correct way to accomplish this Pin
Paresh Chitte10-Apr-08 18:23
Paresh Chitte10-Apr-08 18:23 
GeneralSometimes you bring tears to my eyes Pin
Jim Crafton28-Mar-08 4:00
Jim Crafton28-Mar-08 4:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.