MFC › 5. Menus, Toolbars and Ribbon
Toolbars and Status Bars
Command bars and dynamic status panes.
beginner #mfc#ctoolbar#cstatusbar#docking#indicators#setpanetext#controlbar
Explanation
Control bars are the strips around the client area: CToolBar, CStatusBar, CDialogBar. The frame creates them in OnCreate and MFC’s layout code positions them and shrinks the client area (RecalcLayout).
Toolbar:
- A toolbar resource (
IDR_MAINFRAME) is a bitmap strip of button images plus the list of command IDs. In the resource editor you draw the buttons and assign the IDs, usually the same IDs as menu items. m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC)thenLoadToolBar(IDR_MAINFRAME).- A button with the same ID as a menu item shares its enable/check state automatically (from
ON_UPDATE_COMMAND_UI) and gets a tooltip from the string resource text after the\n. - Docking:
EnableDocking(CBRS_ALIGN_ANY)on the toolbar and on the frame, thenDockControlBar(&m_wndToolBar). - Show/hide:
ShowControlBar(&m_wndToolBar, bShow, FALSE); the View → Toolbar commandID_VIEW_TOOLBARis handled byCFrameWnditself.
Status bar:
- Divided into panes declared with an indicators array of IDs.
ID_SEPARATORis a plain text pane;ID_INDICATOR_CAPS,ID_INDICATOR_NUM,ID_INDICATOR_SCRLshow key states. - The first pane shows the prompt text of a hovered menu item (the string resource) by default.
- Write to a pane:
m_wndStatusBar.SetPaneText(index, text); change width/style withSetPaneInfo. - A custom indicator (a clock, a cursor position) needs a string resource ID and an
ON_UPDATE_COMMAND_UI(ID_INDICATOR_TIME, handler)callingpCmdUI->Enable()andSetText().
Example
#include <afxwin.h>
#include <afxext.h>
static UINT indicators[] = {
ID_SEPARATOR, // pane 0: message line
ID_INDICATOR_CURSORPOS, // pane 1: custom (needs a string resource for its width)
ID_INDICATOR_CAPS,
ID_INDICATOR_NUM
};
class CMainFrame : public CFrameWnd {
DECLARE_DYNCREATE(CMainFrame)
protected:
CToolBar m_wndToolBar;
CStatusBar m_wndStatusBar;
CPoint m_cursor;
afx_msg int OnCreate(LPCREATESTRUCT lpcs) {
if (CFrameWnd::OnCreate(lpcs) == -1) return -1;
// ---- toolbar
if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT,
WS_CHILD | WS_VISIBLE | CBRS_TOP | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC)
|| !m_wndToolBar.LoadToolBar(IDR_MAINFRAME)) {
TRACE0("Failed to create toolbar\n");
return -1; // abort window creation
}
m_wndToolBar.SetWindowText(_T("Standard"));
// ---- status bar
if (!m_wndStatusBar.Create(this) ||
!m_wndStatusBar.SetIndicators(indicators, sizeof(indicators) / sizeof(UINT))) {
TRACE0("Failed to create status bar\n");
return -1;
}
m_wndStatusBar.SetPaneStyle(1, SBPS_NORMAL);
// ---- docking
m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY); // allow the toolbar to dock...
EnableDocking(CBRS_ALIGN_ANY); // ...and the frame to accept it
DockControlBar(&m_wndToolBar);
return 0;
}
afx_msg void OnMouseMove(UINT nFlags, CPoint pt) {
m_cursor = pt;
CFrameWnd::OnMouseMove(nFlags, pt);
}
afx_msg void OnUpdateCursorPos(CCmdUI* pCmdUI) { // called during idle time
CString s;
s.Format(_T("X=%d Y=%d"), m_cursor.x, m_cursor.y);
pCmdUI->Enable(); // required, or the pane stays blank
pCmdUI->SetText(s);
}
afx_msg void OnUpdateProgress(CCmdUI* pCmdUI) { pCmdUI->Enable(); }
void SetMessage(const CString& text) { m_wndStatusBar.SetPaneText(0, text); } // pane 0
DECLARE_MESSAGE_MAP()
};
IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)
BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
ON_WM_CREATE()
ON_WM_MOUSEMOVE()
ON_UPDATE_COMMAND_UI(ID_INDICATOR_CURSORPOS, &CMainFrame::OnUpdateCursorPos)
END_MESSAGE_MAP()
Common pitfalls
LoadToolBarfails if the toolbar’s bitmap size doesn’t match the item count in the resource: the editor must keep them in sync.- The
indicatorsarray must bestaticor global: the status bar keeps a pointer to it. - A custom pane shows nothing unless it has an
ON_UPDATE_COMMAND_UIthat callsEnable(); also give the pane ID a string resource whose length sets the initial width. - Return
-1fromOnCreateon failure; otherwise the frame is created without a bar. - Calling
ShowControlBarorRecalcLayoutbefore the frame exists crashes. - Large toolbar icons on high-DPI monitors look blurry: provide higher-resolution images, or use the Feature Pack toolbar (
CMFCToolBar) which scales. - Toolbar buttons with no handler are greyed out (the auto-disable rule). Check the message map if a button is dead.
Win32 vs MFC
The Win32 equivalents are the TOOLBARCLASSNAME and STATUSCLASSNAME common controls with TB_ADDBUTTONS / SB_SETPARTS messages, plus manual layout in WM_SIZE. MFC’s control bars add docking, tooltips and automatic layout.
Exercises
1. Why is a status bar pane blank after adding ID_INDICATOR_CURSORPOS to the array?
Custom panes are drawn only when an update-UI handler enables them and sets their text. Add ON_UPDATE_COMMAND_UI(ID_INDICATOR_CURSORPOS, ...) and call pCmdUI->Enable() / SetText().