Question

I am using MFC100, VS2010, MDI.

I am over ridding the basic functionality of CPreviewView.

I want it to act very similar to my CMyView(Which is a CView)

CMyView and CMyPreviewView are not in the same class inheritance paths.

So I want to inherit both the CPreviewView message map and CMyView message maps. Currently, it only inherits the CPreviewView message maps.

BEGIN_MESSAGE_MAP(CMyPreviewView, CPreviewView)
    //{{AFX_MSG_MAP(CMyPreviewView)
    ON_COMMAND(AFX_ID_PREVIEW_CLOSE, OnPreviewClose)
    ON_COMMAND(AFX_ID_PREVIEW_PRINT, OnPreviewPrint)
    ON_UPDATE_COMMAND_UI(ID_WINDOW_NEW, OnUpdateWindowNew)
    //}}AFX_MSG_MAP
END_MESSAGE_MAP()

Any Ideas? I saw things like

ALT_MSG_MAP(UINT msgMapID)

But that is only for ATL. I could copy and paste my entire CMyView message map, but that is really messy being that is has 300+ handlers.

Was it helpful?

Solution

Put the message map into a separate .h file and #include it into both classes.

BEGIN_MESSAGE_MAP(CMyPreviewView, CPreviewView)
    //{{AFX_MSG_MAP(CMyPreviewView)
    ON_COMMAND(AFX_ID_PREVIEW_CLOSE, OnPreviewClose)
    ON_COMMAND(AFX_ID_PREVIEW_PRINT, OnPreviewPrint)
    ON_UPDATE_COMMAND_UI(ID_WINDOW_NEW, OnUpdateWindowNew)

#include "SharedViewMessageMap.h"

    //}}AFX_MSG_MAP
END_MESSAGE_MAP()

OTHER TIPS

Per the section CWnd and Message Maps on TN016: Using C++ Multiple Inheritance with MFC, it is not possible to make the MFC message map work with multiple inheritance work with more than one type derived from CWnd as any CWnd-derived base must be the left-most one in its inheritance. Since CView derives from CWnd, you cannot multiply inherit from those classes.

In order to avoid direct duplication of your code, it's possible you may be able to refactor the code you wish to share into some sort of mixin, perhaps using composition or CRTP.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top