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

Adding Menu Resource<br />

Sample 2.2\Menu demonstrates right-click menu implementation. It is a standard SDI application<br />

generated by Application Wizard with all the default settings. This is the same with the previous sample.<br />

We can also start from sample 2.1\Menu and add the new features that will be discussed below.<br />

Like tool bar and dialog bar, a menu can be implemented starting from building menu resource. To add<br />

a menu resource, We can execute Insert | Resource command in Developer Studio, select “menu” resource<br />

type from the popped up dialog box, and click button “New”. Now a new menu resource with a default ID<br />

will be added to the application. In the sample, this default ID is changed to IDR_MENU_POPUP, and a submenu<br />

with four menu items is created. The newly created menu items are “Pop Up Item 1”, “Pop Up Item<br />

2”, “Pop Up Item 3” and “Pop Up Item 4”, whose command IDs are ID__POPUPITEM1, ID__POPUPITEM2,<br />

ID__POPUPITEM3 and ID__POPUPITEM4 respectively (Figure 2-1).<br />

For right-click menu, we<br />

don’t need to provide a<br />

text for sub menu title<br />

Add menu items here<br />

Figure 2-1: Add menu resource for right-click menu<br />

Trapping Right Button Clicking Event<br />

The first step to implement a right-click menu is to detect mouse’s right clicking event, which is a<br />

standard Windows event, and its corresponding message is WM_RBUTTONDOWN. To trap this message, we<br />

need to implement message handler.<br />

When we click mouse’s right button on a window, message WM_RBUTTONDOWN will be sent to that<br />

window. This window could be any type: mainframe window, client window, dialog box, or even button.<br />

We need to handle this message in the class that implements the window. For example, if we want to<br />

handle right click in a dialog box, we need to add the message handler in a CDialog derived class, if we<br />

want to handle it in the client window of an SDI application, we need to add the message handler in CView<br />

derived class.<br />

In our sample, right-clicking menu is implemented in the client window. So we need to trap message<br />

WM_RBUTTONDOWN in class CMenuView.<br />

Adding message handler for message WM_RBUTTONDOWN is similar to that of WM_COMMAND: first we need<br />

to declare an afx_msg type member function OnRButtonDown(…), then use ON_RBUTTONDOWN macro to map<br />

the message to this function. Finally, we need to implement the message handler. Please note that<br />

OnRButtonDow(…) is the standard function name that will be automatically associated with message<br />

WM_RBUTTONDOWN. When using ON_RBUTTONDOWN macro, we do not need to specify function name.<br />

The above-mentioned procedure can be implemented through using Class Wizard as follows: after<br />

invoking the Class Wizard, select class CMenuView, which is the class used to implement the client window.<br />

There are a lot of virtual functions and messages listed in the “Messages” window. By scrolling the vertical<br />

scroll bar, it is easy to find message WM_RBUTTONDOWN. Now highligh this message and click “Add function”<br />

button. This will cause a new member function OnRButtonDown to be added to class CMenuView, and<br />

message mapping macros to be added to the implementation file (See Figure 2-2).<br />

In the sample, the newly added function is CMenuView::OnRButtonDown(…), which needs to be<br />

modified to implement right-click menu. By default, this function does nothing but calling the message<br />

handler implemented by the base class:<br />

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

{<br />

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

}<br />

38

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

Saved successfully!

Ooh no, something went wrong!