How to: Create a Message Map for a Template Class
Important This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here. ArchiveDisclaimer

How to: Create a Message Map for a Template Class 

Message mapping in MFC provides an efficient way to direct Windows messages to an appropriate C++ object instance. Examples of MFC message map targets include application classes, document and view classes, control classes, and so on.

Traditional MFC message maps are declared using the BEGIN_MESSAGE_MAP macro to declare the start of the message map, a macro entry for each message-handler class method, and finally the END_MESSAGE_MAP macro to declare the end of the message map.

One limitation with the BEGIN_MESSAGE_MAP macro occurs when it is used in conjunction with a class containing template arguments. When used with a template class, this macro will cause a compile-time error due to the missing template parameters during macro expansion. The BEGIN_TEMPLATE_MESSAGE_MAP macro was designed to allow classes containing a single template argument to declare their own message maps.


Consider an example where the MFC CListBox class is extended to provide synchronization with an external data source. The fictitious CSyncListBox class is declared as follows:

// Extends the CListBox class to provide synchronization with 
// an external data source
template <typename CollectionT> 
class CSyncListBox : public CListBox
   virtual ~CSyncListBox();

   afx_msg void OnPaint();
   afx_msg void OnDestroy();
   afx_msg LRESULT OnSynchronize( WPARAM wParam, LPARAM lParam );

   // ...additional functionality as needed

The CSyncListBox class is templated on a single type that describes the data source it will synchronize with. It also declares three methods that will participate in the message map of the class: OnPaint, OnDestroy, and OnSynchronize. The OnSynchronize method is implemented as follows:

template <class CollectionT> 
LRESULT CSyncListBox<CollectionT>::OnSynchronize( WPARAM, LPARAM lParam )
   CollectionT* pCollection = (CollectionT*)( lParam );


   if( pCollection != NULL )
      INT nCount = (INT)pCollection->GetCount();
      for( INT n = 0; n < nCount; n++ )
         CString s = StringizeElement( pCollection, n );
         AddString( s );

   return 0L;

The above implementation allows the CSyncListBox class to be specialized on any class type that implements the GetCount method, such as CArray, CList, and CMap. The StringizeElement function is a template function prototyped by the following:

// Template function for converting an element within a collection
// to a CString object
template<typename CollectionT>
CString StringizeElement( CollectionT* pCollection, INT iIndex );

Normally, the message map for this class would be defined as:


where LBN_SYNCHRONIZE is a custom user message defined by the application, such as:


The above macro map will not compile, due to the fact that the template specification for the CSyncListBox class will be missing during macro expansion. The BEGIN_TEMPLATE_MESSAGE_MAP macro solves this by incorporating the specified template parameter into the expanded macro map. The message map for this class becomes:

BEGIN_TEMPLATE_MESSAGE_MAP( CSyncListBox, CollectionT, CListBox )

The following demonstrates sample usage of the CSyncListBox class using a CStringList object:

extern CSyncListBox<CStringList>* pctlStringLB;

void CSyncListBox_Test()
   // Create a CStringList object and add a few strings
   CStringList stringList;
   stringList.AddTail( "A" );
   stringList.AddTail( "B" );
   stringList.AddTail( "C" );

   // Send a message to the list box control to synchronize its
   // contents with the string list
   pctlStringLB->SendMessage( LBN_SYNCHRONIZE, 0, (LPARAM)&stringList );

   // Verify the contents of the list box by printing out its contents
   INT nCount = pctlStringLB->GetCount();
   for( INT n = 0; n < nCount; n++ )
      CHAR szText[256];
      pctlStringLB->GetText( n, szText );
      TRACE( "%s\n", szText );

To complete the test, the StringizeElement function must be specialized to work with the CStringList class:

CString StringizeElement( CStringList* pStringList, INT iIndex )
   if( pStringList != NULL && iIndex < pStringList->GetCount() )
      POSITION pos = pStringList->GetHeadPosition();
      for( INT i = 0; i < iIndex; i++ )
         pStringList->GetNext( pos );
      return pStringList->GetAt( pos );
   return CString(); // or throw, depending on application requirements

See Also

© 2016 Microsoft