天天看點

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相似,不過這個方法作用于一組單選按鈕

圖例:

CCmdUI 簡介

代碼示例:

BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)

ON_WM_CREATE()

ON_WM_TIMER()

ON_WM_CLOSE()

ON_COMMAND(ID_RECORD_START, &CMainFrame::OnRecordStart)

ON_COMMAND(ID_RECORD_STOP, &CMainFrame::OnRecordStop)

ON_UPDATE_COMMAND_UI(ID_RECORD_START, &CMainFrame::OnUpdateRecordStart)

ON_UPDATE_COMMAND_UI(ID_RECORD_STOP, &CMainFrame::OnUpdateRecordStop)

ON_COMMAND(ID_HIGH_QUALITY, &CMainFrame::OnHighQuality)

ON_COMMAND(ID_LOW_QUALITY, &CMainFrame::OnLowQuality)

ON_UPDATE_COMMAND_UI(ID_HIGH_QUALITY, &CMainFrame::OnUpdateHighQuality)

ON_UPDATE_COMMAND_UI(ID_LOW_QUALITY, &CMainFrame::OnUpdateLowQuality)

END_MESSAGE_MAP()

void CMainFrame::OnRecordStart()

{

// TODO: 在此添加指令處理程式代碼

MessageBeep(MB_ICONASTERISK);

this->m_bWorking = TRUE;

}

void CMainFrame::OnRecordStop()

{

// TODO: 在此添加指令處理程式代碼

MessageBeep(MB_ICONHAND);

this->m_bWorking = FALSE;

}

void CMainFrame::OnUpdateRecordStart(CCmdUI *pCmdUI)

{

// TODO: 在此添加指令更新使用者界面處理程式代碼

pCmdUI->Enable(!m_bWorking);

}

void CMainFrame::OnUpdateRecordStop(CCmdUI *pCmdUI)

{

// TODO: 在此添加指令更新使用者界面處理程式代碼

pCmdUI->Enable(m_bWorking) ;

}

void CMainFrame::OnHighQuality()

{

// TODO: 在此添加指令處理程式代碼

m_bHighQuality=TRUE;

}

void CMainFrame::OnLowQuality()

{

// TODO: 在此添加指令處理程式代碼

m_bHighQuality=FALSE;

}

void CMainFrame::OnUpdateHighQuality(CCmdUI *pCmdUI)

{

// TODO: 在此添加指令更新使用者界面處理程式代碼

pCmdUI->SetCheck(m_bHighQuality);

}

void CMainFrame::OnUpdateLowQuality(CCmdUI *pCmdUI)

{

// TODO: 在此添加指令更新使用者界面處理程式代碼

pCmdUI->SetCheck(!m_bHighQuality);

}

繼續閱讀