CCmdUI Class

 

The new home for Visual Studio documentation is Visual Studio 2017 Documentation on docs.microsoft.com.

The latest version of this topic can be found at CCmdUI Class.

Is used only within an ON_UPDATE_COMMAND_UI handler in a CCmdTarget-derived class.

class CCmdUI  

Public Methods

NameDescription
CCmdUI::ContinueRoutingTells the command-routing mechanism to continue routing the current message down the chain of handlers.
CCmdUI::EnableEnables or disables the user-interface item for this command.
CCmdUI::SetCheckSets the check state of the user-interface item for this command.
CCmdUI::SetRadioLike the SetCheck member function, but operates on radio groups.
CCmdUI::SetTextSets the text for the user-interface item for this command.

Public Data Members

NameDescription
CCmdUI::m_nIDThe ID of the user-interface object.
CCmdUI::m_nIndexThe index of the user-interface object.
CCmdUI::m_pMenuPoints to the menu represented by the CCmdUI object.
CCmdUI::m_pOtherPoints to the window object that sent the notification.
CCmdUI::m_pSubMenuPoints to the contained sub-menu represented by the CCmdUI object.

CCmdUI does not have a base class.

When a user of your application pulls down a menu, each menu item needs to know whether it should be displayed as enabled or disabled. The target of a menu command provides this information by implementing an ON_UPDATE_COMMAND_UI handler. For each of the command user-interface objects in your application, use the Properties window to create a message-map entry and function prototype for each handler.

When the menu is pulled down, the framework searches for and calls each ON_UPDATE_COMMAND_UI handler, each handler calls CCmdUI member functions such as Enable and Check, and the framework then appropriately displays each menu item.

A menu item can be replaced with a control-bar button or other command user-interface object without changing the code within the ON_UPDATE_COMMAND_UI handler.

The following table summarizes the effect CCmdUI's member functions have on various command user-interface items.

User-Interface ItemEnableSetCheckSetRadioSetText
Menu itemEnables or disablesChecks (×) or unchecksChecks using dot (•)Sets item text
Toolbar buttonEnables or disablesSelects, unselects, or indeterminateSame as SetCheck(Not applicable)
Status-bar paneMakes text visible or invisibleSets pop-out or normal borderSame as SetCheckSets pane text
Normal button in CDialogBarEnables or disablesChecks or unchecks check boxSame as SetCheckSets button text
Normal control in CDialogBarEnables or disables(Not applicable)(Not applicable)Sets window text

For more on the use of this class, see How to Update User-Interface Objects.

CCmdUI

Header: afxwin.h

Call this member function to tell the command-routing mechanism to continue routing the current message down the chain of handlers.

void ContinueRouting();

Remarks

This is an advanced member function that should be used in conjunction with an ON_COMMAND_EX handler that returns FALSE. For more information, see Technical Note 6.

Call this member function to enable or disable the user-interface item for this command.

virtual void Enable(BOOL bOn = TRUE);

Parameters

bOn
TRUE to enable the item, FALSE to disable it.

Example

   ON_UPDATE_COMMAND_UI(ID_FILE_SAVE, &CMyDoc::OnUpdateFileSave)

void CMyDoc::OnUpdateFileSave(CCmdUI* pCmdUI)
{
   // Enable the menu item if the file has been modified.
   pCmdUI->Enable(m_bModified);   
}

The ID of the menu item, toolbar button, or other user-interface object represented by the CCmdUI object.

UINT m_nID;  

The index of the menu item, toolbar button, or other user-interface object represented by the CCmdUI object.

UINT m_nIndex;  

Pointer (of CMenu type) to the menu represented by the CCmdUI object.

CMenu* m_pMenu;  

Remarks

NULL if the item is not a menu.

Pointer (of CMenu type) to the contained sub-menu represented by the CCmdUI object.

CMenu* m_pSubMenu;  

Remarks

NULL if the item is not a menu. If the sub menu is a pop-up, m_nID contains the ID of the first item in the pop-up menu. For more information, see Technical Note 21.

Pointer (of type CWnd) to the window object, such as a tool or status bar, that sent the notification.

CWnd* m_pOther;  

Remarks

NULL if the item is a menu or a non- CWnd object.

Call this member function to set the user-interface item for this command to the appropriate check state.

virtual void SetCheck(int nCheck = 1);

Parameters

nCheck
Specifies the check state to set. If 0, unchecks; if 1, checks; and if 2, sets indeterminate.

Remarks

This member function works for menu items and toolbar buttons. The indeterminate state applies only to toolbar buttons.

Call this member function to set the user-interface item for this command to the appropriate check state.

virtual void SetRadio(BOOL bOn = TRUE);

Parameters

bOn
TRUE to enable the item; otherwise FALSE.

Remarks

This member function operates like SetCheck, except that it operates on user-interface items acting as part of a radio group. Unchecking the other items in the group is not automatic unless the items themselves maintain the radio-group behavior.

Call this member function to set the text of the user-interface item for this command.

virtual void SetText(LPCTSTR lpszText);

Parameters

lpszText
A pointer to a text string.

Example

void CMyRichEditView::OnUpdateLineNumber(CCmdUI *pCmdUI)
{
   int nLine = GetRichEditCtrl().LineFromChar(-1) + 1;

   CString string;
   string.Format(_T("Line %d"), nLine);
   pCmdUI->Enable(TRUE);
   pCmdUI->SetText(string);
}

MFC Sample MDI
Hierarchy Chart
CCmdTarget Class

Show: