上面发的是3视图切换 这是2视图切换(MSDN上有) BOOL CMainFrame::OnViewChange(UINT nCmdID) // There is an ON_COMMAND_RANGE message map entry associated with // OnViewChange: // ON_COMMAND_RANGE( ID_VIEW_VIEW1, ID_VIEW_VIEW2, OnViewChange) { CView* pViewAdd; CView* pViewRemove; CDocument* pDoc = GetActiveDocument(); if((nCmdID == ID_VIEW_VIEW1) && (m_currentView == 1)) return; if((nCmdID == ID_VIEW_VIEW2) && (m_currentView == 2)) return; if (nCmdID == ID_VIEW_VIEW2) { if (m_pView2 == NULL) { m_pView1 = GetActiveView(); m_pView2 = new CMyView2;//Note that if OnSize has been overridden in CMyView2 //and GetDocument() is used in this override it can //cause assertions and, if the assertions are ignored, //cause access violation.
// Set the child i.d. of the active view to AFX_IDW_PANE_FIRST, // so that CFrameWnd::RecalcLayout will allocate to this // "first pane" that portion of the frame window's client area // not allocated to control bars. Set the child i.d. of the // other view to anything other than AFX_IDW_PANE_FIRST; this // examples switches the child id's of the two views. int nSwitchChildID = pViewAdd->GetDlgCtrlID(); pViewAdd->SetDlgCtrlID(AFX_IDW_PANE_FIRST); pViewRemove->SetDlgCtrlID(nSwitchChildID); // Show the newly active view and hide the inactive view. pViewAdd->ShowWindow(SW_SHOW); pViewRemove->ShowWindow(SW_HIDE); // Connect the newly active view to the document, and // disconnect the inactive view. pDoc->AddView(pViewAdd); pDoc->RemoveView(pViewRemove); SetActiveView(pViewAdd); RecalcLayout(); }
CView::GetDocument()
{ CView* pViewAdd=NULL;
CView* pViewRemove=NULL;
CDocument* pDoc = GetActiveDocument();
if((nCmdID == IDM_VIEW_0) && (m_currentView == 0))
return;
if((nCmdID == IDM_VIEW_1) && (m_currentView == 1))
return;
if((nCmdID == IDM_VIEW_2) && (m_currentView == 2))
return;
if(nCmdID == IDM_VIEW_1)
{
if (m_pView1 == NULL)
{
m_pView0 = (CTest001View*)GetActiveView();
m_pView1 = new CTest001View1;
m_pView1->MyCreate(NULL, NULL, AFX_WS_DEFAULT_VIEW,
rectDefault, this, AFX_IDW_PANE_FIRST + 1, NULL);
}
if(m_pLastView==NULL)
{
m_pLastView=m_pView;
}
else
{
pViewRemove=m_pLastView;
} pViewRemove = m_pLastView;
pViewAdd = m_pView1;
m_currentView= 1;
m_pLastView=m_pView1;
}
else if(nCmdID == IDM_VIEW_2)
{
if (m_pView2 == NULL)
{
m_pView = (CTest001View*)GetActiveView();
m_pView2 = new CTest001View2;
m_pView2->MyCreate(NULL, NULL, AFX_WS_DEFAULT_VIEW,
rectDefault, this, AFX_IDW_PANE_FIRST + 1, NULL); }
if(m_pLastView==NULL)
{
m_pLastView=m_pView;
}
else
{
pViewRemove=m_pLastView;
}
pViewRemove=m_pLastView;
pViewAdd = m_pView2;
m_currentView= 2;
m_pLastView=m_pView2;
}
else if(nCmdID == IDM_VIEW_0)
{
m_pView = (CTest001View*)GetActiveView();
m_pView2 = new CTest001View2;
m_pView2->MyCreate(NULL, NULL, AFX_WS_DEFAULT_VIEW,
rectDefault, this, AFX_IDW_PANE_FIRST + 1, NULL); if(m_pLastView==NULL)
{
m_pLastView=m_pView;
}
else
{
pViewRemove=m_pLastView;
} pViewRemove = m_pLastView;
pViewAdd = m_pView0;
m_currentView= 0;
m_pLastView=m_pView0;
}
else
return; int nSwitchChildID = pViewAdd->GetDlgCtrlID();
pViewAdd->SetDlgCtrlID(AFX_IDW_PANE_FIRST);
pViewRemove->SetDlgCtrlID(nSwitchChildID); pViewAdd->ShowWindow(SW_SHOW);
pViewRemove->ShowWindow(SW_HIDE); pDoc->AddView(pViewAdd);
pDoc->RemoveView(pViewRemove); SetActiveView(pViewAdd);
RecalcLayout();}
这是2视图切换(MSDN上有)
BOOL CMainFrame::OnViewChange(UINT nCmdID)
// There is an ON_COMMAND_RANGE message map entry associated with
// OnViewChange:
// ON_COMMAND_RANGE( ID_VIEW_VIEW1, ID_VIEW_VIEW2, OnViewChange)
{
CView* pViewAdd;
CView* pViewRemove;
CDocument* pDoc = GetActiveDocument(); if((nCmdID == ID_VIEW_VIEW1) && (m_currentView == 1))
return;
if((nCmdID == ID_VIEW_VIEW2) && (m_currentView == 2))
return; if (nCmdID == ID_VIEW_VIEW2)
{
if (m_pView2 == NULL)
{
m_pView1 = GetActiveView();
m_pView2 = new CMyView2;//Note that if OnSize has been overridden in CMyView2
//and GetDocument() is used in this override it can
//cause assertions and, if the assertions are ignored,
//cause access violation.
m_pView2->Create(NULL, NULL, AFX_WS_DEFAULT_VIEW,
rectDefault, this, AFX_IDW_PANE_FIRST + 1, NULL);
}
pViewAdd = m_pView2;
pViewRemove = m_pView1;
m_currentView= 2;
}
else
{
pViewAdd = m_pView1;
pViewRemove = m_pView2;
m_currentView= 1;
}
// Set the child i.d. of the active view to AFX_IDW_PANE_FIRST,
// so that CFrameWnd::RecalcLayout will allocate to this
// "first pane" that portion of the frame window's client area
// not allocated to control bars. Set the child i.d. of the
// other view to anything other than AFX_IDW_PANE_FIRST; this
// examples switches the child id's of the two views. int nSwitchChildID = pViewAdd->GetDlgCtrlID();
pViewAdd->SetDlgCtrlID(AFX_IDW_PANE_FIRST);
pViewRemove->SetDlgCtrlID(nSwitchChildID); // Show the newly active view and hide the inactive view. pViewAdd->ShowWindow(SW_SHOW);
pViewRemove->ShowWindow(SW_HIDE); // Connect the newly active view to the document, and
// disconnect the inactive view.
pDoc->AddView(pViewAdd);
pDoc->RemoveView(pViewRemove); SetActiveView(pViewAdd);
RecalcLayout();
}