友情提示:如果本网页打开太慢或显示不完整,请尝试鼠标右键“刷新”本网页!
深入浅出MFC第2版(PDF格式)-第24部分
快捷操作: 按键盘上方向键 ← 或 → 可快速上下翻页 按键盘上的 Enter 键可回到本书目录页 按键盘上方向键 ↑ 可回到本页顶部! 如果本书没有阅读完,想下次继续接着阅读,可使用上方 "收藏到我的浏览器" 功能 和 "加入书签" 功能!
#0020 CRuntimeClass* m_pBaseClass;
#0021
#0022 // CRuntimeClass objects linked together in simple list
#0023 static CRuntimeClass* pFirstClass; // start of class list
#0024 CRuntimeClass* m_pNextClass; // linked list of registered classes
#0025 };
#0026
#0027 struct AFX_CLASSINIT
#0028 { AFX_CLASSINIT(CRuntimeClass* pNewClass); };
#0029
#0030 #define RUNTIME_CLASS(class_name)
#0031 (&class_name::class##class_name)
#0032
#0033 #define DECLARE_DYNAMIC(class_name)
#0034 public:
#0035 static CRuntimeClass class##class_name;
#0036 virtual CRuntimeClass* GetRuntimeClass() const;
#0037
#0038 #define _IMPLEMENT_RUNTIMECLASS(class_name; base_class_name; wSchema; pfnNew)
#0039 static char _lpsz##class_name'' = #class_name;
#0040 CRuntimeClass class_name::class##class_name = {
#0041 _lpsz##class_name; sizeof(class_name); wSchema; pfnNew;
#0042 RUNTIME_CLASS(base_class_name); NULL };
#0043 static AFX_CLASSINIT _init_##class_name(&class_name::class##class_name);
#0044 CRuntimeClass* class_name::GetRuntimeClass() const
#0045 { return &class_name::class##class_name; }
#0046
#0047 #define IMPLEMENT_DYNAMIC(class_name; base_class_name)
#0048 _IMPLEMENT_RUNTIMECLASS(class_name; base_class_name; 0xFFFF; NULL)
#0049
133
…………………………………………………………Page 196……………………………………………………………
第篇 勿在浮砂築高台
#0050 class CObject
#0051 {
#0052 public:
#0053 CObject::CObject() {
#0054 }
#0055 CObject::~CObject() {
#0056 }
#0057
#0058 virtual CRuntimeClass* GetRuntimeClass() const;
#0059
#0060 public:
#0061 static CRuntimeClass classCObject;
#0062 };
#0063
#0064 class CCmdTarget : public CObject
#0065 {
#0066 DECLARE_DYNAMIC(CCmdTarget)
#0067 public:
#0068 CCmdTarget::CCmdTarget() {
#0069 }
#0070 CCmdTarget::~CCmdTarget() {
#0071 }
#0072 };
#0073
#0074 class CWinThread : public CCmdTarget
#0075 {
#0076 DECLARE_DYNAMIC(CWinThread)
#0077 public:
#0078 CWinThread::CWinThread() {
#0079 }
#0080 CWinThread::~CWinThread() {
#0081 }
#0082
#0083 virtual BOOL InitInstance() {
#0084 return TRUE;
#0085 }
#0086 virtual int Run() {
#0087 return 1;
#0088 }
#0089 };
#0090
#0091 class CWnd;
#0092
#0093 class CWinApp : public CWinThread
#0094 {
#0095 DECLARE_DYNAMIC(CWinApp)
134
…………………………………………………………Page 197……………………………………………………………
第3章 MFC 六大關鍵技術之模擬
#0096 public:
#0097 CWinApp* m_pCurrentWinApp;
#0098 CWnd* m_pMainWnd;
#0099
#0100 public:
#0101 CWinApp::CWinApp() {
#0102 m_pCurrentWinApp = this;
#0103 }
#0104 CWinApp::~CWinApp() {
#0105 }
#0106
#0107 virtual BOOL InitApplication() {
#0108 return TRUE;
#0109 }
#0110 virtual BOOL InitInstance() {
#0111 return TRUE;
#0112 }
#0113 virtual int Run() {
#0114 return CWinThread::Run();
#0115 }
#0116 };
#0117
#0118 class CDocument : public CCmdTarget
#0119 {
#0120 DECLARE_DYNAMIC(CDocument)
#0121 public:
#0122 CDocument::CDocument() {
#0123 }
#0124 CDocument::~CDocument() {
#0125 }
#0126 };
#0127
#0128 class CWnd : public CCmdTarget
#0129 {
#0130 DECLARE_DYNAMIC(CWnd)
#0131 public:
#0132 CWnd::CWnd() {
#0133 }
#0134 CWnd::~CWnd() {
#0135 }
#0136
#0137 virtual BOOL Create();
#0138 BOOL CreateEx();
#0139 virtual BOOL PreCreateWindow();
#0140 };
#0141
135
…………………………………………………………Page 198……………………………………………………………
第篇 勿在浮砂築高台
#0142 class CFrameWnd : public CWnd
#0143 {
#0144 DECLARE_DYNAMIC(CFrameWnd)
#0145 public:
#0146 CFrameWnd::CFrameWnd() {
#0147 }
#0148 CFrameWnd::~CFrameWnd() {
#0149 }
#0150 BOOL Create();
#0151 virtual BOOL PreCreateWindow();
#0152 };
#0153
#0154 class CView : public CWnd
#0155 {
#0156 DECLARE_DYNAMIC(CView)
#0157 public:
#0158 CView::CView() {
#0159 }
#0160 CView::~CView() {
#0161 }
#0162 };
#0163
#0164
#0165 // global function
#0166 CWinApp* AfxGetApp();
MFC。CPP
#0001 #include 〃my。h〃 //
原该包含mfc。h 就好,但为了CMyWinApp 的定义,所以。。。
#0002
#0003 extern CMyWinApp theApp;
#0004
#0005 static char szCObject'' = 〃CObject〃;
#0006 struct CRuntimeClass CObject::classCObject =
#0007 { szCObject; sizeof(CObject); 0xffff; NULL; NULL };
#0008 static AFX_CLASSINIT _init_CObject(&CObject::classCObject);
#0009
#0010 CRuntimeClass* CRuntimeClass::pFirstClass = NULL;
#0011
#0012 AFX_CLASSINIT::AFX_CLASSINIT(CRuntimeClass* pNewClass)
#0013 {
#0014 pNewClass…》m_pNextClass = CRuntimeClass::pFirstClass;
#0015 CRuntimeClass::pFirstClass = pNewClass;
#0016 }
#0017
#0018 CRuntimeClass* CObject::GetRuntimeClass() const
136
…………………………………………………………Page 199……………………………………………………………
第3章 MFC 六大關鍵技術之模擬
#0019 {
#0020 return &CObject::classCObject;
#0021 }
#0022
#0023 BOOL CWnd::Create()
#0024 {
#0025 return TRUE;
#0026 }
#0027
#0028 BOOL CWnd::CreateEx()
#0029 {
#0030 PreCreateWindow();
#0031 return TRUE;
#0032 }
#0033
#0034 BOOL CWnd::PreCreateWindow()
#0035 {
#0036 return TRUE;
#0037 }
#0038
#0039 BOOL CFrameWnd::Create()
#0040 {
#0041 CreateEx();
#0042 return TRUE;
#0043 }
#0044
#0045 BOOL CFrameWnd::PreCreateWindow()
#0046 {
#0047 return TRUE;
#0048 }
#0049
#0050 IMPLEMENT_DYNAMIC(CCmdTarget; CObject)
#0051 IMPLEMENT_DYNAMIC(CWinThread; CCmdTarget)
#0052 IMPLEMENT_DYNAMIC(CWinApp; CWinThread)
#0053 IMPLEMENT_DYNAMIC(CWnd; CCmdTarget)
#0054 IMPLEMENT_DYNAMIC(CFrameWnd; CWnd)
#0055 IMPLEMENT_DYNAMIC(CDocument; CCmdTarget)
#0056 IMPLEMENT_DYNAMIC(CView; CWnd)
#0057
#0058 // global function
#0059 CWinApp* AfxGetApp()
#0060 {
#0061 return theApp。m_pCurrentWinApp;
#0062 }
137
…………………………………………………………Page 200……………………………………………………………
第篇 勿在浮砂築高台
MY。H
#0001 #include
#0002 #include 〃mfc。h〃
#0003
#0004 class CMyWinApp : public CWinApp
#0005 {
#0006 public:
#0007 CMyWinApp::CMyWinApp() {
#0008 }
#0009 CMyWinApp::~CMyWinApp() {
#0010 }
#0011
#0012 virtual BOOL InitInstance();
#0013 };
#0014
#0015 class CMyFrameWnd : public CFrameWnd
#0016 {
#0017 public:
#0018 CMyFrameWnd();
#0019 ~CMyFrameWnd() {
#0020 }
#0021 };
#0022
#0023
#0024 class CMyDoc : public CDocument
#0025 {
#0026 public:
#0027 CMyDoc::CMyDoc() {
#0028 }
#0029 CMyDoc::~CMyDoc() {
#0030 }
#0031 };
#0032
#0033 class CMyView : public CView
#0034 {
#0035 public:
#0036 CMyView::CMyView() {
#0037 }
#0038 CMyView::~CMyView() {
#0039 }
#0040 };
#0041
#0042 // global function
#0043 void PrintAllClasses();
138
…………………………………………………………Page 201……………………………………………………………
第3章 MFC 六大關鍵技術之模擬
MY。CPP
#0001 #include 〃my。h〃
#0002
#0003 CMyWinApp theApp;
#0004
#0005 BOOL CMyWinApp::InitInstance()
#0006 {
#0007 m_pMainWnd = new CMyFrameWnd;
#0008 return TRUE;
#0009 }
#0010
#0011 CMyFrameWnd::CMyFrameWnd()
#0012 {
#0013 Create();
#0014 }
#0015
#0016 void PrintAllClasses()
#0017 {
#0018 CRuntimeClass* pClass;
#0019
#0020 // just walk through the simple list of registered classes
#0021 for (pClass = CRuntimeClass::pFirstClass; pClass != NULL;
#0022 pClass = pClass…》m_pNextClass)
#0023 {
#0024 cout m_lpszClassName Run();
#0039
#0040 PrintAllClasses();
#0041 }
139
…………………………………………………………Page 202……………………………………………………………
第篇 勿在浮砂築高台
IsKindOf (型别辨识)
有了图3…1 这张「类别型录」网,要实现IsKindOf 功能,再轻松不过了:
1。 为CObject 加上一个IsKindOf 函数,于是此函数将被所有类别继承。它将把
参数所指定的某个CRuntimeClass 对象拿来与类别型录中的元素一一比对。比
对成功(在型录中有发现),就传回TRUE,否则传回FALSE :
// in header file
class CObject
{
public:
。。。
BOOL IsKindOf(const CRuntimeClass* pClass) const;
};
// in implementation file
BOOL CObject::IsKindOf(const CRuntimeClass* pClass) const
{
CRuntimeClass* pClassThis = GetRuntimeClass();
while (pClassThis != NULL)
{
if (pClassThis == pClass)
return TRUE;
pClassThis = pClassThis…》m_pBaseClass;
}
return FALSE; // walked to the top; no match
}
注意,while 循环中所追踪的是「同宗」路线,也就是凭借着m_pBaseClass 而非
m_pNextClass。假设我们的调用是:
CView* pView = new CView;
pView…》IsKindOf(RUNTIME_CLASS(CWinApp));
IsKindOf 的参数其实就是&CWinApp::classCWinApp 。函数内利用GetRuntimeClass
快捷操作: 按键盘上方向键 ← 或 → 可快速上下翻页 按键盘上的 Enter 键可回到本书目录页 按键盘上方向键 ↑ 可回到本页顶部!
温馨提示: 温看小说的同时发表评论,说出自己的看法和其它小伙伴们分享也不错哦!发表书评还可以获得积分和经验奖励,认真写原创书评 被采纳为精评可以获得大量金币、积分和经验奖励哦!