深入浅出MFC文档/视图架构之文档模板(2)
发布时间:2021-07-06 05:51:49 所属栏目:大数据 来源: https://blog.csdn.net/dragonf
导读:? 2.文档模板类CDocTemplate 文档模板类CDocTemplate是一个抽象基类(这意味着不能直接用它来定义对象而必须用它的派生类),它定义了文档模板的基本处理函数接口。对一个单文档界面程序,需使用单文档模板类CSingleDocTemplate,而对于一个多文档界面程序,
?
|
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,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* 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还保存了它所支持的全部文档类的信息,包括所支持文档的文件扩展名、文档在框架窗口中的名字、图标等。
(编辑:北几岛)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!