INFO: Dynamic Processing of WM_COMMAND Messages (97693)



The information in this article applies to:

  • The Microsoft Foundation Classes (MFC), when used with:
    • Microsoft Visual C++ for Windows, 16-bit edition 1.0
    • Microsoft Visual C++ for Windows, 16-bit edition 1.5
    • Microsoft Visual C++ for Windows, 16-bit edition 1.51
    • Microsoft Visual C++ for Windows, 16-bit edition 1.52
    • Microsoft Visual C++, 32-bit Editions 1.0
    • Microsoft Visual C++, 32-bit Editions 2.0
    • Microsoft Visual C++, 32-bit Editions 2.1
    • Microsoft Visual C++, 32-bit Editions 4.0

This article was previously published under Q97693

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.

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);
   }
				

REFERENCES

MFC Technical Note 6: Message Maps.
MFC Technical Note 21: Command and Message Routing.

Modification Type:MajorLast Reviewed:12/11/2003
Keywords:kbArchitecture kbDocView kbinfo KB97693