在多文档框架中实现多重文件(不同类型的文件)编辑时,遇到了一个问题。本人做了一个能编辑文本和位图的程序,每次想新建一个文本文件时,都会弹出选择新建文件的类型的窗口,我想是应用程序不知道新建文档的类型的原因所致。请教各位,在新建文件时,如何可以使得指定生成某种类型的文档,避免每次都弹出窗口选择呢?

解决方案 »

  1.   

    1.在应用程序类中添加如下成员(个数由你的文件类型数决定)
    CMultiDocTemplate* m_pDocTemplate;
    CMultiDocTemplate* m_pDocTemplateModule;
    CDocument* CCurveApp::OpenModuleDocumentFile(LPCTSTR lpszFileName)
    CDocument* CCurveApp::OpenCurveDocumentFile(LPCTSTR lpszFileName)2.在应用程序类中的INSTANCE函数中把原来的"CMultiDocTemplate* pDocTemplate;"改为如下:
    m_pDocTemplate = new CMultiDocTemplate(
    IDR_CURVETYPE,
    RUNTIME_CLASS(CCurveDoc),
    RUNTIME_CLASS(CChildFrame), // custom MDI child frame
    RUNTIME_CLASS(CCurveView));
    AddDocTemplate(m_pDocTemplate);
    m_pDocTemplateModule = new CMultiDocTemplate(
    IDR_CURVETYPE,
    RUNTIME_CLASS(CModuleDoc),
    RUNTIME_CLASS(CChildFrame), // custom MDI child frame
    RUNTIME_CLASS(CModuleView));
    AddDocTemplate(m_pDocTemplateModule);
    3.重载ONFILENEW,并直接返回
    4.在文件菜单上插入两个与你文件类型对应菜单,并添加消息映射在相应的消息映射中调用那两个成员函数
    5.成员函数的实现如下:
    CDocument* CCurveApp::OpenModuleDocumentFile(LPCTSTR lpszFileName)
    {
    CDocument* pDoc = NULL;
        pDoc = m_pDocTemplateModule->OpenDocumentFile(NULL);
    pDoc->SetTitle("模块单元");
    return pDoc;
    }CDocument* CCurveApp::OpenCurveDocumentFile(LPCTSTR lpszFileName)
    {
    CDocument* pDoc = m_pDocTemplate->OpenDocumentFile(NULL);
    return pDoc;
    }
      

  2.   

    1.在应用程序中添加如下成员(成员的个数由你的文件类型决定)
    CDocument* OpenCurveDocumentFile(LPCTSTR lpszFileName);
    CDocument* OpenModuleDocumentFile(LPCTSTR lpszFileName);
    CMultiDocTemplate* m_pDocTemplate;
    CMultiDocTemplate* m_pDocTemplateModule;2.上面两个函数的实现如下:
    CDocument* CCurveApp::OpenModuleDocumentFile(LPCTSTR lpszFileName)
    {
    CDocument* pDoc = NULL;
      pDoc = m_pDocTemplateModule->OpenDocumentFile(NULL);
    return pDoc;
    }CDocument* CCurveApp::OpenCurveDocumentFile(LPCTSTR lpszFileName)
    {
    CDocument* pDoc = m_pDocTemplate->OpenDocumentFile(NULL);
    return pDoc;
    }3.在应用程序对象的INSTANCE 函数中将原来APPWIZARD生成的
    CMultiDocTemplate* pDocTemplate;
    m_pDocTemplate = new CMultiDocTemplate(
    IDR_CURVETYPE,
    RUNTIME_CLASS(CCurveDoc),
    RUNTIME_CLASS(CChildFrame), // custom MDI child frame
    RUNTIME_CLASS(CCurveView));
    AddDocTemplate(m_pDocTemplate);
    改为
    // CMultiDocTemplate* pDocTemplate;
    m_pDocTemplate = new CMultiDocTemplate(
    IDR_CURVETYPE,
    RUNTIME_CLASS(CCurveDoc),
    RUNTIME_CLASS(CChildFrame), // custom MDI child frame
    RUNTIME_CLASS(CCurveView));
    AddDocTemplate(m_pDocTemplate);
    m_pDocTemplateModule = new CMultiDocTemplate(
    IDR_CURVETYPE,
    RUNTIME_CLASS(CModuleDoc),
    RUNTIME_CLASS(CChildFrame), // custom MDI child frame
    RUNTIME_CLASS(CModuleView));
    AddDocTemplate(m_pDocTemplateModule);
    //可共享CChildFrame
    4.改写应用程序对象的ONFILENEW为如下:
    void CCurveApp::OnFileNew() 
    {
    // TODO: Add your command handler code here
    return;
    }
    5.在菜单上添加两个菜单项,分别对应相应的文件类型,并添加消息映射,在它们的消息映射中调用相对应的
    OpenCurveDocumentFile或OpenModuleDocumentFile