Jonathan Wood
Jonathan Wood

Reputation: 67193

Hide, show, add or remove buttons from ribbon?

Has anyone figured out if it's possible to either hide, show, add or remove a button from the ribbon bar at run time? Is it possible? I'd like to have a button only show up in DEBUG builds.

Upvotes: 0

Views: 1712

Answers (3)

Marek Macho
Marek Macho

Reputation: 1

pRibbon->RecalcLayout() is not sufficient in my code (Visual C++ 2012) Correct rendering occurred only after use pRibbon->ForceRecalcLayout()

Upvotes: 0

Jonathan Wood
Jonathan Wood

Reputation: 67193

Here's what I came up with. I placed this code right after the line in InitInstance() that calls pFrame->LoadFrame(IDR_MAINFRAME, ...);.

#ifdef _DEBUG
    CMFCRibbonBar* pRibbon = pFrame->GetRibbonBar();
    CMFCRibbonCategory *pCategory = pRibbon->AddCategory(_T("DEBUG"), NULL, NULL);
    CMFCRibbonPanel *pPanel = pCategory->AddPanel(_T("DEBUG"));
    pPanel->Add(new CMFCRibbonButton(ID_DEBUG_RUN, _T("Run")));
    pRibbon->RecalcLayout();
#endif

Rather than figuring out the code to find a particular category (tab) and panel, I decided a new, dedicated category and panel was best for my purposes.

Of course, without a handler, the button will be disabled. Also, without the call to RecalcLayout(), the new category does not show up until I click on one of the tabs.

Seems to work well.

Upvotes: 0

thomiel
thomiel

Reputation: 2937

10 years ago, before ribbon resource files were introduced, adding buttons programmatically in CMainFrame::OnCreate was actually the only way, if you opted for a ribbon gui. Would have looked like this:

CMFCRibbonMainPanel* pMainPanel = m_wndRibbonBar.AddMainCategory (_T("File"), IDB_TOOLBAR_16, IDB_TOOLBAR_32);
pMainPanel->Add (new CMFCRibbonButton (ID_FILE_NEW, "&New\nStrg+N", 0, 0));
pMainPanel->Add (new CMFCRibbonButton (ID_FILE_OPEN, "&Open...\nStrg+O", 1, 1));
pMainPanel->Add (new CMFCRibbonButton (ID_FILE_SAVE, "&Save\nStrg+S", 2, 2));
pMainPanel->Add (new CMFCRibbonButton (ID_FILE_SAVE_AS, "Save &as\nStrg+U", 3, 3));
#ifdef _DEBUG
    pMainPanel->Add (new CMFCRibbonButton (ID_FILE_DEBUG_INFO, "Show &Debug Information\nStrg+D", 4, 4));
#endif

Upvotes: 2

Related Questions