description | title | ms.date | helpviewer_keywords | ms.assetid | ||
---|---|---|---|---|---|---|
Learn more about: How to: Create a Message Map for a Template Class |
How to: Create a Message Map for a Template Class |
11/04/2016 |
|
4e7e24f8-06df-4b46-82aa-7435c8650de3 |
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:
[!code-cppNVC_MFC_CListBox#42]
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:
[!code-cppNVC_MFC_CListBox#43]
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:
[!code-cppNVC_MFC_CListBox#44]
Normally, the message map for this class would be defined as:
BEGIN_MESSAGE_MAP(CSyncListBox, CListBox)
ON_WM_PAINT()
ON_WM_DESTROY()
ON_MESSAGE(LBN_SYNCHRONIZE, OnSynchronize)
END_MESSAGE_MAP()
where LBN_SYNCHRONIZE is a custom user message defined by the application, such as:
[!code-cppNVC_MFC_CListBox#45]
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:
[!code-cppNVC_MFC_CListBox#46]
The following demonstrates sample usage of the CSyncListBox
class using a CStringList
object:
[!code-cppNVC_MFC_CListBox#47]
To complete the test, the StringizeElement
function must be specialized to work with the CStringList
class:
[!code-cppNVC_MFC_CListBox#48]