MFC › 5. Menus, Toolbars and Ribbon
Ribbon and the Feature Pack
Office-style ribbon UI, docking panes and visual managers.
Explanation
The MFC Feature Pack (shipped with Visual Studio 2008 SP1 and later) modernized MFC with Office-style UI. It adds CMFC... classes next to the classic ones:
| Feature | Classes |
|---|---|
| Ribbon | CMFCRibbonBar, CMFCRibbonCategory, CMFCRibbonPanel, CMFCRibbonButton, CMFCRibbonApplicationButton, CMFCRibbonStatusBar |
| Enhanced frames | CFrameWndEx, CMDIFrameWndEx, CMDIChildWndEx; the app base CWinAppEx |
| Menu bar & toolbars | CMFCMenuBar, CMFCToolBar (customizable, DPI-aware) |
| Docking panes | CDockablePane, CPaneContainerManager, auto-hide panes like Visual Studio’s Solution Explorer |
| Tabbed MDI | EnableMDITabbedGroups, CMFCTabCtrl |
| Visual managers (themes) | CMFCVisualManagerOffice2007, ...Office2003, ...VS2005, ...Windows7 |
| Controls | CMFCButton, CMFCEditBrowseCtrl (file/folder edit), CMFCColorButton, CMFCPropertyGridCtrl, CMFCListCtrl |
Ribbon structure: a CMFCRibbonBar holds categories (tabs: Home, Insert, View), each with panels (labelled groups), each with elements (buttons, checkboxes, combo boxes, galleries). Every element is wired to a command ID, so ON_COMMAND and ON_UPDATE_COMMAND_UI work as for menus.
You can build it in code, but the usual route is the Ribbon Designer in Visual Studio, which stores the layout as an XML resource (IDR_RIBBON) that the frame loads with one call: m_wndRibbonBar.LoadFromResource(IDR_RIBBON).
Ribbon projects start from the wizard: choose MFC app → Project style: Visual Studio / Office, Visual style: Office 2007 / Windows 7, “Use a ribbon”.
Example
The frame setup that wizard-generated ribbon apps use, with part of the ribbon built in code:
#include <afxwin.h>
#include <afxcontrolbars.h> // all Feature Pack classes: ribbon, docking panes, visual managers
class CMainFrame : public CFrameWndEx {
DECLARE_DYNAMIC(CMainFrame)
protected:
CMFCRibbonBar m_wndRibbonBar;
CMFCRibbonApplicationButton m_mainButton;
CMFCRibbonStatusBar m_wndStatusBar;
afx_msg int OnCreate(LPCREATESTRUCT lpcs) {
if (CFrameWndEx::OnCreate(lpcs) == -1) return -1;
// theme: Office 2007 look
CMFCVisualManager::SetDefaultManager(RUNTIME_CLASS(CMFCVisualManagerOffice2007));
CMFCVisualManagerOffice2007::SetStyle(CMFCVisualManagerOffice2007::Office2007_ObsidianBlack);
if (!m_wndRibbonBar.Create(this)) return -1;
// normally: m_wndRibbonBar.LoadFromResource(IDR_RIBBON); // layout made in the designer
// application ("File") button
m_mainButton.SetImage(IDB_MAIN);
m_mainButton.SetText(_T("\nFile"));
m_wndRibbonBar.SetApplicationButton(&m_mainButton, CSize(45, 45));
// a category (tab) with panels and buttons
CMFCRibbonCategory* pHome = m_wndRibbonBar.AddCategory(_T("&Home"), IDB_RIBBON_SMALL, IDB_RIBBON_LARGE);
CMFCRibbonPanel* pClip = pHome->AddPanel(_T("Clipboard"));
pClip->Add(new CMFCRibbonButton(ID_EDIT_PASTE, _T("Paste"), 0, 0)); // large button
pClip->Add(new CMFCRibbonButton(ID_EDIT_COPY, _T("Copy"), 1)); // small buttons
pClip->Add(new CMFCRibbonButton(ID_EDIT_CUT, _T("Cut"), 2));
CMFCRibbonPanel* pView = pHome->AddPanel(_T("View"));
pView->Add(new CMFCRibbonCheckBox(ID_VIEW_STATUS_BAR, _T("Status bar")));
// status bar
if (!m_wndStatusBar.Create(this)) return -1;
m_wndStatusBar.AddElement(new CMFCRibbonStatusBarPane(ID_STATUSBAR_PANE1, _T("Ready"), TRUE), _T("Status"));
return 0;
}
DECLARE_MESSAGE_MAP()
};
IMPLEMENT_DYNAMIC(CMainFrame, CFrameWndEx)
BEGIN_MESSAGE_MAP(CMainFrame, CFrameWndEx)
ON_WM_CREATE()
END_MESSAGE_MAP()
class CRibbonApp : public CWinAppEx {
public:
BOOL InitInstance() override {
CWinAppEx::InitInstance();
SetRegistryKey(_T("Demo"));
InitContextMenuManager(); // required by the Feature Pack menus
InitKeyboardManager();
InitTooltipManager();
CMFCToolTipInfo params;
params.m_bVislManagerTheme = TRUE;
GetTooltipManager()->SetTooltipParams(AFX_TOOLTIP_TYPE_ALL, RUNTIME_CLASS(CMFCToolTipCtrl), ¶ms);
CMainFrame* pFrame = new CMainFrame;
if (!pFrame->LoadFrame(IDR_MAINFRAME)) return FALSE;
m_pMainWnd = pFrame;
pFrame->ShowWindow(SW_SHOW);
pFrame->UpdateWindow();
return TRUE;
}
};
CRibbonApp theApp;
Common pitfalls
- Ribbon classes need the Feature Pack headers and a project created with a ribbon-capable style; mixing
CFrameWndwithCMFCRibbonBardoesn’t work. Derive fromCFrameWndEx. - Forgetting
InitContextMenuManager(),InitKeyboardManager()orInitTooltipManager()inInitInstancecauses asserts when customizing or showing tooltips. - Ribbon image resources (bitmaps for each category) must be 32-bit with the right sizes (16×16 small, 32×32 large); wrong sizes give blurry or misplaced icons.
- The look of the application is global: set the visual manager once at start-up, not per window.
- Docking panes keep state in the registry; delete or version it when you change pane layouts, or old layouts reappear.
- Licensing: Microsoft publishes usage guidelines and license terms for Office-style UI. Check the current terms before shipping a ribbon in a product.
- Strategy: MFC is mainly maintained for existing applications. For brand-new UI work, many teams choose WinUI, WPF or Qt instead; the ribbon is mostly worth learning when you maintain an MFC app that already uses it.
Legacy vs modern
Compare with classic MFC: menu bar + CToolBar + CStatusBar (Toolbars and Status Bars). The ribbon replaces menus and toolbars with one tabbed command surface. Command handling stays the same (ON_COMMAND, ON_UPDATE_COMMAND_UI), so existing handlers move over unchanged. See MFC Project Types and the Wizard.
Exercises
1. Do existing ON_COMMAND handlers need changes when you move a menu command to the ribbon?
No. A ribbon button is bound to a command ID like a menu item, so the same ON_COMMAND / ON_UPDATE_COMMAND_UI entries and the same routing apply.