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

}<br />

AfxMessageBox("Pop up menu item 2");<br />

void CMenuDoc::OnPopUpItem3()<br />

{<br />

AfxMessageBox("Pop up menu item 3");<br />

}<br />

void CMenuDoc::OnPopUpItem4()<br />

{<br />

AfxMessageBox("Pop up menu item 4");<br />

}<br />

With the above implementation, we are able to execute the commands contained in the right-click pop<br />

up menu.<br />

2.3 Updating Menu Dynamically<br />

Sometimes it is desirable to change the contents of a menu dynamically. For example, if we create an<br />

application that supports many commands, we may want to organize them into different groups. Sometimes<br />

we want to enable a group of commands, sometimes we want to disable them.<br />

Although we can handle UPDATE_COMMAND_UI message to enable or disable commands, sometimes it is<br />

more desirable if we can remove the whole sub-menu instead of just graying the menu text. Actually, submenu<br />

and menu item can all be modified dynamically: we can either add or delete a sub-menu or menu<br />

item at any time; we can also change the text of a menu item, move a sub-menu or menu item, or add a<br />

separator between two menu items. All these things can be implemented at run-time.<br />

Menu Struture<br />

The structure of menu à sub menu à menu item is like a tree. At the topmost level (the root), the<br />

menu comprises several sub-menus. Each sub-menu also comprises several items, which could be a normal<br />

command or another sub-menu. For example, in application Explorer (file browser in Windows95), its<br />

first level menu comprises five sub-menus: File, Edit, View, Tool, and Help. If we examine File submenu,<br />

we will see that it comprises eight items: New, separator, Create Shortcut, Delete, Rename,<br />

Properties, separator and Close. Here, item New is another sub-menu, which comprises several other<br />

menu items. This kind of structure can continue. As long as our program needs, we can organize our menu<br />

into many different levels.<br />

In <strong>MFC</strong>, class CMenu should be used this way. With a CMenu type pointer to a menu object, we have the<br />

access to only the menu items at certain level. If we want to access a menu item at a lower level, we first<br />

need to access the sub-menu that contains the desired menu item.<br />

This can be explained by the previous “Explorer” example: suppose we have a CMenu type pointer to<br />

the main menu, we can use it to access the first level menu items: File, Edit, View, Tool, and Help. This<br />

means we can use the pointer to disable, enable or set text for any of the above items, but we can not use it<br />

to make change to the items belonging to other levels, for example, New item under File sub-menu. To<br />

access this item, we need to first obtain a CMenu type pointer to File sub-menu, then use it to modify item<br />

File | New.<br />

Inserting and Removing Menu Item<br />

Class CMenu has certain member functions that allow us to insert or delete a menu item dynamically.<br />

We can add either a menu item (including separator), or a whole sub-menu. When we remove a sub-menu,<br />

all the lower level items and sub-menus will be removed.<br />

The function that can be used to insert menu items or sub-menus is CMenu::InsertMenu(…), it has the<br />

following format:<br />

BOOL CMenu::InsertMenu<br />

(<br />

42

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

Saved successfully!

Ooh no, something went wrong!