11.04.2014 Views

Advanced MFC Programming

Advanced MFC Programming

Advanced MFC Programming

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 2. Menu<br />

“clipboard”. We can indicate the data status by putting a check mark on the menu item so the user knows if<br />

the current “data” in the “clipboard” has been pasted (after the user executes View | Paste command). This<br />

check will be removed when either “cut” or “copy” command is executed.<br />

Similar to tool bar, we can call function CCmdUI::SetCheck(…) to set check for a menu item. The<br />

difference between the results of this function is the changing on the interface: while setting check for a<br />

button will make it recess, setting check for a menu item will put a check mark at the left side of the menu<br />

item.<br />

We need a new Boolean type variable to indicate the status of “data”. In the sample application, this<br />

variable is CMenuDoc::m_bDataPasted, which is initialized to FALSE in the constructor. The following<br />

functions show how its value is changed under different situations:<br />

void CMenuDoc::OnEditCopy()<br />

{<br />

m_bPasteAvailable=TRUE;<br />

m_bDataPasted=FALSE;<br />

}<br />

void CMenuDoc::OnEditCut()<br />

{<br />

m_bPasteAvailable=TRUE;<br />

m_bDataPasted=FALSE;<br />

}<br />

void CMenuDoc::OnEditPaste()<br />

{<br />

m_bDataPasted=TRUE;<br />

}<br />

In function OnUpdateEditPaste(…), the menu item is checked only when flag CMenuDoc::<br />

m_bDataPasted is TRUE:<br />

void CMenuDoc::OnUpdateEditPaste(CCmdUI* pCmdUI)<br />

{<br />

pCmdUI->Enable(m_bPasteAvailable);<br />

pCmdUI->SetCheck(m_bDataPasted);<br />

pCmdUI->SetText<br />

(<br />

m_bPasteAvailable ?<br />

(m_bDataPasted ? "Data &pasted":"Please &paste"):<br />

"Do not &paste"<br />

);<br />

}<br />

The text of the menu item is also change to “Data pasted” when the menu item is checked.<br />

The last thing need to be mentioned here is another member function of class CCmdUI: CCmdUI::<br />

SetRadio(…). Like CCmdUI::SetCheck(…), this function will put a check mark on a menu item. The<br />

difference between two functions is that CCmdUI::SetRadio(…) makes menu items behave like radio<br />

buttons: when this function is called to check one item, all other items in the same group will be unchecked<br />

automatically. Calling function CCmdUI::SetCheck(…) does not affect other menu items.<br />

2.2 Right Click Pop Up Menu<br />

In Windows 95, right-click menu becomes a standard user interface. We can right click on the<br />

desktop, task bar, or other types of windows to bring up a menu that contains the most commonly used<br />

commands. In this section, we will discuss how to add right-click menu to our application.<br />

37

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!