CDynamicChain Class


For the latest documentation on Visual Studio 2017 RC, see Visual Studio 2017 RC Documentation.

This class provides methods supporting the dynamic chaining of message maps.

System_CAPS_ICON_important.jpg Important

This class and its members cannot be used in applications that execute in the Windows Runtime.

class CDynamicChain

Public Constructors

CDynamicChain::CDynamicChainThe constructor.
CDynamicChain::~CDynamicChainThe destructor.

Public Methods

CDynamicChain::CallChainDirects a Windows message to another object's message map.
CDynamicChain::RemoveChainEntryRemoves a message map entry from the collection.
CDynamicChain::SetChainEntryAdds a message map entry to the collection or modifies an existing entry.

CDynamicChain manages a collection of message maps, enabling a Windows message to be directed, at run time, to another object's message map.

To add support for dynamic chaining of message maps, do the following:

  • Derive your class from CDynamicChain. In the message map, specify the CHAIN_MSG_MAP_DYNAMIC macro to chain to another object's default message map.

  • Derive every class you want to chain to from CMessageMap. CMessageMap allows an object to expose its message maps to other objects.

  • Call CDynamicChain::SetChainEntry to identify which object and which message map you want to chain to.

For example, suppose your class is defined as follows:

class CMyChainWnd : public CWindowImpl<CMyChainWnd>,
   public CDynamicChain
   CMyChainWnd() {}

      // dynamically chain to the default
      // message map in another object
                // '1313' identifies the object
                // and the message map that will be
                // chained to. '1313' is defined
                // through the SetChainEntry method

   LRESULT OnPaint(UINT /*nMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, 
      BOOL& /*bHandled*/)
      // Do some painting code
      return 0;

   LRESULT OnSetFocus(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, 
      BOOL& /*bHandled*/)
      return 0;

The client then calls CMyWindow::SetChainEntry:

   myCtl.SetChainEntry(1313, &chainedObj);

where chainedObj is the chained object and is an instance of a class derived from CMessageMap. Now, if myCtl receives a message that is not handled by OnPaint or OnSetFocus, the window procedure directs the message to chainedObj's default message map.

For more information about message map chaining, see Message Maps in the article "ATL Window Classes."

Header: atlwin.h

Directs the Windows message to another object's message map.

BOOL CallChain(  
    DWORD dwChainID,
    HWND hWnd,
    UINT uMsg,
    WPARAM wParam,
    LPARAM lParam,
    LRESULT& lResult);


[in] The unique identifier associated with the chained object and its message map.

[in] The handle to the window receiving the message.

[in] The message sent to the window.

[in] Additional message-specific information.

[in] Additional message-specific information.

[out] The result of the message processing.

Return Value

TRUE if the message is fully processed; otherwise, FALSE.


For the window procedure to invoke CallChain, you must specify the CHAIN_MSG_MAP_DYNAMIC macro in your message map. For an example, see the CDynamicChain overview.

CallChain requires a previous call to SetChainEntry to associate the dwChainID value with an object and its message map.

The constructor.


The destructor.



Frees all allocated resources.

Removes the specified message map from the collection.

BOOL RemoveChainEntry(DWORD dwChainID);


[in] The unique identifier associated with the chained object and its message map. You originally define this value through a call to SetChainEntry.

Return Value

TRUE if the message map is successfully removed from the collection. Otherwise, FALSE.

Adds the specified message map to the collection.

BOOL SetChainEntry(  
    DWORD dwChainID,
    CMessageMap* pObject,
    DWORD dwMsgMapID = 0);


[in] The unique identifier associated with the chained object and its message map.

[in] A pointer to the chained object declaring the message map. This object must derive from CMessageMap.

[in] The identifier of the message map in the chained object. The default value is 0, which identifies the default message map declared with BEGIN_MSG_MAP. To specify an alternate message map declared with ALT_MSG_MAP(msgMapID), pass msgMapID.

Return Value

TRUE if the message map is successfully added to the collection. Otherwise, FALSE.


If the dwChainID value already exists in the collection, its associated object and message map are replaced by pObject and dwMsgMapID, respectively. Otherwise, a new entry is added.

CWindowImpl Class
Class Overview