我的程序中有两个视图,我希望在弹出一个对话框之后,激活另外一个new视图,而hide掉old视图,该怎么做?关键问题是:对话框弹出之后,old视图已经处于不被激活的状态,这种情况下如何得到old视图的指针,并且将之hide掉!希望我已经说明白了,谢谢关注!

解决方案 »

  1.   

    激活对话框后虽然view 不是当前窗口,可还是active !
    可以用GetActiveView()的!
    pView=GetActiveView();
      

  2.   

    void CMainFrame::SwitchToForm(UINT nForm)
    {
      CView *pOldActiveView =GetActiveView();
      CView *pNewActiveView=(CView*)GetDlgItem(nForm);
      if(pNewActiveView==NULL)
      {
    switch(nForm)
    {
      case IDD_MULTISCREEN_FORM1:
    pNewActiveView=new CView1();
    break;
      case IDD_MULTISCREEN_FORM2:
    pNewActiveView=new CView2();
    break;
      case IDD_MULTISCREEN_FORM3:
    pNewActiveView=new CView3();
    break;
      case ID_MainView:
    pNewActiveView=new CMultiScreenView();
    break;
    }
      
      CCreateContext context;
      context.m_pCurrentDoc=pOldActiveView->GetDocument();
      //add the flag:WS_BORDER,or the view will capture all the client included the windowrect!
      pNewActiveView->Create(NULL,NULL,WS_CHILD|WS_VISIBLE|WS_BORDER,CFrameWnd::rectDefault,this,nForm,&context);
      pNewActiveView->OnInitialUpdate();
      }
      SetActiveView(pNewActiveView);
      pNewActiveView->ShowWindow(SW_SHOW);
      pOldActiveView->ShowWindow(SW_HIDE);
      //I use the win32 based function:SetWindowLong() instead of the older style:SetWindowWord()
      ::SetWindowLong(pNewActiveView->m_hWnd,GWL_ID,AFX_IDW_PANE_FIRST);
      this->RecalcLayout();
      delete pOldActiveView;
    }
      

  3.   

    只要在CMainFrame中相应代码,总可以使用GetActiveView()的!
      

  4.   

    可是我GetActiveView()后的值是0x000000呀!
      

  5.   

    难道不能先保存一个view指针?
      

  6.   

    我给你一个代码,我想你应能得到你的,他是从另一个角度来做的。
    请给Email.
    我的Email为[email protected]
      

  7.   

    这个问题我做了,以前也回答过别人这样的问题,你可以找一下
    代码如下
    void CMainFrame::CreateOrActivateFrame(CDocTemplate* pTemplate,CRuntimeClass* pViewClass)
    {
    CMDIChildWnd* pMDIActive=MDIGetActive();//获得当前激活的子窗口
    if (pMDIActive==NULL)
    {
    MessageBox(_T("没有打开的文档类!"));
    ::PostQuitMessage(0);
    return;
    }
    WINDOWPLACEMENT WndStatus;
    CRect rect;
    rect.left=0;
    rect.top=0;
    rect.bottom=200;
    rect.right=200;
    WndStatus.rcNormalPosition=rect;
    pMDIActive->SetWindowPlacement(&WndStatus);
    CDocument* pDoc=pMDIActive->GetActiveDocument();//获得当前激活的文档
    ASSERT(pDoc!=NULL);
    CView* pView;
    POSITION pos=pDoc->GetFirstViewPosition();
    while(pos!=NULL)
    {
    pView=pDoc->GetNextView(pos);
    if (pView->IsKindOf(pViewClass))
    {
    pView->GetParentFrame()->ActivateFrame();
    //激活pViewClass 指向的视图
    return;
    }
    }
    //用当前文档模板创建新视图并激活
    CMDIChildWnd* pNewFrame=(CMDIChildWnd*)(pTemplate->CreateNewFrame
    (pDoc,NULL));
    if (pNewFrame==NULL)
    return;
    ASSERT_KINDOF(CMDIChildWnd,pNewFrame);
    pTemplate->InitialUpdateFrame(pNewFrame,pDoc);
    //    MDITile(MDITILE_HORIZONTAL);
    }
    在你想要的地方调用
    CreateOrActivateFrame(AfxGetApp()->m_pDocTemplate,CRuntimeCLass(CYourView))就可以了,
    m_pDocTemplate是和你的CYourView相关的文档模板类的对象
      

  8.   

    see the links below, maybe useful : http://www.codeproject.com/docview/replacingview.asp
    http://www.codeproject.com/docview/switchingviews.asp
      

  9.   

    Mark
    我需要这样的代码
    谢谢
      

  10.   

    楼上的答的不错呀
    循环浏览已经打开的文档 
    使用CDocTemplate中的GetFirstDocPosition()和GetNextDoc()函数。 应用程序中循环浏览已经打开的视 
    CDocument 中的 GetFirstViewPosition() 和 GetNextView() 函数。 
      

  11.   

    可是,因为我的程序是一个数据库程序,根本就涉及不到doc的事情,直接就是对后台的数据库进行操作,那我有怎么样来判断是否已经有视图存在,并得到他的指针哪?
      

  12.   

    就这个问题我也来问一下,
    MDI和SDI切换有很大的区别吗