深入浅出MFC 第三章

mfc.h

#pragma once
#include <iostream>
using std::cout;
using std::endl;

namespace chapter03
{
	class CWnd;

	class CObject
	{
	public:
		CObject() 
		{
			cout << "CObject Constructor" << endl;
		}

		~CObject()
		{
			cout << "CObject Destructor" << endl;

			getchar();
		}
	};

	class CCmdTarget : public CObject
	{
	public:
		CCmdTarget()
		{
			cout << "CCmdTarget Constructor" << endl;
		}

		~CCmdTarget()
		{
			cout << "CCmdTarget Destructor" << endl;
		}
	};

	class CWinThread : public CCmdTarget
	{
	public:
		CWinThread()
		{
			cout << "CWinThread Constructor" << endl;
		}

		~CWinThread()
		{
			cout << "CWinThread Destructor" << endl;
		}

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

	class CWinApp : public CWinThread
	{
	public:
		CWinApp()
		{
			m_pCurrentWinApp = this;
			cout << "CWinApp Constructor" << endl;
		}

		~CWinApp()
		{
			cout << "CWinApp Destructor" << endl;
		}

		virtual BOOL InitApplication()
		{
			cout << "CWinApp::InitApplication" << endl;
			return TRUE;
		}

		virtual BOOL InitInstance()
		{
			cout << "CWinApp::InitInstance" << endl;
			return TRUE;
		}

		virtual int Run()
		{
			cout << "CWinApp::Run" << endl;
			return CWinThread::Run();
		}

		CWinApp    *m_pCurrentWinApp;
		CWnd       *m_pMainWnd;
	};

	class CWnd : public CCmdTarget
	{
	public:
		CWnd()
		{
			cout << "CWnd Constructor" << endl;
		}

		~CWnd()
		{
			cout << "CWnd Destructor" << endl;
		}

		virtual BOOL Create()
		{
			cout << "CWnd::Create" << endl;

			return TRUE;
		}

		BOOL CreateEx()
		{
			cout << "CWnd::CreateEx" << endl;
			PreCreateWindow();
			return TRUE;
		}

		virtual BOOL PreCreateWindow()
		{
			cout << "CWnd::PreCreateWindow" <<endl;
			return TRUE;
		}
	};

	class CView : public CWnd
	{
	public:
		CView()
		{
			cout << "CView Constructor" << endl;
		}

		~CView()
		{
			cout << "CView Destructor" << endl;
		}
	};

	class CFrameWnd : public CWnd
	{
	public:
		CFrameWnd()
		{
			cout << "CFrameWnd Constructor" << endl;
		}

		~CFrameWnd()
		{
			cout << "CFrameWnd Destructor" << endl;
		}

		virtual BOOL Create()
		{
			cout << "CFrameWnd::Create" << endl;
			CreateEx();
			return TRUE;
		}

		virtual BOOL PreCreateWindow()
		{
			cout << "CFrameWnd::PreCreateWindow" << endl;
			return TRUE;
		}
	};

	class CDocument : public CCmdTarget
	{
	public:
		CDocument()
		{
			cout << "CDocument Constructor" << endl;
		}

		~CDocument()
		{
			cout << "CDocument Destructor" << endl;
		}
	};

	CWinApp* AfxGetApp();
}


mfc.cpp

#include "StdAfx.h"
//#include "mfc.h"
#include "my.h"

namespace chapter03
{
	extern CMyWinApp theApp;

	CWinApp* AfxGetApp()
	{
		return theApp.m_pCurrentWinApp;
	}
}





my.h

#pragma once

#include "mfc.h"

namespace chapter03
{
	class CMyFrameWnd : public CFrameWnd
	{
	public:
		CMyFrameWnd()
		{
			cout << "CMyFrameWnd Constructor" << endl;
			Create();
		}

		~CMyFrameWnd()
		{
			cout << "CMyFrameWnd Destructor" << endl;
		}
	};

	class CMyWinApp : public CWinApp
	{
	public:
		CMyWinApp()
		{
			cout << "CMyWinApp Constructor" << endl;
		}

		~CMyWinApp()
		{
			cout << "CMyWinApp Destructor" << endl;
		}

		virtual BOOL InitInstance()
		{
			cout << "CMyWinApp::InitInstance" << endl;
			m_pMainWnd = new CMyFrameWnd;
			return TRUE;
		}
	};



	void MyMain();
}


my.cpp

#include "StdAfx.h"
#include "my.h"

namespace chapter03
{
	CMyWinApp theApp;

	void MyMain()
	{
		CWinApp* pApp = AfxGetApp();

		pApp->InitApplication();
		pApp->InitInstance();

		pApp->Run();
	}
}



 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值