MFC.h
#ifndef __MFC_H_
#define __MFC_H_
#define BOOL int
#define TRUE 1
#define FALSE 0
#define LPCSTR LPSTR
typedef char* LPSTR;
#define UINT int
#define PASCAL _stdcall
#include <iostream>
class CObject;
struct CRuntimeClass {
LPCSTR m_lpszClassName;
int m_nObjectSize;
UINT m_wSchema;
CObject* (PASCAL* m_pfnCreateObject) ();
CRuntimeClass* m_pBaseClass;
static CRuntimeClass* pFirstClass;
CRuntimeClass* m_pNextClass;
};
struct AFX_CLASSINIT {
AFX_CLASSINIT(CRuntimeClass* pNewClass);
};
#define RUNTIME_CLASS(class_name) \
(&class_name::class##class_name)
#define DECLARE_DYNAMIC(class_name) \
public: \
static CRuntimeClass class##class_name; \
virtual CRuntimeClass* GetRuntimeClass() const;
#define _IMPLEMENT_RUNTIMECLASS(class_name, base_class_name, wSchema, pfnNew) \
static char _lpsz##class_name[] = #class_name; \
CRuntimeClass class_name::class##class_name = { \
_lpsz##class_name, sizeof(class_name), wSchema, pfnNew, \
RUNTIME_CLASS(base_class_name), NULL}; \
static AFX_CLASSINIT _init_##class_name(&class_name::class##class_name); \
CRuntimeClass* class_name::GetRuntimeClass() const \
{ \
return &class_name::class##class_name; \
}
#define IMPLEMENT_DYNAMIC(class_name, base_class_name) \
_IMPLEMENT_RUNTIMECLASS(class_name, base_class_name, 0XFFFF, NULL)
class CObject {
public:
CObject() {}
~CObject() {}
virtual CRuntimeClass* GetRuntimeClass() const;
static CRuntimeClass classCObject;
};
class CCmdTarget : public CObject {
DECLARE_DYNAMIC(CCmdTarget)
public:
CCmdTarget() {}
~CCmdTarget() {}
};
class CWinThread : public CCmdTarget {
DECLARE_DYNAMIC(CWinThread)
public:
CWinThread() {}
~CWinThread() {}
virtual BOOL InitInstance() {
return TRUE;
}
virtual int Run() {
return 1;
}
};
class CWnd;
class CWinApp : public CWinThread {
DECLARE_DYNAMIC(CWinApp)
public:
CWinApp* m_pCurrentWinApp;
CWnd* m_pMainWnd;
public:
CWinApp() {
m_pCurrentWinApp = this;
}
~CWinApp() {}
virtual BOOL InitApplication() {
return TRUE;
}
virtual BOOL InitInstance() {
return TRUE;
}
virtual int Run() {
return CWinThread::Run();
}
};
class CDocument : public CCmdTarget {
DECLARE_DYNAMIC(CDocument)
public:
CDocument::CDocument() {
}
~CDocument() {}
};
class CWnd : public CCmdTarget {
DECLARE_DYNAMIC(CWnd)
public:
CWnd() {}
~CWnd() {}
virtual BOOL Create();
BOOL CreateEx();
virtual BOOL PreCreateWindow();
};
class CFrameWnd : public CWnd {
DECLARE_DYNAMIC(CFrameWnd)
public:
CFrameWnd() {}
~CFrameWnd() {}
BOOL Create();
virtual BOOL PreCreateWindow();
};
class CView : public CWnd {
DECLARE_DYNAMIC(CView)
public:
CView() {}
~CView() {}
};
CWinApp* AfxGetApp();
#endif
MFC.cpp
#include "my.h"
extern CMyWinApp theApp;
static char szCObject[] = "CObject";
struct CRuntimeClass CObject::classCObject = {
szCObject, sizeof(CObject), 0xffff, NULL, NULL, NULL};
static AFX_CLASSINIT _init_CObject(&CObject::classCObject);
CRuntimeClass* CRuntimeClass::pFirstClass = NULL;
AFX_CLASSINIT::AFX_CLASSINIT(CRuntimeClass* pNewClass) {
pNewClass->m_pNextClass = CRuntimeClass::pFirstClass;
CRuntimeClass::pFirstClass = pNewClass;
}
CRuntimeClass* CObject::GetRuntimeClass() const {
return &CObject::classCObject;
}
BOOL CWnd::Create() {
return TRUE;
}
BOOL CWnd::CreateEx() {
PreCreateWindow();
return TRUE;
}
BOOL CWnd::PreCreateWindow() {
return TRUE;
}
BOOL CFrameWnd::Create() {
CreateEx();
return TRUE;
}
BOOL CFrameWnd::PreCreateWindow() {
return TRUE;
}
IMPLEMENT_DYNAMIC(CCmdTarget, CObject)
IMPLEMENT_DYNAMIC(CWinThread, CCmdTarget)
IMPLEMENT_DYNAMIC(CWinApp, CWinThread)
IMPLEMENT_DYNAMIC(CWnd, CCmdTarget)
IMPLEMENT_DYNAMIC(CFrameWnd, CWnd)
IMPLEMENT_DYNAMIC(CDocument, CCmdTarget)
IMPLEMENT_DYNAMIC(CView, CWnd)
CWinApp* AfxGetApp() {
return theApp.m_pCurrentWinApp;
}
MY.h
#ifndef __MY_H_
#define __MY_H_
#include <IOSTREAM>
#include "MFC.H"
using namespace std;
class CMyFrameWnd:public CFrameWnd
{
public:
CMyFrameWnd();
~CMyFrameWnd(){}
};
class CMyWinApp:public CWinApp
{
public:
CMyWinApp(){}
~CMyWinApp(){}
virtual BOOL InitInstance();
};
class CMyDocument:public CDocument
{
public:
CMyDocument(){}
~CMyDocument(){}
};
class CMyView:public CView
{
public:
CMyView(){}
~CMyView(){}
};
void PrintAllClasses();
#endif
MY.cpp
#include "MY.h"
CMyWinApp theApp;
BOOL CMyWinApp::InitInstance() {
m_pMainWnd = new CMyFrameWnd;
return TRUE;
}
CMyFrameWnd::CMyFrameWnd() {
Create();
}
void PrintAllClasses() {
CRuntimeClass* pClass;
for(pClass = CRuntimeClass::pFirstClass; pClass != NULL;
pClass = pClass->m_pNextClass) {
std::cout << pClass->m_lpszClassName << std::endl;
std::cout << pClass->m_nObjectSize << std::endl;
std::cout << pClass->m_wSchema << std::endl;
}
}
int main() {
CWinApp* pApp = AfxGetApp();
pApp->InitApplication();
pApp->InitInstance();
pApp->Run();
PrintAllClasses();
return 0;
}
标签:MFC,name,CRuntimeClass,DYNAMIC,public,BOOL,IMPLEMENT,class
From: https://blog.51cto.com/u_13566975/7272455