Search code examples
c++labelribbonstatusbarmfc-feature-pack

How do I dynamically change the text of a CMFCRibbonLabel


My CMDIFrameWndEx derived main frame window uses a CMFCRibbonStatusBar to which I add a CMFCRibbonLabel.

I'd like to change the text of this label at runtime:

m_pLabel->SetText(description);
m_pLabel->Redraw();

It only updates the text but not the rectangle in which to draw it. So if the original text was too short, the new string won't be visible completely.

How do I get it to resize correctly?


Solution

  • Answering my own question again...

    I worked around the issue by adding and removing the label instead of trying to change the text.

    Code for adding the label:

    CMFCRibbonLabel* pLabel = new CMFCRibbonLabel(description);
    pLabel->SetID(ID_MYLABEL); // ID is 0 by default
    
    m_wndStatusBar.AddDynamicElement(pLabel);
    m_wndStatusBar.RecalcLayout();
    m_wndStatusBar.RedrawWindow();
    

    Note that I'm setting an ID so I can later call CMFCRibbonStatusBar::RemoveElement() with that ID. The calls to RecalcLayout() and RedrawWindow() are needed to make the changes visible.

    Code for removing the label:

    if(m_wndStatusBar.RemoveElement(ID_MYLABEL))
    {
        m_wndStatusBar.RecalcLayout();
        m_wndStatusBar.RedrawWindow();
    }