Unlike the message-handler functions for menu commands and child notification messages, the message-handler functions for other types of WM_XXX messages, such as WM_PAINT or WM_RBUTTONDOWN, variously take one or more arguments and may have a return a value. The function name and the argument signature required for each of these message-handler functions is predefined by the message-map macro for each particular message.
For example, the message-handler function for the WM_RBUTTONDOWN message must be declared as shown here:
afx_msg void OnRButtonDown( UINT nFlags, CPoint point );
The function prototypes for all the message handler functions are declared in the CWnd declaration in AFXWIN.H. Note the difference here from the previous discussions of message-handler functions for menu commands and notification messages. For menu commands and notification messages, you are free to make up your own name for your message-handler function. You identify the message-handler function by passing the function name to the message-map entry macro for that message.
For example, if you called the message-handler function for the IDM_ABOUT menu command MyWonderfulMenuCommandHandler, your message map would look like this:
BEGIN_MESSAGE_MAP( CMyWnd, CFrameWnd )
ON_COMMAND( IDM_ABOUT, MyWonderfulMenuCommandHandler )
END_MESSAGE_MAP()
In contrast, the names for the message-handler functions for the other WM_XXX messages are predefined by the Microsoft Foundation Class Library. Thus, you must use the Foundation-defined name and the required argument signature for each window message that you handle through the message map. The file AFXWIN.H contains function prototypes for all the message-handler functions for the WM_XXX messages. You can find these prototypes by searching AFXWIN.H for the word afx_msg. This word, when prefixed to a function prototype, identifies the function as a message-handler function.
Note:
Message-handler functions prefixed with afx_msg can be thought of just like virtual functions in that they are meant to be overridden in derived classes. They differ from virtual functions in the way they are actually implemented and dispatched, which is more efficient than standard virtual functions.
The macro that defines the message-map entry for the WM_RBUTTONDOWN message is named ON_WM_RBUTTONDOWN. This macro expects to find a message-handler member function named OnRButtonDown defined for the window class. Since the names of the message-handler functions for WM_XXX window messages are predefined, the macros for the message-map entries for these messages do not need any arguments, as shown in the following procedure.
·To create a window class that responds to a right mouse button click:
Declare a class and define a message map as follows:
class CMyWnd : public CFrameWnd
{
// constructor not shown...
public:
afx_msg void OnRButtonDown( UINT nFlags, CPoint point );
DECLARE_MESSAGE_MAP()
};
// in .CPP file
BEGIN_MESSAGE_MAP( CMyWnd, CFrameWnd )
ON_WM_RBUTTONDOWN()
END_MESSAGE_MAP()
The prototype function declaration for OnRButtonDown is contained in the CWnd class declaration in AFXWIN.H. Consult this file to determine the proper function name and argument/return value signature for the message-handler function for the message that you want to handle.