示例:
//This example illustrates extending the framework's standard command
//route from the view to objects managed by the view. This example
//is from an object-oriented drawing application, similar to the
//DRAWCLI sample application, which draws and edits "shapes".
BOOL CMyView::OnCmdMsg(UINT nID, int nCode, void* pExtra,
AFX_CMDHANDLERINFO* pHandlerInfo )
{
//Extend the framework's command route from the view to
//the application-specific CMyShape that is currently selected.
//in the view. m_pActiveShape is NULL if no shape object
//is currently selected in the view.
if( (m_pActiveShape!=NULL)&&
m_pActiveShape->OnCmdMsg(nID, nCode, pExtra, pHandlerInfo
) )
return TRUE;
//If the object(s) in the extended command route don't handle
//the command, then let the base class OnCmdMsg handle it.
return CView::OnCmdMsg(nID, nCode, pExtra, pHandlerInfo );
}
//The command handler for ID_SHAPE_COLOR (menu command used to change
//the color of the currently selected shape ) was added to
//the message map of CMyShape (note, not CMyView ) using ClassWizzard.
//The menu item will be automatically enabled or disabled, depending
//on whether a CMyShape is currently selected in the view, that is
//depending on whether CMyView::m_pActiveView is NULL. It is not
//necessary to implement an ON_UPDATE_COMMAND_UI handler to enable
//or disable the menu item.
BEGIN_MESSAGE_MAP( CMyShape, CCmdTarget )
//{{AFX_MSG_MAP( CMyShape )
ON_COMMAND( ID_SHAPE_COLOR, OnShapeColor )
//}}AFX_MSG_MAP( )
END_MESSAGE_MAP( )