CCmdUI 简介

 

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相似,不过这个方法作用于一组单选按钮

图例:

 

 

 

代码示例:

 

 

### 修改 MFC `CCmdUI` 控件的字体颜色 在 MFC 应用程序中,直接修改 `CCmdUI` 对象的颜色并不是一件简单的事情,因为 `CCmdUI` 类本身并不提供用于设置控件属性(如字体颜色)的方法。然而,可以通过自定义绘制技术来实现这一需求。 为了改变菜单项或其他由 `CCmdUI` 表示的对象的文字颜色,通常的做法是在消息映射机制内捕获特定的消息并执行自定义绘图逻辑。具体来说,在派生类中覆盖 `afx_msg void OnUpdateCmdUI(CFrameWnd* pTarget, BOOL bDisableIfNoHndler)` 函数,并在此处添加代码以拦截相关事件[^2]。 对于工具栏按钮这类控件而言,可以考虑继承 `CToolBar` 或者 `CToolBarCtrl` 并重载虚函数 `OnCustomDraw()` 来实施个性化渲染过程。下面给出一段简单的例子展示如何操作: ```cpp class CMyToolBar : public CToolBar { protected: afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct); virtual void PreSubclassWindow(); afx_msg HRESULT OnCustomDraw(NMHDR *pNMHDR, LRESULT *pResult); DECLARE_MESSAGE_MAP() }; BEGIN_MESSAGE_MAP(CMyToolBar, CToolBar) ON_WM_CREATE() ON_NOTIFY_REFLECT(TTN_NEEDTEXTW, &CMyToolBar::OnToolTipText) ON_NOTIFY_REFLECT_EX(NM_CUSTOMDRAW, OnCustomDraw) END_MESSAGE_MAP() int CMyToolBar::OnCreate(LPCREATESTRUCT lpCreateStruct) { if (CToolBar::OnCreate(lpCreateStruct) == -1) return -1; // 初始化... return 0; } void CMyToolBar::PreSubclassWindow() { ModifyStyle(0, TBSTYLE_FLAT | CCS_ADJUSTABLE); SetFlatStyle(true); CToolBar::PreSubclassWindow(); } HRESULT CMyToolBar::OnCustomDraw(NMHDR *pNMHDR, LRESULT *pResult){ LPNMCUSTOMDRAW lpcustomdraw = reinterpret_cast<LPNMCUSTOMDRAW>(pNMHDR); switch(lpcustomdraw->dwDrawStage){ case CDDS_PREPAINT:{ *pResult = CDRF_NOTIFYITEMDRAW; break; } case CDDS_ITEMPREPAINT:{ CDC dc; dc.Attach(lpcustomdraw->hdc); COLORREF oldColor = dc.SetTextColor(RGB(255, 0, 0)); // 设置红色文字 // 还原之前的颜色配置 dc.Detach(); *pResult = CDRF_DODEFAULT; break; } default:*pResult = CDRF_DODEFAULT;break; } return S_OK; } ``` 这段代码展示了如何通过覆写 `OnCustomDraw` 方法来自定义工具栏上各个项目的外观表现形式,这里仅作为示范设置了所有项目文本为红色。实际应用时可以根据需要调整条件判断语句以便针对不同类型的条目施加不同的样式效果。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值