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 Item | Enable | SetCheck | SetRadio | SetText |
---|---|---|---|---|
Menu item | Enables or disables | Checks (×) or unchecks | Checks using dot (•) | Sets item text |
Toolbar button | Enables or disables | Selects, unselects, or indeterminate | Same as SetCheck | (Not applicable) |
Status-bar pane | Makes text visible or invisible | Sets pop-out or normal border | Same as SetCheck | Sets pane text |
Normal button in CDialogBar | Enables or disables | Checks or unchecks check box | Same as SetCheck | Sets button text |
Normal control in CDialogBar | Enables or disables | (Not applicable) | (Not applicable) | Sets window text |
具体解释如下:
CCmdUI 类没有基类,
当用户点击应用程序中的菜单时,菜单中的每个菜单项需要知道它应该显示为可用或不可用。这个菜单通过实现ON_UPDATE_COMMAND_UI 来提供这个通知给每个菜单项。用户应用程序中的每个命令都应该通过属性窗口来构造这么一个消息对。(例:ON_UPDATE_COMMAND_UI(ID_HIGH_QUALITY, &CMainFrame::OnUpdateHighQuality))
当这个菜单被按下的时候,应用程序框架就会搜索与这个菜单对应的ON_UPDATE_COMMAND_UI 消息对,并调用消息对中指定的函数(这个函数的参数中有CCmdUI 类的对象),在这个函数中可以调用CCmdUI 的成员函数如Enable或者SetCheck 等来设置每个菜单项。
1、Enable函数的函数原型为:
virtual void Enable(
BOOL bOn = TRUE
);
bOn
TRUE to enable the item, FALSE to disable it.
TRUE的话这个项可以使用,FALSE则是禁用。
2、SetCheck 的函数原型为:
virtual void SetCheck(
int nCheck = 1
);
nCheck
Specifies the check state to set. If 0, unchecks; if 1, checks; and if 2, sets indeterminate.
0不选中、1选中、2终止
3、SetRadio 的函数原型为:
virtual void SetRadio(
BOOL bOn = TRUE
); |
-
bOn
-
TRUE to enable the item; otherwise FALSE.
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.
这个方法的作用SetCheck相似,不过这个方法作用于一组单选按钮
图例:
代码示例: