问题请教:mfc怎么样使用两个不同类型的文档视图啊,
如第一个为helloview.cpp,hellofram.cpp,hellodoc.cpp,第二个为ballview.doc,ballfram.cpp,balldoc.cpp这样在hello里面和ball里面做不同的事情。msdn里面有个example,工程名为mdi.dsw,讲多文档视图的,就是这个,
那位知道这个例子的框架怎么做的?多谢

解决方案 »

  1.   

    先在InitInstance中增加document template
    m_pImageDocTemplate = new CMultiDocTemplate(IDR_dcmviewerTYPE,
    RUNTIME_CLASS(CdcmviewerDoc),
    RUNTIME_CLASS(CChildFrame), // custom MDI child frame
    RUNTIME_CLASS(CdcmviewerView));
    if (!m_pImageDocTemplate)
    return FALSE;
    AddDocTemplate(m_pImageDocTemplate);
     m_pEditDocTemplate = new CMultiDocTemplate(
    IDR_dcmviewerTYPE,
    RUNTIME_CLASS(CdcmviewerDoc),
    RUNTIME_CLASS(CChildFrame), // custom MDI child frame
    RUNTIME_CLASS(CEditView));
    AddDocTemplate(m_pEditDocTemplate);

    然后override CxxxApp::OpenDocumentFile
      

  2.   

    CDocument* CxxxApp::OpenDocumentFile(LPCTSTR lpszFileName)
    {
    CDocument* pDoc = NULL;
    bool is_text_file = false;
    if(end_with(lpszFileName,_T(".txt"),true)|| end_with(lpszFileName,_T(".ini"),true)
    ||end_with(lpszFileName,_T(".h"),true)||end_with(lpszFileName,_T(".cpp"),true)
    ||end_with(lpszFileName,_T(".xml"),true)||end_with(lpszFileName,_T(".hpp"),true)
    ||end_with(lpszFileName,_T(".c"),true)||end_with(lpszFileName,_T(".cxx"),true)
    ||end_with(lpszFileName,_T(".html"),true) )
    {
    pDoc = m_pEditDocTemplate->OpenDocumentFile(lpszFileName);
    is_text_file = true;
    }
    else
            pDoc = m_pImageDocTemplate->OpenDocumentFile(lpszFileName);
    CMainFrame *pMainFrame = (CMainFrame*)AfxGetMainWnd();
    CMDIChildWnd* pMDIActive = pMainFrame->MDIGetActive();
    if(is_text_file)
    {
    CEditView *pview = (CEditView*)pMDIActive->GetActiveView();
    pMDIActive->SetTitle(lpszFileName);
    CEdit& wndEdit = pview->GetEditCtrl();
    pview->SetTabStops(4);
    wndEdit.ModifyStyle(ES_WANTRETURN,ES_AUTOVSCROLL|ES_MULTILINE|ES_AUTOHSCROLL);
    CFile ftmp;
    if(ftmp.Open(lpszFileName,CFile::modeRead) && ftmp.GetLength()>0)
    {
    std::auto_ptr<char> buf(new char[ftmp.GetLength()+1]);
    if(buf.get())
    {
    ftmp.Read(buf.get(),ftmp.GetLength());
    buf.get()[ftmp.GetLength()] = 0;
    wndEdit.SetWindowText(buf.get());
    }
    else
    {
    wndEdit.SetWindowText(_T("failed to read file"));
    }
    }
    wndEdit.Invalidate(); }
    if(lpszFileName)
    pMainFrame->MDIMaximize(pMDIActive);
    pMainFrame->MDIActivate(pMDIActive);//activate the view so that WM_KEYDOWN can work
    return pDoc;

    }
      

  3.   

    声明两个CDocMananger* 变量,付值后假如模板链(AddDocTemplate(pDocTemplate))
    创建新文件是会跳出个对话框,让你选择模板(很难看的:(),如过不想要的话就重载
    OnFileNew(),遍历模板链可以一次打开两个文档,也可以为没个文档仿OnFileNew()函数各做一个
    OnMyFileNew()