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

This function returns a CMenu type pointer that could be used to further access each item contained in<br />

the sub-menu. Before the menu is displayed, we may want to set the state of each menu item: we can<br />

enable, disable, set check or change text for a menu item. Please note that for a right-click menu, we do not<br />

need to handle message UPDATE_COMMAND_UI in order to set the states of menu items. Instead, there exist<br />

two member functions that can be used:<br />

UINT CMenu::EnableMenuItem(UINT nIDEnableItem, UINT nEnable);<br />

UINT CMenu::CheckMenuItem(UINT nIDCheckItem, UINT nCheck);<br />

The above two functions can be used to enable/disable, set/remove check for a menu item. When<br />

calling the two functions, we can reference a menu item by using either its command ID or its position.<br />

Normally we can pass a command ID to nIDEnableItem or nIDCheckItem parameter. If we want to<br />

reference an item by its position (0 based, for example, in the sample application, ID__POPUPITEM1’s<br />

position is 0, and ID__POPUPITEM2’s position is 1…), we need to set MF_BYPOSITION bit of nEnable or<br />

nCheck parameter.<br />

The menu can be activated and tracked by calling function CMenu::TrackPopupMenu(…):<br />

BOOL CMenu::TrackPopupMenu<br />

(<br />

UINT nFlags, int x, int y, CWnd* pWnd, LPCRECT lpRect=NULL<br />

);<br />

This function has 5 parameters. The first parameter nFlags lets us set styles of the menu (Where<br />

should the menu be put, which mouse button will be tracked). The most commonly used combination is<br />

TPM_LEFTALIGN | TPM_RIGHTBUTTON, which aligns menu’s left border according to parameter x, and tracks<br />

mouse’s right button activity (because we are implementing a right-click menu). The second parameter y<br />

decides the vertical position of the menu’s top border. Please note that when message WM_RBUTTONDOWN is<br />

received, position of current mouse cursor will be passed to one of the parameters of function<br />

OnRButtonDown(…) as a CPoint type object. To make right-click menu easy to use, we can pass this<br />

position to function CMenu::TrackPopupMenu(…), which will create a pop up menu at the position of<br />

current mouse cursor. The fourth parameter is a CWnd type pointer, which indicates which window owns the<br />

pop up menu. In the sample, because the menu is implemented in the member function of class CMenuView,<br />

we can use this pointer to indicate the menu owner. The final parameter discribes a rectangle within which<br />

the user can click the mouse without dismissing the pop up menu. We could set it to NULL, in which case<br />

the menu will be dismissed if the user clicks outside the pop up menu.<br />

Implementing Right-Click Menu<br />

Now we can implement WM_RBUTTONDOWN message handler, load the menu resource and create rightclick<br />

menu in the member function:<br />

void CMenuView::OnRButtonDown(UINT nFlags, CPoint point)<br />

{<br />

CMenu menu;<br />

CMenu *ptrMenu;<br />

menu.LoadMenu(IDR_MENU_POPUP);<br />

ptrMenu=menu.GetSubMenu(0);<br />

ptrMenu->EnableMenuItem(ID__POPUPITEM1, MF_GRAYED);<br />

ptrMenu->EnableMenuItem(ID__POPUPITEM2, MF_ENABLED);<br />

ptrMenu->CheckMenuItem(ID__POPUPITEM3, MF_UNCHECKED);<br />

ptrMenu->CheckMenuItem(ID__POPUPITEM4, MF_CHECKED);<br />

ClientToScreen(&point);<br />

ptrMenu->TrackPopupMenu<br />

(<br />

TPM_LEFTALIGN|TPM_RIGHTBUTTON,<br />

point.x,<br />

point.y,<br />

this,<br />

NULL<br />

);<br />

CView::OnRButtonDown(nFlags, point);<br />

40

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

Saved successfully!

Ooh no, something went wrong!