Dialog和FormView如何派生通用类

派生通用类涉及到派生类的构造函数需要传递窗口ID和CWnd,所以要在派生类中事先定义好

在Dialog中构造函数是这样定义的

public:
    CDialogEx();
    CDialogEx(UINT nIDTemplate, CWnd *pParent = NULL);
    CDialogEx(LPCTSTR lpszTemplateName, CWnd *pParentWnd = NULL);

所以在派生

Dialog类时要这样构造
class CThemeDialogForFixShootSearch : public CDialogEx
{
    DECLARE_DYNAMIC(CThemeDialogForFixShootSearch)

public:
    CThemeDialogForFixShootSearch(UINT nIDTemplate, CWnd* pParent = NULL);   // standard constructor
    virtual ~CThemeDialogForFixShootSearch();
}
#include "stdafx.h"
#include "ThemeDialogForFixShootSearch.h"



// CThemeDialogForFixShootSearch dialog

IMPLEMENT_DYNAMIC(CThemeDialogForFixShootSearch, CDialogEx)

CThemeDialogForFixShootSearch::CThemeDialogForFixShootSearch(UINT nIDTemplate, CWnd* pParent /*=NULL*/)
    : CDialogEx(nIDTemplate, pParent)
{
   
}

CThemeDialogForFixShootSearch::~CThemeDialogForFixShootSearch()
{
}

void CThemeDialogForFixShootSearch::DoDataExchange(CDataExchange* pDX)
{
    CDialogEx::DoDataExchange(pDX);
}


BEGIN_MESSAGE_MAP(CThemeDialogForFixShootSearch, CDialogEx)
END_MESSAGE_MAP()
同样FormView的构造函数为
protected:      // must derive your own class
    CFormView(LPCTSTR lpszTemplateName);
    CFormView(UINT nIDTemplate);

因为FormView没有默认构造,所以必须带参数

#pragma once
#include "afxext.h"

class CBackgroundColor :public CFormView
{
    DECLARE_DYNAMIC(CBackgroundColor)
public:
    CBackgroundColor(UINT nIDTemplate);
    virtual ~CBackgroundColor();
    DECLARE_MESSAGE_MAP()
    afx_msg void OnPaint();
};
#include "stdafx.h"
#include "BackgroundColor.h"
#include <afxtempl.h>
IMPLEMENT_DYNAMIC(CBackgroundColor, CFormView)
CBackgroundColor::CBackgroundColor (UINT nIDTemplate)
: CFormView(nIDTemplate)
{
}


CBackgroundColor::~CBackgroundColor()
{
}
BEGIN_MESSAGE_MAP(CBackgroundColor, CFormView)
    ON_WM_PAINT()
END_MESSAGE_MAP()
 
原文地址:https://www.cnblogs.com/ye-ming/p/8880089.html