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

}<br />

In the sample, bitmaps are prepared for both checked and unchecked states for a menu item. When<br />

calling function CMenu::SetMenuItemBitmaps(…), if either of the bitmaps is not provided (the<br />

corresponding parameter is NULL), nothing will be displayed for that state. If both parameters are NULL,<br />

the default tick mark will be used for the checked state.<br />

2.5 System Menu and Bitmap Menu Item<br />

System Menu<br />

By default, every application has a system menu, which is accessible through left clicking on the small<br />

icon located at the left side of application’s caption bar, or right clicking on the application when it is in<br />

icon state. The system menu can be customized to meet special requirement. Especially, we can add and<br />

delete menu items dynamically just like a normal menu.<br />

We already know how to access an application’s standard menu. Once we obtained a CMenu type<br />

pointer to the application’s standard menu, we can feel free to add new menu items, remove menu items,<br />

and change their attributes dynamically.<br />

System menu is different from a standard menu. We need to call another function to obtain a pointer to<br />

it. In <strong>MFC</strong>, the function that can be used to access system menu is CWnd::GetSystemMenu(…). Please note<br />

that we must call this function for a window that has an attached system menu. For an SDI or MDI<br />

application, system menu is attached to the mainframe window. For a dialog box based application, the<br />

system menu is attached to the dialog window.<br />

Unlike user implemented commands, system commands (commands on the system menu) are sent<br />

through WM_SYSCOMMAND rather than WM_COMMAND message. If we implement message handlers to receive<br />

system commands, we need to use ON_WM_SYSCOMMAND macro.<br />

Bitmap Menu Item<br />

From the samples in the previous sections, we already know how to customize a menu item: we can set<br />

check, remove check, change text dynamically. We can also use bitmaps to represent its checked and<br />

unchecked states. Besides above features, a menu item can be modified to display a bitmap instead of a text<br />

string. This can make the application more attractive, because sometimes images are more intuitive than<br />

text strings.<br />

Sample 2.5\Menu demonstrates the two techniques described above, it is based on sample 2.4\Menu.<br />

In this sample, one of the system menu items (a separator) is changed to bitmap menu item. If we execute<br />

this “bitmap command”, a message box will pop up. Also, another new command is added to the system<br />

menu, it allows the user to resume the original system menu.<br />

New Functions<br />

Function CWnd::GetSystemMenu(…) has only one Boolean type parameter:<br />

CMenu *CWnd::GetSystemMenu(BOOL bRevert);<br />

Although we can call this function to obtain a pointer to the system menu and manipulate it, the<br />

original default system menu can be reverted at any time by calling this function and passing a TRUE value<br />

to its bRevert parameter. In this case, function’s returned value has no meaning and should not be treated<br />

as a pointer to a menu object. We need to pass FALSE to this parameter in order to obtain a valid pointer to<br />

the system menu.<br />

Function CMenu::ModifyMenu(…) allows us to change any menu item to a separator, a sub-menu, a<br />

bitmap menu item. It can also be used to modify a menu item’s text. This member function has two<br />

versions:<br />

47

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

Saved successfully!

Ooh no, something went wrong!