CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Dec 2008
    Posts
    28

    Change MenuBar & ToolBar background color

    Hi, I wanted to ask is there any other ways to change the background color for MenuBar(whole line of menu bar, including menu item) and also Toolbar background to other colour rather than following window's color scheme ??

    I've search for few days, the solution i found is either :
    1. Define our own menu [refer to below's link which teaches us to do OwnerDrawMenu

    http://www.codeguru.com/Cpp/controls...cle.php/c3721/

    2. Change the window's settings [This is not really a good approach because it will force all other application to have the same colour]


    Is there anyone of you able to find other solution rather than these 2 i stated??

    Another question, if you tried to go for [1] solution, on that demo, it will only have the background change for menu items ONLY. For the right side of the MenuBar, the colour will keep on the default [which is gray]. Is there any ways to make the whole MenuBar[whole line] to have the colour that i preferred?

    Any help would be appreciated..

  2. #2
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: Change MenuBar & ToolBar background color

    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  3. #3
    Join Date
    Dec 2008
    Posts
    28

    Re: Change MenuBar & ToolBar background color

    I want to change the background color as below attachment "printscreen.JPG"

    Thanks for your reply ^^
    Attached Images Attached Images  

  4. #4
    Join Date
    Mar 2005
    Posts
    26

    Re: Change MenuBar & ToolBar background color

    You also should create a custom toolbar class (such as you have done for the menu).

    Regrading the background of menu, you try this code and add them into the OnCreate of CMainFrame:
    Code:
      MENUINFO MenuInfo = {0};
      MenuInfo.cbSize = sizeof(MenuInfo);
      MenuInfo.hbrBack = *pBrush; // Brush you want to draw
      MenuInfo.fMask = MIM_BACKGROUND;
    
      if(IsMenu(hMenu))
      {
        SetMenuInfo(hMenu, &MenuInfo);
      }

  5. #5
    Join Date
    Dec 2008
    Posts
    28

    Re: Change MenuBar & ToolBar background color

    THANKS ! truongptr

    For the MenuBar, it works !!
    Thanks a lot !
    ^^

    As for the Toolbar, I'm trying to load a bitmap inside for the color i want.
    As for the MenuItem, i'm trying to set the background color of it..
    Still searching how to do it

  6. #6
    Join Date
    Dec 2008
    Posts
    28

    Re: Change MenuBar & ToolBar background color

    To change the background color of MenuBar, simply add below's line of code, beginning of mainframe OnCreate

    CBrush* NewBrush;
    NewBrush = new CBrush;
    NewBrush->CreateSolidBrush(RGB(139,137,137));

    MENUINFO MenuInfo = {0};
    MenuInfo.cbSize = sizeof(MenuInfo);
    MenuInfo.hbrBack = *NewBrush; // Brush you want to draw
    MenuInfo.fMask = MIM_BACKGROUND;
    MenuInfo.dwStyle = MNS_AUTODISMISS;

    CMenu* pMenu = this->GetMenu();

    if(IsMenu(pMenu->m_hMenu))
    {
    SetMenuInfo(pMenu->m_hMenu, &MenuInfo);
    }

    As for Toolbar's background color, create OnNotify at MainFrame, put below's code

    LPNMHDR pnmh = (LPNMHDR) lParam;
    if(pnmh->hwndFrom == m_wndToolBar.m_hWnd)
    {
    LPNMTBCUSTOMDRAW lpNMCustomDraw = (LPNMTBCUSTOMDRAW) lParam;
    CRect rect;
    m_wndToolBar.GetClientRect(rect);
    FillRect(lpNMCustomDraw->nmcd.hdc, rect, (HBRUSH)GetStockObject(GRAY_BRUSH));

    }
    return CFrameWnd::OnNotify(wParam, lParam, pResult);



    Thanks for those who helped me. I'm giving out the solution i've got.

    Special Thanks to truongptr

  7. #7
    Join Date
    Aug 2009
    Posts
    5

    Re: Change MenuBar & ToolBar background color

    thanks a lot!
    I've just resolved my problem by above approach!

  8. #8
    Join Date
    Nov 2021
    Posts
    2

    Re: Change MenuBar & ToolBar background color

    Custom draw worked for me.
    Here is my code.

    m_toolbar1.CreateEx(this, TBSTYLE_FLAT | TBSTYLE_CUSTOMERASE, WS_CHILD | WS_VISIBLE | CBRS_ALIGN_TOP, CRect(10,10,100,100), AFX_IDW_TOOLBAR);
    //m_toolbar1.CreateEx(this);
    m_toolbar1.LoadToolBar(IDR_TOOLBAR1);

    BOOL CMyMFCDialogSimpleDlg::OnNotify(WPARAM wParam, LPARAM lParam, LRESULT* pResult)//to make toolbar non transparent
    {
    NMHDR* hdr = (NMHDR*)lParam;
    //static HBRUSH hbr = CreateHatchBrush(HS_BDIAGONAL, RGB(64,134,247));
    //static HBRUSH hbr = CreateHatchBrush(HS_DIAGCROSS, RGB(64,134,247));
    static HBRUSH hbr = CreateHatchBrush(HS_DIAGCROSS, RGB(242, 99, 142));

    if (hdr->code == NM_CUSTOMDRAW)
    {
    if (hdr->hwndFrom == m_toolbar1.GetSafeHwnd())
    {
    *pResult = CDRF_DODEFAULT;

    NMCUSTOMDRAW* cdi = (NMCUSTOMDRAW*)lParam;
    if (cdi->dwDrawStage == CDDS_PREPAINT)
    {
    *pResult = CDRF_NOTIFYITEMDRAW | CDRF_NOTIFYPOSTERASE | CDRF_NOTIFYPOSTERASE | CDRF_NOTIFYPOSTPAINT;
    return TRUE;
    }
    else if (cdi->dwDrawStage == CDDS_PREERASE)
    {
    *pResult = CDRF_NOTIFYITEMDRAW | CDRF_NOTIFYPOSTERASE | CDRF_NOTIFYPOSTERASE | CDRF_NOTIFYPOSTPAINT;
    return TRUE;
    }
    else if (cdi->dwDrawStage == CDDS_ITEMPREPAINT)
    {
    *pResult = CDRF_NOTIFYITEMDRAW | CDRF_NOTIFYPOSTERASE | CDRF_NOTIFYPOSTERASE | CDRF_NOTIFYPOSTPAINT;
    return TRUE;
    }
    else if (cdi->dwDrawStage == CDDS_POSTERASE)
    {
    *pResult = CDRF_NOTIFYITEMDRAW | CDRF_NOTIFYPOSTERASE | CDRF_NOTIFYPOSTERASE | CDRF_NOTIFYPOSTPAINT;
    CRect rc = {};
    m_toolbar1.GetClientRect(&rc);
    //FillRect(cdi->hdc, &rc, (HBRUSH)GetStockObject(GRAY_BRUSH));
    FillRect(cdi->hdc, &rc, hbr);
    return TRUE;
    }
    else if (cdi->dwDrawStage == CDDS_ITEMPOSTERASE)
    {
    *pResult = CDRF_NOTIFYITEMDRAW | CDRF_NOTIFYPOSTERASE | CDRF_NOTIFYPOSTERASE | CDRF_NOTIFYPOSTPAINT;
    return TRUE;
    }
    else if (cdi->dwDrawStage == CDDS_POSTPAINT)
    {
    *pResult = CDRF_DODEFAULT;
    return TRUE;
    }

    }
    }
    return 0;// CDialogEx::OnNotify(wParam, lParam, pResult);
    }

  9. #9
    Join Date
    Nov 2021
    Posts
    2

    Re: Change MenuBar & ToolBar background color

    I was having hard time getting CDDS_POSTERASE to fire, neither was I getting CDDS_POSTPAINT.
    I realized that to get CDDS_POSTPAINT you have to specify CDRF_NOTIFYPOSTPAINT in CDDS_PREPAINT stage, and for CDDS_POSTERASE you must handle CDDS_PREERASE first, and then specify CDRF_NOTIFYPOSTERASE

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured