深入浅出MFC第2版(PDF格式)-第36部分
按键盘上方向键 ← 或 → 可快速上下翻页,按键盘上的 Enter 键可回到本书目录页,按键盘上方向键 ↑ 可回到本页顶部!
————未阅读完?加入书签已便下次继续阅读!
#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 的参数其实就是&CWi