3.MFC的运行流程

我们来了解一下MFC程序创建的流程, 一下代码在控制台程序中,模拟MFC的运行流程,每一个流程的内容是在控制台输出一些语句。

程序运行的结果如下:

类图结构:

上图便是 MFC 程序 的运行流程

MFC.h
#pragma once
#include<iostream>
using namespace std;

class CObject
{
public:
	
};

class CCmdTarget : public CObject
{
public:
	
};


class CWinThread : public CCmdTarget
{
public:

	virtual bool InitInstance()
	{
		cout << "CWinThread:InitInstance \n";
		return true;
	}

	virtual int Run()
	{
		cout << "CWinThread:Run \n";
		return 1;
	}

};
//先声音 CWnd类 因为在 CWinApp中需要用到,但是却没有 实际定义 CWnd这个类
class CWnd;

class CWinApp : public CWinThread
{
public:
	CWinApp* m_pCurrentWinApp;
	CWnd* m_pMainWnd;
public:
	CWinApp::CWinApp()
	{
		m_pCurrentWinApp = this;
	
	}
	

	virtual bool InitInstance()
	{
		cout << "CWinApp:InitInstance \n";
		return true;
	}

	virtual int Run()
	{
		cout << "CWinApp:Run \n";
		return CWinThread::Run();
	}
	
	virtual bool InitApplication()
	{
		cout << "CWinApp:InitApplication \n";
		return true;
	}

};



class CDocument : public CCmdTarget
{
public:

};

class CWnd : public CCmdTarget
{
public:

	virtual bool Create();
	bool CreateEx();
	virtual bool PreCreateWindow();

};

class CView : public CWnd
{
public:

};

class CFrameWnd : public CWnd
{
public:

	bool Create();
	virtual bool PreCreateWindow();
};

CWinApp* AfxGetApp();


MFC.cpp
#include "my.h"
extern CMyWinAPP theApp;
CWinApp* AfxGetApp()
{
	return theApp.m_pCurrentWinApp;
}

bool CWnd::Create()
{
	cout << "CWnd::Create \n";
	return true;
}

bool CWnd::CreateEx()
{
	cout << "CWnd::CreateEx \n";
	//PreCreateWindow 是虚函数,所以调用哪个类的PreCreateWindow是根据 传入的指针来确定的
	PreCreateWindow();
	return true;
}

bool CWnd::PreCreateWindow()
{
	cout << "CWnd::PreCreateWindow \n";
	return true;
}

bool CFrameWnd::Create()
{
	cout << "CFrameWnd::Create \n";
	//由于CFrameWnd 并没有重写 CreateEx 所以 调用父类的 CreateEx函数
	CreateEx();
	return true;
}

bool CFrameWnd::PreCreateWindow()
{
	cout << "CFrameWnd::PreCreateWindow \n";
	return true;
}





my.h
#include<iostream>
#include"MFC.h"

class CMyWinAPP :public CWinApp
{
public:

	bool InitInstance();
};

class MyFrameWnd:public CFrameWnd
{
public:
	MyFrameWnd::MyFrameWnd()
	{ 
		cout << "MyFrameWnd::MyFrameWnd \n";
		//MyFrameWnd 并没有重写Create 所以 调用了 父类的Create函数
		Create();
	}

};


my.cpp
#include"my.h"

CMyWinAPP theApp;

bool CMyWinAPP::InitInstance()
{
	//让父类也初始化
	CWinApp::InitInstance();
	cout << "CMyWinAPP::InitInstance \n";

	m_pMainWnd = new MyFrameWnd();
	return true;
}

void main()
{
	CWinApp *pApp = AfxGetApp();
	//初始化运行环境等工作
	pApp->InitApplication();
	//对给个类对象进行初始化工作
	pApp->InitInstance();
	//程序开始运行
	pApp->Run();


	system("pause");
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值