如何:为模板类创建消息映射

MFC 中的信息映射提供了一种有效方法直接处理 Windows 消息为适当的 C++ 对象实例。 MFC 消息映射目标示例包含应用程序类、文档和视图类,控件类等等。

传统 MFC 消息映射声明使用 BEGIN_MESSAGE_MAP 宏声明消息映射的开头,它为每个消息处理程序类方法的宏输入,最后 END_MESSAGE_MAP 宏声明映射消息的结尾。

当该宏与包含模板参数的类一起使用时, BEGIN_MESSAGE_MAP 宏的一个限制会出现。 在和模板类一起使用时,此宏会导致编译时错误由于在宏展开过程中缺少模板参数。 BEGIN_TEMPLATE_MESSAGE_MAP 宏的设计允许类包含单个模板参数声明其消息映射。

示例

考虑 MFC CListBox 类扩展为提供与外部数据源同步的示例。 虚拟 CSyncListBox 类的声明如下:

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

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

   // ...additional functionality as needed
};

CSyncListBox 类上描述将同步的数据源,为单一类型模板。 它还声明三种方法参与类的消息映射:OnPaintOnDestroyOnSynchronizeOnSynchronize 方法实现如下:

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

   ResetContent();

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

   return 0L;
}

上述实现允许 CSyncListBox 类可以被任何实现 GetCount 方法的类类型指定,例如 CArrayCListCMapStringizeElement 函数为模板函数原型如下:

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

通常,此类的消息映射将如下定义:

BEGIN_MESSAGE_MAP(CSyncListBox, CListBox)

ON_WM_PAINT()

ON_WM_DESTROY()

ON_MESSAGE(LBN_SYNCHRONIZE, OnSynchronize)

END_MESSAGE_MAP()

LBN_SYNCHRONIZE 为自定义用户消息的情况由应用程序定义,例如:

#define LBN_SYNCHRONIZE (WM_USER + 1)

上面宏映射将不编译,因为在宏展开过程中 CSyncListBox 类的模板规范会丢失。 BEGIN_TEMPLATE_MESSAGE_MAP 宏通过合并指定的模板参数到扩展的宏映射来解决此问题。 此类的消息映射将成为:

BEGIN_TEMPLATE_MESSAGE_MAP(CSyncListBox, CollectionT, CListBox)
   ON_WM_PAINT()
   ON_WM_DESTROY()
   ON_MESSAGE(LBN_SYNCHRONIZE, OnSynchronize)
END_MESSAGE_MAP()

下面演示使用 CStringList 对象的 CSyncListBox 类的用法:

void CSyncListBox_Test(CWnd* pParentWnd)
{
   CSyncListBox<CStringList> ctlStringLB;
   ctlStringLB.Create(WS_CHILD | WS_VISIBLE | LBS_STANDARD | WS_HSCROLL, 
      CRect(10,10,200,200), pParentWnd, IDC_MYSYNCLISTBOX);

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

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

   // Verify the contents of the list box by printing out its contents
   INT nCount = ctlStringLB.GetCount();
   for( INT n = 0; n < nCount; n++ )
   {
      TCHAR szText[256];
      ctlStringLB.GetText(n, szText);
      TRACE(_T("%s\n"), szText);
   }
}

若要完成测试,StringizeElement 函数必须指定与 CStringList 类一起使用:

template<>
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
}

请参见

参考

BEGIN_TEMPLATE_MESSAGE_MAP

概念

消息处理和映射