Microsoft KB Archive/97693

= INFO: Dynamic Processing of WM_COMMAND Messages =

Article ID: 97693

Article Last Modified on 11/21/2006

-

APPLIES TO

 Microsoft Foundation Class Library 4.2, when used with:  Microsoft Visual C++ 1.0 Professional Edition

 Microsoft Visual C++ 1.5 Professional Edition

 Microsoft Visual C++ 1.51

 Microsoft Visual C++ 1.52 Professional Edition</li></ul>

 Microsoft Visual C++ 1.0 Professional Edition</li></ul>

 Microsoft Visual C++ 2.0 Professional Edition</li></ul>

 Microsoft Visual C++ 2.1</li></ul>

 Microsoft Visual C++ 4.0 Standard Edition</li></ul> </li></ul>

-

<div class="notice_section">

This article was previously published under Q97693

<div class="notice_section">

<div class="summary_section">

SUMMARY
The message-map mechanism provided by the Microsoft Foundation Classes (MFC) can process WM_COMMAND messages for a constant ID. However, in some cases, an application needs to process WM_COMMAND messages for an ID that is not known until run time. This can occur when an application modifies menus or dynamically creates controls at run time. To process these messages, your application must override the CCmdTarget::OnCmdMsg function.

<div class="moreinformation_section">

MORE INFORMATION
The Microsoft Foundation Classes provide both the ON_COMMAND and ON_UPDATE_COMMAND_UI message-map macros to process WM_COMMAND messages. The ON_COMMAND macro dispatches a WM_COMMAND message generated when the user selects a user interface object (for example, a control or a menu item). The ON_UPDATE_COMMAND_UI macro updates the status of the user interface object that generates the WM_COMMAND message. Each of these macros requires a constant for the command ID. If the command ID is not known until run time, the message must be processed without using a message map.

The sample code below demonstrates overriding the OnCmdMsg member function to dynamically process messages from a dynamically-created button.

The sample code processes messages for a class derived from CDocument. The command ID is stored in the m_MyButtonID member variable. The code calls the DoMyButton member function when the user chooses the button, and calls the DoUpdateMyButton member function when the Microsoft Foundation Classes library requests the status of the button.

Sample Code
BOOL CMyDocument::OnCmdMsg(UINT nID, int nCode, void * pExtra,                             AFX_CMDHANDLERINFO* pHandlerInfo) {     // If pHandlerInfo is NULL, then handle the message if (pHandlerInfo == NULL) {        // Filter the commands sent to a button the // ID of which is stored in m_MyButtonID.

if (nID == m_MyButtonID) {           if (nCode == CN_COMMAND) DoMyButton;     // Handles WM_COMMAND messages // sent by the button. else if (nCode == CN_UPDATE_COMMAND_UI) DoUpdateMyButton((CCmdUI*) pExtra); // Update buttons UI. return(TRUE); }        }

// Call the base class version of OnCommand // so the message-map can handle the message. return CDocument::OnCmdMsg(nID, nCode, pExtra, pHandlerInfo); }

<div class="references_section">