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 />

ON_COMMAND(ID_EDIT_COPY, OnEditCopy)<br />

ON_COMMAND(ID_EDIT_CUT, OnEditCut)<br />

ON_COMMAND(ID_EDIT_PASTE, OnEditPaste)<br />

//}}AFX_MSG_MAP<br />

END_MESSAGE_MAP()<br />

3) Three blank message handlers are added in file “MenuDoc.cpp”:<br />

void CMenuDoc::OnEditCopy()<br />

{<br />

}<br />

void CMenuDoc::OnEditCut()<br />

{<br />

}<br />

void CMenuDoc::OnEditPaste()<br />

{<br />

}<br />

When first added, these functions are empty. We have to add our own code in order to support<br />

command execution.<br />

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

Cut and Edit | Paste menu items are all enabled. This is because three blank message handlers have just<br />

been added.<br />

Enabling & Disabling a Command<br />

The sample application will not actually cut, copy or paste data. The three commands will be<br />

implemented just to simulate data copy and paste procedure. Before going on to implement it, we need to<br />

make following assumptions.<br />

Suppose the application supports only internal data copy, cut and paste (it does not accept data from<br />

other applications through using system clipboard). Before command Edit | Copy or Edit | Cut is<br />

executed, there should be no data stored in the “local clipboard”. Therefore, if we execute Edit | Paste<br />

command at this time, there will be an error. To avoid this, we need to disable Edit | Paste command<br />

before data has been copied to the clipboard.<br />

The state of menu item can be set thought handling UPDATE_COMMAND_UI message. The parameter<br />

comes along with this message is a pointer to CCmdUI type object, which can be used to enable or disable a<br />

command, set or remove check for a menu item. Handling this message for menu items is the same with<br />

that of tool bar controls.<br />

So it is easy to find out a mechanism for updating command Edit | Paste: we need to declare a<br />

Boolean type variable in class CMenuDoc and initialize it to FALSE. We can set this flag to TRUE when<br />

Edit | Cut or Edit | Copy command is executed, and enable Edit | Paste command only if this flag is set.<br />

In the sample application, this Boolean variable is CMenuDoc::m_bPasteAvailable. The following<br />

code fragment shows how it is declared and initialized in the constructor of class CMenuDoc:<br />

……<br />

……<br />

class CMenuDoc : public CDocument<br />

{<br />

protected:<br />

BOOL m_bPasteAvailable;<br />

}<br />

CMenuDoc::CMenuDoc()<br />

{<br />

m_bPasteAvailable=FALSE;<br />

}<br />

The value of CmenuDoc::m_bPasteAvailable is set to TRUE when user executes either Edit | Copy or<br />

Edit | Cut command:<br />

35

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

Saved successfully!

Ooh no, something went wrong!