首先有个不明白的地方,我的书上说CDocManager类是不公开的,可是我还是找到了他的类定义和实现,真搞不懂她这个不公开是什么意思??? CDocManager的定义如下: class CDocManager : public CObject { DECLARE_DYNAMIC(CDocManager) public: // Constructor CDocManager(); //Document functions virtual void AddDocTemplate(CDocTemplate* pTemplate); virtual POSITION GetFirstDocTemplatePosition() const; virtual CDocTemplate* GetNextDocTemplate(POSITION& pos) const; virtual void RegisterShellFileTypes(BOOL bCompat); void UnregisterShellFileTypes(); virtual CDocument* OpenDocumentFile(LPCTSTR lpszFileName); // open named file virtual BOOL SaveAllModified(); // save before exit virtual void CloseAllDocuments(BOOL bEndSession); // close documents before exiting virtual int GetOpenDocumentCount(); // helper for standard commdlg dialogs virtual BOOL DoPromptFileName(CString& fileName, UINT nIDSTitle, DWORD lFlags, BOOL bOpenFileDialog, CDocTemplate* pTemplate); //Commands // Advanced: process async DDE request virtual BOOL OnDDECommand(LPTSTR lpszCommand); virtual void OnFileNew(); virtual void OnFileOpen(); // Implementation protected: CPtrList m_templateList; int GetDocumentCount(); // helper to count number of total documents public: static CPtrList* pStaticList; // for static CDocTemplate objects static BOOL bStaticInit; // TRUE during static initialization static CDocManager* pStaticDocManager; // for static CDocTemplate objects public: virtual ~CDocManager(); #ifdef _DEBUG virtual void AssertValid() const; virtual void Dump(CDumpContext& dc) const; #endif }; 就变量而言,这个类只有四个变量,分别是CPtrList m_templateList,static CPtrList* pStaticList,static BOOL bStaticInit,static CDocManager* pStaticDocManager后面三个都是静态的,目前没有搞明白这三个成员变量是干什么的??在网上找了些资料,谈到这一点的时候,都说无关紧要,恩,这个是下次心情好时研究的对像。 最重要的变量是CPtrList m_templateList,在普通的mfc程序中的InitInstance()函数中都有一条语句(这里以CMultiDocTemplate为例): CMultiDocTemplate* pDocTemplate; pDocTemplate = new CMultiDocTemplate( nIDResource, RUNTIME_CLASS(pDocClass), RUNTIME_CLASS(pFrameClass), // custom MDI child frame RUNTIME_CLASS(pViewClass)): AddDocTemplate(pDocTemplate); 正是这个AddDocTemplate(pDocTemplate)函数,看起来好像是一个CWinApp的函数,实际上CWinApp::AddDocTemplate(pDocTemplate)调用了上面的CDocManager ::AddDocTemplate(CDocTemplate* pTemplate)。如下: void CWinApp::AddDocTemplate(CDocTemplate* pTemplate) { if (m_pDocManager == NULL) m_pDocManager = new CDocManager; m_pDocManager->AddDocTemplate(pTemplate); } 这里函数里面似乎有个不知出处的m_pDocManager ,呵呵,这个我稍候提出,先来看看m_pDocManager->AddDocTemplate(pTemplate)到底是干什么的??如下: void CDocManager::AddDocTemplate(CDocTemplate* pTemplate) { if (pTemplate == NULL) { if (pStaticList != NULL) { POSITION pos = pStaticList->GetHeadPosition(); while (pos != NULL) { CDocTemplate* pTemplate = (CDocTemplate*)pStaticList->GetNext(pos); AddDocTemplate(pTemplate); } delete pStaticList; pStaticList = NULL; } bStaticInit = FALSE; } else { ASSERT_VALID(pTemplate); ASSERT(m_templateList.Find(pTemplate, NULL) == NULL);// must not be in list pTemplate->LoadTemplate(); m_templateList.AddTail(pTemplate); } } 可以看出它主要的功能是m_templateList.AddTail(pTemplate),就是说把这个加入到他的内部变量m_templateList中。简单的说,CDocManager::m_templateList拥有程序中所有的模板。上面提到的那一个m_pDocManager 实际上是一个CWinApp的成员变量,在CWinApp的构造函数中初始化为NULL。实际上整个程序中也就是他拥有哪个模板的集合。 在仔细看一下CDocManager提供的函数,会发现很多熟悉的,如OnFileNew()等,实际CWinApp::OnFileNew和CWinApp::OnFileOpen都是通过调用CDocManager的对应函数实现的。如下: void CWinApp::OnFileNew() { if (m_pDocManager != NULL) m_pDocManager->OnFileNew(); } void CWinApp::OnFileOpen() { ASSERT(m_pDocManager != NULL); m_pDocManager->OnFileOpen(); } 说了这么多,那么CDocManager类到底发挥了什么作用的??正如前面所说,由于在CWinApp中有一个CDocManager对象,而CDocManager保存了程序中的所有模板,那就是说CDocManager是CWinApp和CDocTemplate沟通的桥梁。 
|