孟源的专栏

msdn中居然找不到CDocManager类的说明,网上找到这篇文档,有待分析。首先有个不明白的地方,我的书上说CDocManager类是不公开的,可是我还是找到了他的类定义和实现,真搞不懂她这个不公开是什么意思???

CDocManager的定义如下:

class CDocManager : public CObject{DECLARE_DYNAMIC(CDocManager)public:

// ConstructorCDocManager();

//Document functionsvirtual 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 filevirtual BOOL SaveAllModified(); // save before exitvirtual void CloseAllDocuments(BOOL bEndSession); // close documents before exitingvirtual int GetOpenDocumentCount();

// helper for standard commdlg dialogsvirtual BOOL DoPromptFileName(CString& fileName, UINT nIDSTitle,DWORD lFlags, BOOL bOpenFileDialog, CDocTemplate* pTemplate);

//Commands// Advanced: process async DDE requestvirtual BOOL OnDDECommand(LPTSTR lpszCommand);virtual void OnFileNew();virtual void OnFileOpen();

// Implementationprotected:CPtrList m_templateList;int GetDocumentCount(); // helper to count number of total documents

public:static CPtrList* pStaticList; // for static CDocTemplate objectsstatic BOOL bStaticInit; // TRUE during static initializationstatic CDocManager* pStaticDocManager; // for static CDocTemplate objects

public:virtual ~CDocManager();#ifdef _DEBUGvirtual 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 frameRUNTIME_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 listpTemplate->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();}

人言未必皆真,听言只听三分。

孟源的专栏

相关文章:

你感兴趣的文章:

标签云: