none
CMFCRibbonStatusBar background color RRS feed

  • Question

  • I have a MDI project with a nice "status-bar" displayed below the document(s). 

    My question: how do a dynamically change the "background-color" for the status-bar? More specifically for "Element-0" (left-hand-portion)?

    I have the status-bar:
    m_wndStatusBar.Create(this)
    But it doesn't seem to have any "background-color" method. 

    And I can get a pointer to "Element-0":
    m_wndStatusBar.GetElement(0)
    But again, it doesn't seem to have any way of setting the "background-color". 

    How can I do this?

    Monday, September 18, 2017 6:33 PM

Answers

  • I have come up with a solution that will allow me to statically assign color the the CMFCRibbonStatusBar via the CMFCRibbonStatusBarPane.

    The CMFCRibbonStatusBarPane needs to be sub-classed like the following. The color/text is setup in the OnDraw() function. I have it as MyPane.h:

    #pragma once
    #include <afxribbonstatusbarpane.h>
    
    class MyPane : public CMFCRibbonStatusBarPane
    {
    public:
        MyPane() {};
        MyPane(
            UINT   nCmdID,
            LPCTSTR lpszText,
            BOOL   bIsStatic = FALSE,
            HICON  hIcon = NULL,
            LPCTSTR lpszAlmostLargeText = NULL)
            :CMFCRibbonStatusBarPane(nCmdID,
                lpszText, bIsStatic, hIcon, lpszAlmostLargeText) {}
        MyPane(
            UINT   nCmdID,
            LPCTSTR lpszText,
            HBITMAP hBmpAnimationList,
            int   cxAnimation = 16,
            COLORREF clrTrnsp = RGB(192, 192, 192),
            HICON  hIcon = NULL,
            BOOL   bIsStatic = FALSE)
            :CMFCRibbonStatusBarPane(nCmdID, lpszText,
                hBmpAnimationList, cxAnimation, clrTrnsp, hIcon, bIsStatic) {}
    
        MyPane(
            UINT   nCmdID,
            LPCTSTR lpszText,
            UINT   uiAnimationListResID,
            int   cxAnimation = 16,
            COLORREF clrTrnsp = RGB(192, 192, 192),
            HICON  hIcon = NULL,
            BOOL   bIsStatic = FALSE)
            :CMFCRibbonStatusBarPane(nCmdID, lpszText,
                uiAnimationListResID, cxAnimation, clrTrnsp, hIcon, bIsStatic) {}
    
    
    
        ~MyPane() {};
    
        virtual COLORREF OnFillBackground(CDC* pDC)override
        {
            return RGB(255, 0, 0); //return whatever new color you want
        }
    
        virtual void OnDraw(CDC* pDC)override
        {
            // this is the background color
            pDC->FillSolidRect(0,0,5000,1000, RGB(201, 255, 170));
    
            // text label
            CFont font;
            font.CreatePointFont(120, L"Arial Black");
            CFont *pFont = pDC->SelectObject(&font);
            COLORREF color = RGB(0, 0, 0);
            pDC->SetTextColor(color);
            pDC->TextOut(20, 0, L"THIS IS A TEST", 14);
            pDC->SelectObject(pFont);
            font.DeleteObject();
        }
    
    };
    

    And then in the Main-Form initialization do this:

    if (!m_wndStatusBar.Create(this))
    	{
    		TRACE0("Failed to create status bar\n");
    		return -1;      // fail to create
    	}
    
    
    	m_wndStatusBar.AddElement(new MyPane(ID_STATUSBAR_PANE1, strTitlePane1, TRUE), strTitlePane1);


    Hopefully this will help someone else.

    • Marked as answer by user93949340 Wednesday, September 20, 2017 4:44 PM
    Wednesday, September 20, 2017 4:44 PM

All replies

  • FYI: if it makes any difference, I am using Visual Studio 2017.

    Tuesday, September 19, 2017 1:20 PM
  • I have come up with a solution that will allow me to statically assign color the the CMFCRibbonStatusBar via the CMFCRibbonStatusBarPane.

    The CMFCRibbonStatusBarPane needs to be sub-classed like the following. The color/text is setup in the OnDraw() function. I have it as MyPane.h:

    #pragma once
    #include <afxribbonstatusbarpane.h>
    
    class MyPane : public CMFCRibbonStatusBarPane
    {
    public:
        MyPane() {};
        MyPane(
            UINT   nCmdID,
            LPCTSTR lpszText,
            BOOL   bIsStatic = FALSE,
            HICON  hIcon = NULL,
            LPCTSTR lpszAlmostLargeText = NULL)
            :CMFCRibbonStatusBarPane(nCmdID,
                lpszText, bIsStatic, hIcon, lpszAlmostLargeText) {}
        MyPane(
            UINT   nCmdID,
            LPCTSTR lpszText,
            HBITMAP hBmpAnimationList,
            int   cxAnimation = 16,
            COLORREF clrTrnsp = RGB(192, 192, 192),
            HICON  hIcon = NULL,
            BOOL   bIsStatic = FALSE)
            :CMFCRibbonStatusBarPane(nCmdID, lpszText,
                hBmpAnimationList, cxAnimation, clrTrnsp, hIcon, bIsStatic) {}
    
        MyPane(
            UINT   nCmdID,
            LPCTSTR lpszText,
            UINT   uiAnimationListResID,
            int   cxAnimation = 16,
            COLORREF clrTrnsp = RGB(192, 192, 192),
            HICON  hIcon = NULL,
            BOOL   bIsStatic = FALSE)
            :CMFCRibbonStatusBarPane(nCmdID, lpszText,
                uiAnimationListResID, cxAnimation, clrTrnsp, hIcon, bIsStatic) {}
    
    
    
        ~MyPane() {};
    
        virtual COLORREF OnFillBackground(CDC* pDC)override
        {
            return RGB(255, 0, 0); //return whatever new color you want
        }
    
        virtual void OnDraw(CDC* pDC)override
        {
            // this is the background color
            pDC->FillSolidRect(0,0,5000,1000, RGB(201, 255, 170));
    
            // text label
            CFont font;
            font.CreatePointFont(120, L"Arial Black");
            CFont *pFont = pDC->SelectObject(&font);
            COLORREF color = RGB(0, 0, 0);
            pDC->SetTextColor(color);
            pDC->TextOut(20, 0, L"THIS IS A TEST", 14);
            pDC->SelectObject(pFont);
            font.DeleteObject();
        }
    
    };
    

    And then in the Main-Form initialization do this:

    if (!m_wndStatusBar.Create(this))
    	{
    		TRACE0("Failed to create status bar\n");
    		return -1;      // fail to create
    	}
    
    
    	m_wndStatusBar.AddElement(new MyPane(ID_STATUSBAR_PANE1, strTitlePane1, TRUE), strTitlePane1);


    Hopefully this will help someone else.

    • Marked as answer by user93949340 Wednesday, September 20, 2017 4:44 PM
    Wednesday, September 20, 2017 4:44 PM