我这样作了一个文档对应显示两个不同的视图的程序,可以分别显示出两个视图了,
 CMultiDocTemplate* m_pDocTempMain;
 CMultiDocTemplate* m_pDocTempOther;  在InitInstance()中
  m_pDocTempMain = new CMultiDocTemplate(IDR_DEMOTYPE,
RUNTIME_CLASS(CCommeDoc),
RUNTIME_CLASS(CChildFrame), // 自定义 MDI 子框架
RUNTIME_CLASS(CMainView));
AddDocTemplate(m_pDocTempMain);  pDocTempOther = new CMultiDocTemplate(IDR_FAULTTYPE,
    RUNTIME_CLASS(CCommeDoc),
RUNTIME_CLASS(CChildFrame), // 自定义 MDI 子框架
RUNTIME_CLASS(COtherView));
AddDocTemplate(pDocTempOther);  CMainFrame* pMainFrame = new CMainFrame;
if (!pMainFrame->LoadFrame(IDR_MAINFRAME))
return FALSE;
  m_pMainWnd = pMainFrame;
  
  CCommandLineInfo cmdInfo;
  ParseCommandLine(cmdInfo);
  if (cmdInfo.m_nShellCommand == CCommandLineInfo::FileNew)
  {
    cmdInfo.m_nShellCommand = CCommandLineInfo::FileNothing;
  }
  if (!ProcessShellCommand(cmdInfo))
     return FALSE;  // 主窗口已初始化,因此显示它并对其进行更新
  pMainFrame->ShowWindow(SW_SHOWMAXIMIZED);
  pMainFrame->UpdateWindow();
  return TRUE;
请问,我这么实现这两个不同试图之间的切换显示,比如选择菜单的某一项显示
CMainView对应的视图,而选择另外一项则显示COtherView对应的视图?

解决方案 »

  1.   

    在WinApp里响应菜单命令(当然也可以是工具条按钮命令)
    CMyApp::OnViewMainView()
    {
       m_pDocTempMain->OpenDocumentFile(NULL);//显示CMainView对应的视图
     }CMyApp::OnViewOtherView()
    {
       m_pDocTempOther->OpenDocumentFile(NULL);//显示COtherView对应的视图
     }
      

  2.   

    我的本意是
    m_pDocTempMain->OpenDocumentFile(NULL);//显示CMainView对应的视图
    m_pDocTempOther->OpenDocumentFile(NULL);//显示COtherView对应的视图
    之后,比如当前显示的是CMainView视图,如何切换到COtherView视图显示,
    而不是再创建一个新的视图显示。也就是实现一般的文档/试图中的“窗口”
    菜单中的窗口选择功能。
      

  3.   

    如何切换视口而不破坏它们?
    我创建了一个带有静态分隔区的sdi应用程序,左边显示工作区,右过显示左边选取
    的东西.我想达到的是如果在分隔区之间进行切换,而不覆盖或破坏原来的CView对象.
    A:以下代码是你所想要的:
    class CExSplitterWnd : public CSplitterWnd
    {
    // Construction
    public:
        CExSplitterWnd();
    // Overrides
        // ClassWizard generated virtual function overrides
        //{{AFX_VIRTUAL(CExSplitterWnd)
        //}}AFX_VIRTUAL
    // Implementation
        virtual ~CExSplitterWnd();
        BOOL AttachView(CWnd* pView, int row, int col);
        BOOL DetachView(int row, int col);
        // Generated message map functions
        //{{AFX_MSG(CExSplitterWnd)
            // NOTE - the ClassWizard will add and remove member functions here.
        //}}AFX_MSG
        DECLARE_MESSAGE_MAP()
    };CExSplitterWnd::CExSplitterWnd()
    {
    }CExSplitterWnd::~CExSplitterWnd()
    {
    }BOOL CExSplitterWnd::AttachView(CWnd* pView, int row, int col)
    {
        //Make sure the splitter window was created
        if (!IsWindow(m_hWnd))
        {
            ASSERT(0);
            TRACE(_T("Create the splitter window before attaching windows to panes"));
            return (FALSE);
        }    //Make sure the row and col indices are within bounds
        if (row >= GetRowCount() || col >= GetColumnCount())
        {
            ASSERT(0);
            return FALSE;
        }    //Is the window to be attached a valid one
        if (pView == NULL || (!IsWindow(pView->m_hWnd)))
        {
            ASSERT(0);
            return FALSE;
        }    pView->SetDlgCtrlID(IdFromRowCol(row, col));
        pView->SetParent(this);
        pView->ShowWindow(SW_SHOW);
        pView->UpdateWindow();
        return (TRUE);
    }BOOL CExSplitterWnd::DetachView(int row, int col)
    {
        //Make sure the splitter window was created
        if (!IsWindow(m_hWnd))
        {
            ASSERT(0);
            TRACE(_T("Create the splitter window before attaching windows to panes"));
            return (FALSE);
        }    //Make sure the row and col indices are
        //within bounds
        if (row >= GetRowCount() || col >= GetColumnCount())
        {
            ASSERT(0);
            return FALSE;
        }    CWnd* pWnd = GetPane(row, col);
        if (pWnd == NULL || (!IsWindow(pWnd->m_hWnd)))
        {
            ASSERT(0);
            return FALSE;
        }
        pWnd->ShowWindow(SW_HIDE);
        pWnd->UpdateWindow();    //Set the parent window handle to NULL so that this child window is not
        //destroyed when the parent (splitter) is destroyed
        pWnd->SetParent(NULL);
        return (TRUE);
    }
      

  4.   

    自己创建一个视图的指针列表,然后调用p->setActiveWindow()激活你想切换的视图
      

  5.   

    修改CView的派生类,把构造函数改为public型的。在派生类中,添加函数
    void CXXXView::SwitchToForm(int nForm)
    {
      CView* pOldView = GetActiveView();
      CView* pNewView = (CView*)GetDlgItem(nForm);
      if(pNewView ==NULL)
      {
         switch(nForm)
         {
           case IDD_YOURVIEW1:
           pNewView = (CView*) new CXXXView;
           break;
           case IDD_YOURVIEW2:
           pNewView = (CView*) new CXXXView;
           break;
           ..........
          }
          CCreateContext context;
          context.m_pCurrentDoc = pOldView->GetDocument();
          pNewView = pOldView->Create(NULL,NULL,0L,CFrameWnd::rectDefault,this,nForm,&context);
         pNewView->OnInitialUpdate();
       }
      SetActiveView(pNewView);
      pNewView->ShowWindow(SW_SHOW);
      pOldView->ShowWindow(SW_HIDE);
      ::SetWindowWord(pNewView->m_hWnd,GWL_ID,AFX_IDW_PANE_FIRST);
      RecalcLayout();
      delete pOldView;
    }