“没有新的文档出现”是什么意思?不明白。能不能说的具体一点?    MFC打开文档的过程是这样的:首先是CWinApp::OnFileOpen(),这个函数只是简单调用CDocManager::OnFileOpen()。CDocManager::OnFileOpen()负责显示文件对话框,然后调用CWinApp::OpenDocumentFile()打开选择的文件。    你使用CWinApp::OpenDocumentFile()了吗?仔细检查你的代码,这个问题应该比较简单。

解决方案 »

  1.   


        另外,出错的原因还可能是因为你注册的多个模板中没有与打开文件匹配的类型。这时CDocManager::OpenDocumentFile(LPCTSTR lpszFileName)将返回NULL,即无法打开文档。这个函数如下(copy自MFC,^_^):CDocument* CDocManager::OpenDocumentFile(LPCTSTR lpszFileName)
    {
    // find the highest confidence
    POSITION pos = m_templateList.GetHeadPosition();
    CDocTemplate::Confidence bestMatch = CDocTemplate::noAttempt;
    CDocTemplate* pBestTemplate = NULL;
    CDocument* pOpenDocument = NULL; TCHAR szPath[_MAX_PATH];
    ASSERT(lstrlen(lpszFileName) < _countof(szPath));
    TCHAR szTemp[_MAX_PATH];
    if (lpszFileName[0] == '\"')
    ++lpszFileName;
    lstrcpyn(szTemp, lpszFileName, _MAX_PATH);
    LPTSTR lpszLast = _tcsrchr(szTemp, '\"');
    if (lpszLast != NULL)
    *lpszLast = 0;
    AfxFullPath(szPath, szTemp);
    TCHAR szLinkName[_MAX_PATH];
    if (AfxResolveShortcut(AfxGetMainWnd(), szPath, szLinkName, _MAX_PATH))
    lstrcpy(szPath, szLinkName); while (pos != NULL)
    {
    CDocTemplate* pTemplate = (CDocTemplate*)m_templateList.GetNext(pos);
    ASSERT_KINDOF(CDocTemplate, pTemplate); CDocTemplate::Confidence match;
    ASSERT(pOpenDocument == NULL);
    match = pTemplate->MatchDocType(szPath, pOpenDocument);
    if (match > bestMatch)
    {
    bestMatch = match;
    pBestTemplate = pTemplate;
    }
    if (match == CDocTemplate::yesAlreadyOpen)
    break;      // stop here
    } 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;
    } if (pBestTemplate == NULL)
    {
    AfxMessageBox(AFX_IDP_FAILED_TO_OPEN_DOC);
    return NULL;
    } return pBestTemplate->OpenDocumentFile(szPath);
    }
      

  2.   

    非常感谢,问题得到初步解决,现在能出现新的视图,但是新打开的文件被显示在原来的窗口,
    而不是那个刚刚被打开的窗口。下面是我的OnFileOpen()
    {
    CWinApp* pWinApp;
    pWinApp = (CWinApp*) AfxGetApp( );
    static char szFilter[]="BMP FILES(*.BMP)|*.BMP||";
    FileDialog FileDlg(TRUE, NULL, NULL,
    OFN_HIDEREADONLY, szFilter);
    if(FileDlg.DoModal()==IDOK){
    pWinApp->OpenDocumentFile(FileDlg.GetPathName());
    m_pDib->Load( FileDlg.GetPathName());
    }
    }
    其中,m_pDib是定义在CMyDoc中的一个指向图象类(CDib)的指针,Load()用来读取文件进内存