中国IT动力,最新最全的IT技术教程
最新100篇 | 推荐100篇 | 专题100篇 | 排行榜 | 搜索 | 在线API文档
首 页 | 程序开发 | 操作系统 | 软件应用 | 图形图象 | 网络应用 | 精文荟萃 | 教育认证 | 硬件维护 | 未整理篇 | 站长教程
ASP JS PHP工程 ASP.NET 网站建设 UML J2EESUN .NET VC VB VFP 网络维护 数据库 DB2 SQL2000 Oracle Mysql
服务器 Win2000 Office C DreamWeaver FireWorks Flash PhotoShop 上网宝典 CorelDraw 协议大全 网络安全 微软认证
硬件维护  CPU  主板  硬盘  内存  显卡  显示器  键盘鼠标  声卡音箱  打印机  机箱电源  BIOS  网卡  C#  Java  Delphi  vs.net2005
  当前位置:> 程序开发 > 编程语言 > 综合其它
今天心情不错,对CDocManager和CWinApp作了一点粗劣的学习,写出来勉励自己。
作者:未知 时间:2005-07-27 23:24 出处:CSDN 责编:chinaitpower
              摘要:今天心情不错,对CDocManager和CWinApp作了一点粗劣的学习,写出来勉励自己。

        首先有个不明白的地方,我的书上说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沟通的桥梁。


关闭本页
 
首页 | 投资与合作 | 服务条款 | 隐私政策 | 收藏本站 | 设为首页 | 新用户注册 | 免责声明 | 使用帮助
Copyright ©2005-2008 chinaitpower.com All rights reserved. www.chinaitpower.com 版权所有