//MFC.h #define BOOL int #define TRUE 1 #define FALSE 0 #include <iostream> using namespace std; class CObject { public: CObject(){}; ~CObject(){}; }; class CCmdTarget : public CObject { public: CCmdTarget(){}; ~CCmdTarget(){}; }; class CWinThread : public CCmdTarget { public: CWinThread(){}; ~CWinThread(){}; virtual BOOL InitInstance() { cout<<"CWinThread::InitInstance"<<endl; return TRUE; } virtual int Run() { cout<<"CWinThread::Run"<<endl; return 1; } }; class CWnd;//声明 class CWinApp : public CWinThread { public: CWinApp *m_pCurrentWinApp; CWnd *m_pMainWnd; public: CWinApp() { m_pCurrentWinApp = this; } ~CWinApp() { } 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(); } }; class CDocument : public CCmdTarget { public: CDocumnt(){}; ~CDocument(){}; }; class CWnd : public CCmdTarget { public: CWnd(){}; ~CWnd(){}; public: virtual BOOL Create(); BOOL CreateEx(); virtual BOOL PreCreateWindow(); }; class CFrameWnd : public CWnd { public: CFrameWnd(){}; ~CFrameWnd(){}; public: virtual BOOL Create(); virtual BOOL PreCreateWindow(); }; class CView : public CWnd { public: CView(){}; ~CView(){}; }; //global function CWinApp* AfxGetApp(); //MY.h #include "MFC.h" #include <iostream> using namespace std; class CMyWinApp : public CWinApp { public: CMyWinApp(){}; ~CMyWinApp(){}; public: virtual BOOL InitInstance(); }; class CMyFrameWnd : public CFrameWnd { public: CMyFrameWnd(); ~CMyFrameWnd(){}; }; //MFC.cpp #include "MY.h" extern CMyWinApp theApp;//extern global object BOOL CWnd::Create() { cout<<"CWnd::Create"<<endl; return TRUE; } BOOL CWnd::CreateEx() { cout<<"CWnd::CreateEx"<<endl; PreCreateWindow(); return TRUE; } BOOL CWnd::PreCreateWindow() { cout<<"CWnd::PreCreateWindow"<<endl; return TRUE; } BOOL CFrameWnd::Create() { cout<<"CFrameWnd::Create"<<endl; CreateEx(); return TRUE; } BOOL CFrameWnd::PreCreateWindow() { cout<<"CFrameWnd::PreCreateWnd"<<endl; return TRUE; } CWinApp* AfxGetApp() { return theApp.m_pCurrentWinApp; } //MY.cpp #include "MY.h" CMyWinApp theApp;//global object BOOL CMyWinApp::InitInstance() { cout<<"CMyWinApp::InitInstance"<<endl; this->m_pMainWnd = new CMyFrameWnd; return TRUE; } CMyFrameWnd::CMyFrameWnd() { cout<<"CMyFrameWnd::CMyFrameWnd"<<endl; Create(); } void main() { CWinApp* pApp = AfxGetApp(); pApp->InitApplication(); pApp->InitInstance(); pApp->Run(); }