首页 新闻 论坛 群组 Blog 文档 下载 读书 Tag 网摘 搜索 .NET Java 游戏 视频 人才 外包 培训 数据库 书店 程序员
中国软件网
欢迎您:游客 | 登录 注册 帮助
  • (转)深入浅出MFC文档/视图架构之文档模板
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • arbiterrr
    • 等级:
    • 可用分等级:
    • 总技术专家分:
    • 总技术专家分排名:
    • 揭帖率:
    发表于:2007-10-09 01:58:03 楼主
    作者: 宋宝华 出处: 天极开发 责任编辑:方舟


    2.文档模板类CDocTemplate

      文档模板类CDocTemplate是一个抽象基类(这意味着不能直接用它来定义对象而必须用它的派生类),它定义了文档模板的基本处理函数接口。对一个单文档界面程序,需使用单文档模板类CSingleDocTemplate,而对于一个多文档界面程序,需使用多文档模板类CMultipleDocTemplate。我们首先来看看CDocTemplate类的声明:

    class CDocTemplate : public CCmdTarget
    {
     DECLARE_DYNAMIC(CDocTemplate)

     // Constructors
     protected:
      CDocTemplate(UINT nIDResource, CRuntimeClass* pDocClass, CRuntimeClass* pFrameClass, CRuntimeClass* pViewClass);

     public:
      virtual void LoadTemplate();

      // Attributes
     public:
      // setup for OLE containers
      void SetContainerInfo(UINT nIDOleInPlaceContainer);

      // setup for OLE servers
      void SetServerInfo(UINT nIDOleEmbedding, UINT nIDOleInPlaceServer = 0,
    CRuntimeClass* pOleFrameClass = NULL, CRuntimeClass* pOleViewClass = NULL);

      // iterating over open documents
      virtual POSITION GetFirstDocPosition() const = 0;
      virtual CDocument* GetNextDoc(POSITION& rPos) const = 0;

      // Operations
     public:
      virtual void AddDocument(CDocument* pDoc); // must override
      virtual void RemoveDocument(CDocument* pDoc); // must override
      enum DocStringIndex
      {
       windowTitle, // default window title
       docName, // user visible name for default document
       fileNewName, // user visible name for FileNew
       // for file based documents:
       filterName, // user visible name for FileOpen
       filterExt, // user visible extension for FileOpen
       // for file based documents with Shell open support:
       regFileTypeId, // REGEDIT visible registered file type identifier
       regFileTypeName, // Shell visible registered file type name
      };
      virtual BOOL GetDocString(CString& rString,
    enum DocStringIndex index) const; // get one of the info strings
      CFrameWnd* CreateOleFrame(CWnd* pParentWnd, CDocument* pDoc,BOOL bCreateView);

      // Overridables
     public:
      enum Confidence
      {
       noAttempt,
       maybeAttemptForeign,
       maybeAttemptNative,
       yesAttemptForeign,
       yesAttemptNative,
       yesAlreadyOpen
      };
      virtual Confidence MatchDocType(LPCTSTR lpszPathName,CDocument*& rpDocMatch);
      virtual CDocument* CreateNewDocument();
      virtual CFrameWnd* CreateNewFrame(CDocument* pDoc, CFrameWnd* pOther);
      virtual void InitialUpdateFrame(CFrameWnd* pFrame, CDocument* pDoc,BOOL bMakeVisible = TRUE);
      virtual BOOL SaveAllModified(); // for all documents
      virtual void CloseAllDocuments(BOOL bEndSession);
      virtual CDocument* OpenDocumentFile(
       LPCTSTR lpszPathName, BOOL bMakeVisible = TRUE) = 0;
       // open named file
       // if lpszPathName == NULL => create new file with this type
      virtual void SetDefaultTitle(CDocument* pDocument) = 0;

      // Implementation
     public:
      BOOL m_bAutoDelete;
      virtual ~CDocTemplate();

      // back pointer to OLE or other server (NULL if none or disabled)
      CObject* m_pAttachedFactory;

      // menu & accelerator resources for in-place container
      HMENU m_hMenuInPlace;
      HACCEL m_hAccelInPlace;

      // menu & accelerator resource for server editing embedding
      HMENU m_hMenuEmbedding;
      HACCEL m_hAccelEmbedding;

      // menu & accelerator resource for server editing in-place
      HMENU m_hMenuInPlaceServer;
      HACCEL m_hAccelInPlaceServer;

      #ifdef _DEBUG
       virtual void Dump(CDumpContext&) const;
       virtual void AssertValid() const;
      #endif
      virtual void OnIdle(); // for all documents
      virtual BOOL OnCmdMsg(UINT nID, int nCode, void* pExtra,AFX_CMDHANDLERINFO* pHandlerInfo);

     protected:
      UINT m_nIDResource; // IDR_ for frame/menu/accel as well
      UINT m_nIDServerResource; // IDR_ for OLE inplace frame/menu/accel
      UINT m_nIDEmbeddingResource; // IDR_ for OLE open frame/menu/accel
      UINT m_nIDContainerResource; // IDR_ for container frame/menu/accel

      CRuntimeClass* m_pDocClass; // class for creating new documents
      CRuntimeClass* m_pFrameClass; // class for creating new frames
      CRuntimeClass* m_pViewClass; // class for creating new views
      CRuntimeClass* m_pOleFrameClass; // class for creating in-place frame
      CRuntimeClass* m_pOleViewClass; // class for creating in-place view

      CString m_strDocStrings; // '\n' separated names
      // The document names sub-strings are represented as _one_ string:
      // windowTitle\ndocName\n ... (see DocStringIndex enum)
    };

      文档模板挂接了后面要介绍的文档、视图和框架窗口,使得它们得以互相关联。通过文档模板,程序确定了创建或打开一个文档时,以什么样的视图和框架窗口来显示。文档模板依靠保存相互对应的文档、视图和框架窗口的CRuntimeClass对象指针来实现上述挂接,这就是文档模板类中的成员变量m_pDocClass、m_pFrameClass、m_pViewClass的由来。实际上,对m_pDocClass、m_pFrameClass、m_pViewClass的赋值在CDocTemplate类的构造函数中实施:

    CDocTemplate::CDocTemplate(UINT nIDResource, CRuntimeClass* pDocClass,
    CRuntimeClass* pFrameClass, CRuntimeClass* pViewClass)
    {
     ASSERT_VALID_IDR(nIDResource);
     ASSERT(pDocClass == NULL || pDocClass->IsDerivedFrom(RUNTIME_CLASS(CDocument)));
     ASSERT(pFrameClass == NULL ||pFrameClass->IsDerivedFrom(RUNTIME_CLASS(CFrameWnd)));
     ASSERT(pViewClass == NULL || pViewClass->IsDerivedFrom(RUNTIME_CLASS(CView)));

     m_nIDResource = nIDResource;
     m_nIDServerResource = NULL;
     m_nIDEmbeddingResource = NULL;
     m_nIDContainerResource = NULL;

     m_pDocClass = pDocClass;
     m_pFrameClass = pFrameClass;
     m_pViewClass = pViewClass;
     m_pOleFrameClass = NULL;
     m_pOleViewClass = NULL;

     m_pAttachedFactory = NULL;
     m_hMenuInPlace = NULL;
     m_hAccelInPlace = NULL;
     m_hMenuEmbedding = NULL;
     m_hAccelEmbedding = NULL;
     m_hMenuInPlaceServer = NULL;
     m_hAccelInPlaceServer = NULL;

     // add to pStaticList if constructed as static instead of on heap
     if (CDocManager::bStaticInit)
     {
      m_bAutoDelete = FALSE;
      if (CDocManager::pStaticList == NULL)
       CDocManager::pStaticList = new CPtrList;
      if (CDocManager::pStaticDocManager == NULL)
       CDocManager::pStaticDocManager = new CDocManager;
       CDocManager::pStaticList->AddTail(this);
     }
     else
     {
      m_bAutoDelete = TRUE; // usually allocated on the heap
      LoadTemplate();
     }
    }

      文档模板类CDocTemplate还保存了它所支持的全部文档类的信息,包括所支持文档的文件扩展名、文档在框架窗口中的名字、图标等。
    0  修改 删除 举报 引用 回复

    网站简介广告服务网站地图帮助联系方式诚聘英才English 问题报告
    北京创新乐知广告有限公司 版权所有 京 ICP 证 070598 号
    世纪乐知(北京)网络技术有限公司 提供技术支持
    Copyright © 2000-2008, CSDN.NET, All Rights Reserved