//********************************MFC.H***************************
#define BOOL int
#define TRUE 1
#define FALSE 0
#include <iostream.h>
class CObject
{
public:
CObject::CObject() {
}
CObject::~CObject() {
}
};
class CCmdTarget : public CObject
{
public:
CCmdTarget::CCmdTarget() {
}
CCmdTarget::~CCmdTarget() {
}
};
class CWinThread : public CCmdTarget
{
public:
CWinThread::CWinThread() {
}
CWinThread::~CWinThread() {
}
virtual BOOL InitInstance() {
cout << "CWinThread::InitInstance \n";
return TRUE;
}
virtual int Run() {
cout << "CWinThread::Run \n";
return 1;
}
};
class CWnd;
class CWinApp : public CWinThread
{
public:
CWinApp* m_pCurrentWinApp;
CWnd* m_pMainWnd;
public:
CWinApp::CWinApp() {
m_pCurrentWinApp = this;
}
CWinApp::~CWinApp() {
}
virtual BOOL InitApplication() {
cout << "CWinApp::InitApplication \n";
return TRUE;
}
virtual BOOL InitInstance() {
cout << "CWinApp::InitInstance \n";
return TRUE;
}
virtual int Run() {
cout << "CWinApp::Run \n";
return CWinThread::Run();
}
};
class CDocument : public CCmdTarget
{
public:
CDocument::CDocument() {
}
CDocument::~CDocument() {
}
};
class CWnd : public CCmdTarget
{
public:
CWnd::CWnd() {
}
CWnd::~CWnd() {
}
virtual BOOL Create();
BOOL CreateEx();
virtual BOOL PreCreateWindow();
};
class CFrameWnd : public CWnd
{
public:
CFrameWnd::CFrameWnd() {
}
CFrameWnd::~CFrameWnd() {
}
BOOL Create();
virtual BOOL PreCreateWindow();
};
class CView : public CWnd
{
public:
CView::CView() {
}
CView::~CView() {
}
};
// global function
CWinApp* AfxGetApp();
//*****************************MFC.CPP*************************************
#include "my.h" // it should be mfc.h, but for CMyWinApp definition, so...
extern CMyWinApp theApp;
BOOL CWnd::Create()
{
cout << "CWnd::Create \n";
return TRUE;
}
BOOL CWnd::CreateEx()
{
cout << "CWnd::CreateEx \n";
PreCreateWindow();
return TRUE;
}
BOOL CWnd::PreCreateWindow()
{
cout << "CWnd::PreCreateWindow \n";
return TRUE;
}
BOOL CFrameWnd::Create()
{
cout << "CFrameWnd::Create \n";
CreateEx();
return TRUE;
}
BOOL CFrameWnd::PreCreateWindow()
{
cout << "CFrameWnd::PreCreateWindow \n";
return TRUE;
}
CWinApp* AfxGetApp()
{
return theApp.m_pCurrentWinApp;
}
//************************MY.H*********************
#include <iostream.h>
#include "mfc.h"
class CMyWinApp : public CWinApp
{
public:
CMyWinApp::CMyWinApp() {
}
CMyWinApp::~CMyWinApp() {
}
virtual BOOL InitInstance();
};
class CMyFrameWnd : public CFrameWnd
{
public:
CMyFrameWnd();
~CMyFrameWnd() {
}
};
//*************************MY.CPP************************
#include "my.h"
CMyWinApp theApp;
BOOL CMyWinApp::InitInstance()
{
cout << "CMyWinApp::InitInstance \n";
m_pMainWnd = new CMyFrameWnd;
return TRUE;
}
CMyFrameWnd::CMyFrameWnd()
{
cout << "CMyFrameWnd::CMyFrameWnd \n";
Create();
}
//------------------------------------------------------------------
// main
//------------------------------------------------------------------
void main()
{
CWinApp* pApp = AfxGetApp();
pApp->InitApplication();
pApp->InitInstance();
pApp->Run();
}
//------------------------------------------------------------------
CObject
/CCmdTarget
//CWinThread
///CWinApp
CMyWinApp
//CWnd
///CFrameWnd
CMyFrameWnd
1) CMyWinApp theApp;
1)CObject(){} //调用CObject构造函数 为空
2)CCmdTarget(){} //调用CCmdTarget()构造函数 为空
3)CWinThread(){} //调用CWinThread()构造函数 为空
4)CWinApp(){m_pCurrentWinApp=this} //调用CWinApp()构造函数 this指向CMyWinApp类对象theApp
5)CMyWinApp(){} //调用CMyWinApp()的构造函数 为空
2) CWinApp* pApp=AfxGetApp(); //=theApp.m_pCurrentWinApp 指向CMyWinApp类对象theApp
3) pApp->InitApplication();
1)CMyWinApp没有重定义虚函数InitApplication()
2)调用CWinApp中的InitApplication()
1)输出 CWinApp::InitApplication
2)返回 CWinApp中的InitApplication()完成
4) pApp->InitInstance(); //调用CMyWinApp::InitInstance()
1)输出 CMyWinApp::InitInstance
2)m_pMainWnd = new CMyFrameWnd;
1)CObject(){} //调用CObject()构造函数 为空
2)CCmdTarget(){} //调用CCmdTarget()构造函数 为空
3)CWnd(){} //调用CWnd()构造函数 为空
4)CFrameWnd(){} //调用CFrameWnd构造函数 为空
5)CMyFrameWnd(); //调用CMyFrameWnd构造函数
1)输出 CMyFrameWnd::CMyFrameWnd
2)Create();
1)从类CMyFrameWnd中寻找 无
2)从类CFrameWnd中寻找 BOOL Create();
1)输出 CFrameWnd::Create
2)调用CreateEx();
1)类CFrameWnd中没有定义
2)从CWnd中寻找BOOL CreateEx();
1)输出 CWnd::CreateEx
2)调用PreCreateWindow()是虚函数
1)类CMyFrameWnd中无定义这个虚函数
2)类CFrameWnd定义了这个虚函数
1)输出 CFrameWnd::PreCreateWindow
2)返回 PreCreateWindow()完成
3)返回 CWnd中的CreateEx()完成
3)返回 CFrameWnd中Create()完成
3)CMyFrameWnd构造函数结束
3)CMyWinApp::InitInstance()完成
5)pApp->Run();
1)CMyWinApp 没有重定义虚函数Run()
2)调用CWinApp中的Run()
1)输出 CWinApp::Run
2)调用CWinThread::Run()做为返回
1)输出 CWinThread::Run
2)返回 CWinApp中的Run()完成
***************************整理输出为*********************
CWinApp::InitApplication
CMyWinApp::InitInstance
CMyFrameWnd::CMyFrameWnd
CFrameWnd::Create
CWnd::CreateEx
CFrameWnd::PreCreateWindow
CWinApp::Run
CWinThread::Run
和书上一样