DYNAMENU

The DYNAMENU sample demontrates dynamic modification of menus, regardless of whether handling commands are known at compile time.

DYNAMENU initially displays a window with the text "This text is displayed in the current color."

To exercise the dynamic menu update feature of DYNAMENU, click Change Options on the Color menu. This opens a Change Color Options dialog box. Four check boxes, for the colors Black, Dark Gray, Light Gray and White, allow you to choose which colors are dynamically offered in the Color menu. By unchecking the box for a particular color, you can clear the color from the Color menu.

Dynamically Updating the List of Items in a Menu

Class CMainFrame in MAINFRM.CPP implements dynamic updating of items in the Color menu. When the list of available colors is updated, the CMainFrame::RefreshColorMenu function resets the command bar, inserts the menu (without the color options), and uses CMenu::InsertMenu to add the currently available colors to the menu.

Implementing Command Handlers for Dynamic Menu Items

DYNAMENU could have been implemented by reserving a fixed list of command IDs for the colors: ID_COLOR_BLACK, ID_COLOR_WHITE, etc. In such a case, ON_COMMAND and ON_UPDATE_COMMAND_UI handlers could have been implemented for the color commands as usual. This would be the most straightforward way to implement DYNAMENU.

For sake of illustration, DYNAMENU does not use fixed command IDs. Instead, DYNAMENU dynamically assigns command IDs not known or associated with the menu items at compile time. This illustration can be applied to more complex cases, such as user-configurable menus.

The equivalent of ON_COMMAND and ON_UPDATE_COMMAND message map handling is implemented in the document's override of CCmdTarget::OnCmdMsg. If the OnCmdMsg function is called with a NULL pointer for the AFX_CMDHANDLERINFO* parameter, this means that no message map entry has been found for the command. In this case, the override of OnCmdMsg checks whether the command ID, passed as the first parameter, is one of the dynamically assigned command IDs for the color menu items. If so, the override calls either a command handler (DoSelectColor) or command user interface handler (DoUpdateSelectColor), depending on whether the second parameter passed to OnCmdMsg is the MFC-defined CN_COMMAND or CN_UPDATE_COMMAND_UI.

This sample demonstrates the following keywords:

CArchive::IsStoring CObject::AssertValid
CCmdTarget::OnCmdMsg CObject::Dump
CCmdUI::Enable CObject::Serialize
CDC::DrawText CString::LoadString
CDC::SetBkMode CView::GetDocument
CDC::SetTextColor CWinApp::AddDocTemplate
CDialog::DoModal CWinApp::OnFileNew
CDocument::GetFirstViewPosition CWnd::Attach
CDocument::GetNextView CWnd::CenterWindow
CDocument::OnNewDocument CWnd::GetClientRect
CDocument::UpdateAllViews CWnd::GetDlgItem
CFrameWnd::AddAdornments CWnd::GetMenu
CFrameWnd::GetActiveDocument CWnd::GetParentFrame
CFrameWnd::InsertMenu CWnd::SetWindowText
CMenu::CheckMenuItem AfxGetMainWnd
CMenu::InsertMenu

You can find the DYNAMENU project files in the Wce\Samples\Mfc\DYNAMENU directory.

To build the DYNAMENU sample, follow the procedure described in Building the Sample Applications, selecting one or more of the following project configurations.