Handle any category of messages using Class Wizard


Very often the Class Wizard will not list the messages you want to handle. In such situations you have to add the message map entry yourself. One common situation where this happens is when you derive from a class which itself has been derived from an MFC class. For example, if CListViewEx is derived from CListView and then you derive CMyListView from CListViewEx, then you would be unable to handle any of the list view control messages in the CMyListView class using the Class Wizard.

Here's what you can do to continue using the Class Wizard. At the top of your implementation file (cpp file) will the the message map which begins with a line like

BEGIN_MESSAGE_MAP(CMyListView, CListViewEx)
Class Wizard uses the second argument in this macro to decide whether it needs to display more messages. Since class wizard doesn't recognize the CListViewEx class it does not show any of the list view control messages. What you can do is change the line to
#define CListView CListViewEx
BEGIN_MESSAGE_MAP(CMyListView, CListView)
#undef CListView

This will now allow you to add message handlers for the CListView class.

Thanks to Gert Rijs from Netherlands for suggesting the use of the #define directive. It fools the Class Wizard parser but provides the right code to the compiler.