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.
-
That might differ how you create the ribbon... Are you using a GUI framework? I personally think it's a bad idea to use a ribbon at all anyway, needs more space than classic menus and requires more clicking around (in general)... – Aconcagua Oct 03 '19 at 19:58
-
Yes, I'm just using the default MFC ribbon. Microsoft did a lot of research on the ribbon bar and decided it had a lot of advantages. I tend to agree, and I could go into a lot of reasons why. Of course, you still have the option to create your own apps using whatever kind of menus you want. – Jonathan Wood Oct 03 '19 at 20:37
-
You need to consider as well that Microsoft always is in need for new features, and sure they do promote it. Microsoft's opinion is not fully neutral about the matter. Even after 10 years, ribbon is still controverse. So actually, best you could do would be offering *both*, so anyone could chose according to her/his personal preference... – Aconcagua Oct 04 '19 at 00:08
-
@Aconcagua: Well, I've been designing user interfaces for over 30 years. I'm comfortable in my choice of interface. I just wish there was a straight forward way to hide or show ribbon buttons. – Jonathan Wood Oct 04 '19 at 00:49
-
And there *will* be users being unhappy about the decision (unless you write it only for yourself). Actually, I'm not that much opposing the ribbon per se (albeit considering it a bad concept), but much more being left without a choice. I understand pretty much, though, if you don't want to invest the effort to implement both parallelly. The real pity is that MS missed to implement the items such that they could appear as both menu or ribbon, two optical varieties of one and the same thing. Discussion gone, anybody would use whatever she/he wanted... – Aconcagua Oct 04 '19 at 09:03
3 Answers
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

- 2,467
- 22
- 37
-
If you use Visual Studio 2008, this will work for sure. I just didn't try this with a recent version of Visual Studio but you should be able to get the CMFCRibbonMainPanel of your application without problems, then get the right panel from it and ``Add`` the Button programmatically as shown in the code snippet. – thomiel Oct 05 '19 at 16:49
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.

- 65,341
- 71
- 269
- 466
pRibbon->RecalcLayout()
is not sufficient in my code (Visual C++ 2012)
Correct rendering occurred only after use pRibbon->ForceRecalcLayout()

- 1
- 1