MFC程序初始化过程仿真(参考了侯俊杰的《深入浅出MFC》)

       下面的Windows Console程序是 MFC程序初始化过程的仿真,如下:

 

#include <iostream>
using namespace std;

// MFC类
class CObject{};

class CCmdTarget : public CObject{};

class CWinThread : public CCmdTarget
{
public:
	virtual void InitInstance() 
	{
		cout << "CWinThread::InitInstance \n";
	}

	virtual void Run()                     // 8
	{
		cout << "CWinThread::Run \n";
	}
};

class CWnd;
class CWinApp : public CWinThread
{
public:
	CWinApp* m_pCurrentWinApp;
	CWnd* m_pMainWnd;

public:
	CWinApp::CWinApp()  
	{
		m_pCurrentWinApp = this;
	}

	virtual void InitApplication()         // 第1个被调用
	{
		cout << "CWinApp::InitApplication \n";
	}

	virtual void InitInstance()    
	{
		cout << "CWinApp::InitInstance \n";
	}

	virtual void Run()                     // 7
	{
		cout << "CWinApp::Run \n";
	    CWinThread::Run();
	}
};

class CWnd : public CCmdTarget
{
public:
	virtual void Create()
	{
		cout << "CWnd::Create \n";
	}

	void CreateEx()
	{
		cout << "CWnd::CreateEx \n";       // 5
		PreCreateWindow();
	}

	virtual void PreCreateWindow()         // 非6
	{
		cout << "CWnd::PreCreateWindow \n";
	}
};

class CFrameWnd : public CWnd
{
public:
	void Create()  // 4
	{
		cout << "CFrameWnd::Create \n";
		CreateEx();
	}

	virtual void PreCreateWindow()         // 6
	{
		cout << "CFrameWnd::PreCreateWindow \n";
	}
};

class CMyFrameWnd : public CFrameWnd
{
public:
	CMyFrameWnd::CMyFrameWnd()             // 3
	{
		cout << "CMyFrameWnd::CMyFrameWnd \n";
		Create();
	}
};

class CMyWinApp : public CWinApp
{
public:
	virtual void InitInstance()            // 2 
	{
		cout << "CMyWinApp::InitInstance \n";
		m_pMainWnd = new CMyFrameWnd;      // 没考虑内存释放
	}
};


CMyWinApp theApp;     // 全局对象

CWinApp* AfxGetApp()  // 全局函数
{
  return theApp.m_pCurrentWinApp;
}

int main()
{

	CWinApp* pApp = AfxGetApp();

	pApp->InitApplication();
	// CWinApp::InitApplication

	pApp->InitInstance();
	// CMyWinApp::InitInstance
	// CMyFrameWnd::CMyFrameWnd
    // CFrameWnd::Create
    // CWnd::CreateEx
	// CFrameWnd::PreCreateWindow (千万注意这个)

	pApp->Run();
	// CWinApp::Run
	// CWinThread::Run

	return 0;
}

 

 

 

 

 

 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值