深入浅出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();
	}
}



 

你可能感兴趣的:(深入浅出MFC 第三章)