Adding   multiple   views   to   an   SDI   application   
  1.build   an   normal   SDI   project   
  2.Add   a   new   view   class   derived   from   CView   
  3.Add   debug   and   non   debug   versions   of   the   GetDocument()   function   
  4.Add   a   public   constructor   
  5.make   the   destructor   provided   by   class   wizard   public   
  6.include   the   document   class   header   file   in   the   source   file   of   the   new   view   class   
  7.add   a   handler   for   the   OnDraw   event   of   the   new   view   class   
  8include   the   header   file   of   the   new   view   class   in   MainFrm.cpp   
  9add   two   protected   variables   in   the   CMainFrame   class   
  These   variables   will   be   pointers   to   the   two   view   objects   your   application   uses.   Initialize   these   
  pointers   to   NULL   in   the   constructor   of   CMainFrame,as   shown   in   the   following     
  class   CMainFrame:public   CFrameWnd   
  {   
  ...   
  protected:   
  CItalicsView*   m_pItalicView;   
  CDefaultView*   m_pDefaultView;   
  ...   
  }   
  CMainFrame::CMainFrame()   
  {   
  m_pItalicsView=NULL;   
  m_pDefaultView=NULL;   
  }   
  10.add   two   menu   selection   to   permit   the   application   to   switch   between   the   two   views.   
  11.add   command   handlers   to   CMainFrame   for   the   two   new   menu   items. 
  
  void   CMainFrame::OnViewItalics()   
  {     CDocument*   pDoc=GetActiveDocument();   
  if(NULL==m_pItalicView)   
  {   
  m_pDefaultView=(CDefaultView*)GetActiveView();   
  m_pItalicsView=new   CItalicsView(NULL);   
  m_pItalicsView->Create(NULL,NULL,AFX_WS_DEFAULT_VIEW,rectDefault,this,AFX_IDW_PANE_FIRST+1);   
  }   // m_pDefaultView->SaveData(); 保存另一个视图  pDoc->AddView(m_pItalicsView);   
  m_pItalicsView->SetDlgCtrlID(AFX_IDW_PANE_FIRST);   
  m_pDefaultView->SetDlgCtrlID(AFX_IDW_PANE_FIRST+1);   
  m_pItalicsView->ShowWindow(SW_SHOW);   
  m_pDefaultView->ShowWindow(SW_HIDE);   
  SetActiveView(m_pItalicsView);   
  pDoc->RemoveView(m_pDefaultView);   
  RecalcLayout();   
  }   
    
  void   CMainFrame::OnViewDefault()   
  {   
// m_pItalicsView->SaveData(); 保存另一个视图  CDocument*   pDoc=GetActiveDocument();   
  pDoc->AddView(m_pDefaultView);   
  m_pItalicsView->ShowWindow(SW_HIDE);   
  m_pDefaultView->ShowWindow(SW_SHOW);   
  m_pItalicsView->SetDlgCtrlID(AFX_IDW_PANE_FIRST+1);   
  m_pDefaultView->SetDlgCtrlID(AFX_IDW_PANE_FIRST);   
    
  SetActiveView(m_pDefaultView);   
  pDoc->RemoveView(m_pItalicsView);   
  RecalcLayout();   
  }  OnViewItalics和OnViewDefault是切换视图的两个方法,你完全可以在两个视图类里面分别添加一个SaveData的方法,然后在切换时调用。

解决方案 »

  1.   

    void CMainFrame::SelectView(int nView)
    {
    m_nView=nView;
    CView* pOldActiveView=GetActiveView();
    CView* pNewActiveView=(CView*)GetDlgItem(nView);
    if(pNewActiveView==NULL)
    {
    switch(nView)
    {
    case 1:
    pNewActiveView=(CView*) new CSTest2View;
    break;
    case 2:
    pNewActiveView=(CView*) new CFormView1;
    break;
    }
    CCreateContext context;
    context.m_pCurrentDoc=pOldActiveView->GetDocument();
    pNewActiveView->Create(NULL,NULL,WS_BORDER|WS_CHILD,CFrameWnd::rectDefault,this,nView,&context);
    pNewActiveView->OnInitialUpdate();

    }
    SetActiveView(pNewActiveView);
    pNewActiveView->ShowWindow(SW_SHOW);
    pOldActiveView->ShowWindow(SW_HIDE);
    if(pOldActiveView->GetRuntimeClass()==RUNTIME_CLASS(CSTest2View))
    pOldActiveView->SetDlgCtrlID(1);
    else if(pOldActiveView->GetRuntimeClass()==RUNTIME_CLASS(CFormView1))
    pOldActiveView->SetDlgCtrlID(2);
    pNewActiveView->SetDlgCtrlID(AFX_IDW_PANE_FIRST);
    delete pOldActiveView;
    RecalcLayout();
    }
    我是用这种方法切换的, 你说的那不知道能继承CFormView类不?我的这种方法要怎么保存变量的值呢?3q