欢迎访问:常州市武进区嘉泽中心小学网站 !今天是:
栏目列表
您现在的位置是:首页>>教师>>计算机技术>>程序设计>>杂项>>文章内容
多模板文档视图结构的应用
发布时间:2008-11-20   点击:   来源:本站原创   录入者:佚名
 

 

FMD STUDIO 之 VC++编程 之 技术篇 之 : 【回上一页】 编号 主  题 来 源 收录时间 5 多模板文档视图结构的应用 自撰 00.05.08

多模板文档视图结构的应用

一、概述

①在一个MDI程序中,需要使用到不同类型的子窗口,而每种类型窗口有可能有很多个,对应不同的数据。

这时,可以使用多模板的文档视图结构。以利用文档视图结构带来的便利。

②建立某种类型的MDI子窗口时,调用对应文档模板的OpenDocumentFile()

而其参数LPCTSTR lpszFileName可以作为建立MDI窗口时的参数

③在使用过程中,文档对象不一定要对应磁盘文件,而是根据需要定义成内部的数据结构。

但MFC文档结构中,很多地方与实际文件相联系,因此,使用中,有些细节需处理。

二、多文档文档模板的建立

①在应用程序类中建立多个文档模板指针作为成员数据

例:

CMultiDocTemplate * pCalcDocTemplate;

CMultiDocTemplate * pDataDocTemplate;

②在应用程序类InitInstance中建立多个文档模板对象。

每种文档模板对应不同的文档、视图类组合。

例:

 pCalcDocTemplate = new CMultiDocTemplate(

  IDR_VFPTYPE,

  RUNTIME_CLASS(CVFPDoc),

  RUNTIME_CLASS(CChildFrame), // custom MDI child frame

  RUNTIME_CLASS(CVFPView));

 AddDocTemplate(pCalcDocTemplate);

 pDataDocTemplate = new CMultiDocTemplate(

  IDR_DATATYPE,

  RUNTIME_CLASS(CDataDoc),

  RUNTIME_CLASS(CDataChildFrame), // custom MDI child frame

  RUNTIME_CLASS(CDataListView));

 AddDocTemplate(pDataDocTemplate);

③在应用程序类中添加建立窗口的成员。

例:

CDocument * CXXXApp::OpenDataDocumentFile(LPCTSTR lpszFileName)

{

 if(pDataDocTemplate)

  return pDataDocTemplate-> OpenDocumentFile(lpszFileName);

 else

  return NULL;

}

CDocument * CXXXApp::OpenCalcDocumentFile(LPCTSTR lpszFileName)

{

 if(pCalcDocTemplate)

  return pCalcDocTemplate-> OpenDocumentFile(lpszFileName);

 else

  return NULL;

}

④需要建立不同类型的MDI子窗口时,调用以上成员即可

三、细节处理

①避免同一参数对应的MDI子窗口多次打开。

在处理磁盘文件时,打开某一文件时,文档管理器会检查是否打开过,打开过,则激活已经打开的窗口。

当自己调用文档模板的OpenDocumentFile打开时,这一检查被绕过了。

但需要时,可以在自己的打开"文件"成员函数中添加相应代码(基本上是MFC原码的复制)。

例:以上的OpenCalcDocumentFile可更改为:

CDocument * CXXXApp::OpenCalcDocumentFile(LPCTSTR lpszFileName)

{

 if(pCalcDocTemplate)

 {

  CDocument* pOpenDocument = NULL;

  pCalcDocTemplate-> MatchDocType(lpszFileName, pOpenDocument); //判断是否lpszFileName的窗口已经建立

  if (pOpenDocument != NULL)  //若已经建立过,则激活

  {

   POSITION pos = pOpenDocument-> GetFirstViewPosition();

   if (pos != NULL)

   {

    CView* pView = pOpenDocument-> GetNextView(pos); // get first one

    ASSERT_VALID(pView);

    CFrameWnd* pFrame = pView-> GetParentFrame();

    if (pFrame != NULL)

     pFrame-> ActivateFrame();

    else

     TRACE0("Error: Can not find a frame for document to activate.\n");

    CFrameWnd* pAppFrame;

    if (pFrame != (pAppFrame = (CFrameWnd*)AfxGetApp()-> m_pMainWnd))

    {

     ASSERT_KINDOF(CFrameWnd, pAppFrame);

     pAppFrame-> ActivateFrame();

    }

   }

   else

   {

    TRACE0("Error: Can not find a view for document to activate.\n");

   }

   return pOpenDocument;

  }

  else  //否则直接打开

   return pCalcDocTemplate-> OpenDocumentFile(lpszFileName);

  

 }

 else

  return NULL;

}

但改写后,发现某些情况下窗口还是被重复打开。原因是MatchDocType判断失误。

在MatchDocType中,需要比较文档对象中保存的文件名和输入的文档名参数。

但文档对象建立时,会对文档名取全路径,而以上作为参数的"文件名"被认为是相对路径而添加了目录名。比较时自然不相等了。

好在MFC在设计时已经考虑了这些,文档类提供了虚函数SetPathName(LPCTSTR lpszPathName, BOOL bAddToMRU),

在自己的文档类中,重载SetPathName,屏蔽其缺省实现,更改为:

void CXXXDoc::SetPathName(LPCTSTR lpszPathName, BOOL bAddToMRU) 

{

 m_strPathName = lpszPathName; //直接将传入名作为最后的文档名

 m_bEmbedded = FALSE; //模式(不是嵌入对象)

 SetTitle(lpszPathName); //设置标题(可具体更改)

}

当实际使用时,对每个文档模板,其OpenDocumentFile对要添加很多代码,很不方便, 这是,可以将共用部分建立成成员函数

例:

CDocument * CXXXApp::OpenWithTemplate(CMultiDocTemplate * pDocTemplate,LPCTSTR lpszFileName)

{

 if(pDocTemplate)

 {

  CDocument* pOpenDocument = NULL;

  pDocTemplate-> MatchDocType(lpszFileName, pOpenDocument); //避免重复打开

  if (pOpenDocument != NULL)

  {

   POSITION pos = pOpenDocument-> GetFirstViewPosition();

   if (pos != NULL)

   {

    CView* pView = pOpenDocument-> GetNextView(pos); // get first one

    ASSERT_VALID(pView);

    CFrameWnd* pFrame = pView-> GetParentFrame();

    if (pFrame != NULL)

     pFrame-> ActivateFrame();

    else

     TRACE0("Error: Can not find a frame for document to activate.\n");

    CFrameWnd* pAppFrame;

    if (pFrame != (pAppFrame = (CFrameWnd*)AfxGetApp()-> m_pMainWnd))

    {

     ASSERT_KINDOF(CFrameWnd, pAppFrame);

     pAppFrame-> ActivateFrame();

    }

   }

   else

   {

    TRACE0("Error: Can not find a view for document to activate.\n");

   }

   return pOpenDocument;

  }

  else

   return pDocTemplate-> OpenDocumentFile(lpszFileName);

  

 }

 else

  return NULL;

}


回上一页


附件:
    关闭窗口
    打印文档
    账号登录
    保持登录 忘记密码?
    账号与武进教师培训平台同步