In addition to the ON_COMMAND message map macro, the MFC library provides an extended variation, ON_COMMAND_EX. The extended command message map macro provides two features not supplied by the regular command message—a command ID function parameter and the ability to reject a command at run time, sending it to the next object in the command route. If the extended command handler returns TRUE, the command goes no further; if it returns FALSE, the application framework looks for another command handler.
The command ID parameter is useful when you want one function to handle several related command messages. You might invent some of your own uses for the rejection feature.
The code wizards available from Class View's Properties window can't help you with extended command handlers, so you have to do the coding yourself, outside the AFX_MSG_MAP brackets. Assume that IDM_ZOOM_1 and IDM_ZOOM_2 are related command IDs defined in
BEGIN_MESSAGE_MAP(CMyView, CView) ON_COMMAND_EX(IDM_ZOOM_1, OnZoom) ON_COMMAND_EX(IDM_ZOOM_2, OnZoom) END_MESSAGE_MAP() BOOL CMyView::OnZoom(UINT nID) { if (nID == IDM_ZOOM_1) { // code specific to first zoom command } else { // code specific to second zoom command } // code common to both commands return TRUE; // Command goes no further }
Here's the function prototype:
afx_msg BOOL OnZoom(UINT nID);
Other MFC message map macros are helpful for processing ranges of commands, as you might see in dynamic menu applications. These macros include ON_COMMAND_RANGE, ON_COMMAND_EX_RANGE, and ON_UPDATE_COMMAND_UI_RANGE.
If the values of IDM_ZOOM_1 and IDM_ZOOM_2 were consecutive, you could rewrite the CMyView message map as follows:
BEGIN_MESSAGE_MAP(CMyView, CView) ON_COMMAND_EX_RANGE(IDM_ZOOM_1, IDM_ZOOM_2, OnZoom) END_MESSAGE_MAP()
Now OnZoom is called for both menu commands, and the handler can determine the command from the integer parameter.