现在需要做一个MDI程序,有两个不同的View类(不要求同时存在)。现在想知道是如何创建一个新的view。参考代码是
http://www.codeguru.com/Cpp/W-D/doc_view/viewmanagement/article.php/c6121/我的程序是这样的:CDocument *CDifferent1::DynamicOpenDocumentFile(LPCTSTR lpszPathName, BOOL
bMakeVisible)
{
CDocument* pDocument = NULL;
CFrameWnd* pFrame = NULL;
BOOL bCreated = FALSE;pDocument = CreateNewDocument();   //这一步有问题!!
ASSERT(pFrame == NULL); // will be created below
bCreated = TRUE;if(pDocument == NULL)
{
TRACE("pDocument == NULL");
}
pFrame = (CFrameWnd*) AfxGetMainWnd();CCreateContext context;
context.m_pCurrentFrame = NULL; // pFrame
context.m_pCurrentDoc = pDocument;
context.m_pLastView = NULL;
context.m_pNewDocTemplate = this;
context.m_pNewViewClass = m_pViewClass;CWnd *pView = pFrame->CreateView(&context);if (lpszPathName == NULL)
{
// create a new document
// avoid creating temporary compound file when starting up invisible
if (!bMakeVisible)
pDocument->m_bEmbedded = TRUE;}
return pDocument;
}现在的问题是
pDocument = CreateNewDocument();
这一步过后pDocument总是NULL,是怎么回事啊?

解决方案 »

  1.   

    我的工程 MDI , 一个网络扫描器, 多个视图都是基于 FormView 的在 CChildFrame 中重载父类 CMDIChildWnd 的成员虚函数 LoadFrameBOOL CChildFrame::LoadFrame(UINT nIDResource, DWORD dwDefaultStyle , CWnd* pParentWnd , CCreateContext* pContext)
    {
    // TODO: 在此添加专用代码和/或调用基类 return CMDIChildWnd::LoadFrame(nIDResource, dwDefaultStyle, pParentWnd, pContext);
    }字窗口由点击主窗口上的 Toolbar 创建,消息响应在 App 类中void CHinetApp::OnBtnArpspf()
    {
    // TODO: 在此添加命令处理程序代码
    CMainFrame* pFrame = STATIC_DOWNCAST(CMainFrame, m_pMainWnd);
    // create a new MDI child window
    //pFrame->CreateNewChild(
    // RUNTIME_CLASS(CChildFrame), IDR_HinetTYPE, m_hMDIMenu, m_hMDIAccel);
    CChildFrame *my = new CChildFrame();
    my->m_nMinWidth = 500;
    my->m_nMinHeight = 300;
    CCreateContext context;
    context.m_pNewViewClass = RUNTIME_CLASS(CARPSpoof); //CARPSpoof 是其中一个视图类
    if(!my->LoadFrame(IDR_MAINFRAME, WS_MAXIMIZE | WS_OVERLAPPEDWINDOW, this->m_pMainWnd, &context))
    return;
    my->ShowWindow(SW_SHOW);
    my->InitialUpdateFrame(NULL, true);
    }CARPSpoof 是其中一个视图类,其他视图也是用相似代码创建相同,不同视图类都可创建任意个数实例。子窗口功能代码都在相应的视图类中实现如:void CARPSpoof::OnBnClickedArpspfSave()
    {
        //function code here
    }
      

  2.   

    楼上的代码使用后确实可以建一个新的view。
    但是在我关了所有的view,再选择菜单上的新建时,
    会谈出一个dialog让我选择新建哪个view。
    怎么能不弹出那个框。而是默认新建的就是原来的view,而通过另外的按钮来创建我的view。
     
    与我在InitInstance加入的部分有关系么
     
    BOOL CDifferentViewApp::InitInstance()
    {
     InitCommonControls();
     
     CWinApp::InitInstance();
     
     if (!AfxOleInit())
     {
      AfxMessageBox(IDP_OLE_INIT_FAILED);
      return FALSE;
     }
     AfxEnableControlContainer();
     LoadStdProfileSettings(4);    CMultiDocTemplate* pDocTemplate;
     pDocTemplate = new CMultiDocTemplate(IDR_DifferentViewTYPE,
      RUNTIME_CLASS(CDifferentViewDoc),
      RUNTIME_CLASS(CChildFrame), 
      RUNTIME_CLASS(CDifferentViewView));
     if (!pDocTemplate)
      return FALSE;
     AddDocTemplate(pDocTemplate); CMainFrame* pMainFrame = new CMainFrame;
     if (!pMainFrame || !pMainFrame->LoadFrame(IDR_MAINFRAME))
      return FALSE;
     m_pMainWnd = pMainFrame;
      CCommandLineInfo cmdInfo;
     ParseCommandLine(cmdInfo);
     if (!ProcessShellCommand(cmdInfo))
      return FALSE;
     pMainFrame->ShowWindow(m_nCmdShow);
     pMainFrame->UpdateWindow();
     
    //我添加的部分
     pDocTemplate1 = new CDifferent1(IDR_DifferentViewTYPE,
      RUNTIME_CLASS(CDifferent1),
      RUNTIME_CLASS(CChildFrame), 
      RUNTIME_CLASS(CDifferent1View));
     if (!pDocTemplate1)
      return FALSE;
     AddDocTemplate(pDocTemplate1);
     
      return TRUE;
    }
      

  3.   

    CMultiDocTemplate* pDocTemplate;
     pDocTemplate = new CMultiDocTemplate(IDR_DifferentViewTYPE,
      RUNTIME_CLASS(CDifferentViewDoc),
      RUNTIME_CLASS(CChildFrame), 
      RUNTIME_CLASS(CDifferentViewView));
     if (!pDocTemplate)
      return FALSE;
     AddDocTemplate(pDocTemplate);我没有使用文档模板你的 OnNewFile 贴出来看看。
      

  4.   

    修改 OnNewFile, 不要用文档模板创建新视图。