11.04.2014 Views

Advanced MFC Programming

Advanced MFC Programming

Advanced MFC Programming

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 2. Menu<br />

void CMenuDoc::OnEditCopy()<br />

{<br />

m_bPasteAvailable=TRUE;<br />

}<br />

void CMenuDoc::OnEditCut()<br />

{<br />

m_bPasteAvailable=TRUE;<br />

}<br />

Now we will use CMenuDoc::m_bPasteAvailable to enable Edit | Paste menu item when it becomes<br />

TRUE. In <strong>MFC</strong>, menu items are updated through handling UPDATE_COMMAND_UI messages. When the state<br />

of a menu command needs to be updated, UPDATE_COMMAND_UI message will be automatically sent to the<br />

application. If there exists a corresponding message handler, it will be called for updating the<br />

corresponding menu item. Otherwise, the menu item will remain unchanged.<br />

Adding an UPDATE_COMMAND_UI message handler is the same with adding a WM_COMMAND message<br />

handler. After invoking the Class Wizard, we need to select the class name, the command ID, and highlight<br />

“UPDATE_COMMAND_UI” instead of “WM_COMMAND” in “Messages” window. Finally, we need to<br />

click button “Add Function”.<br />

After adding the message handler for ID_EDIT_PASTE command, we will have a new member function<br />

declared in class CMenuDoc, and a new message mapping macro added to the class implementation file. In<br />

addition, we will have an empty function that could be modified to implement message handling:<br />

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

{<br />

}<br />

The only parameter to this function is a pointer to CCmdUI type object. Here, class CCmdUI has several<br />

member functions, which can be used to set the state of the menu item. To enable or disable the menu item,<br />

we can call function CCmdUI::Enable(…). The function has only one Boolean type parameter, we can pass<br />

TRUE to enable the menu item and pass FALSE to disable it. Because the state of Edit | Paste command<br />

depends upon variable CMenuDoc::m_bPasteAvailable, we can use it to set the state of menu item:<br />

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

{<br />

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

}<br />

By compiling and executing the sample application at this point, we will see that Edit | Paste<br />

command is disabled at the beginning, and after we execute either Edit | Cut or Edit | Copy command, it<br />

will be enabled.<br />

Changing Menu Text<br />

We will go on to add more features to the mainframe menu. Class CCmdUI has another useful member<br />

function CCmdUI::SetText(…), which allows us to change the text of a menu item dynamically. By using<br />

this function, we can change the text of Edit | Paste menu item so that it can convey more information to<br />

the user. For example, we could set text to “Do not paste” when data is not available, and to “Please paste”<br />

when data is available. To add this feature, all we need is to call CCmdUI::SetText(…) in the above<br />

message handler as follows:<br />

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

{<br />

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

pCmdUI->SetText(m_bPasteAvailable ? "Please &paste":"Do not &paste");<br />

}<br />

Checking a Menu Item<br />

Let’s further add some more interesting features to the menu commands. With the current<br />

implementation we do not know if the data has been “Pasted” after it is “cut” or “copied” to the<br />

36

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

Saved successfully!

Ooh no, something went wrong!