First, SDI
1, INTO MAINFRAME.H ADD MEMBER
INT M_CurrentView; CView2 * m_pview2; cView1 * m_pView1;
2, in InitInstance ()
((CMAINFRAME *) m_pmainwnd) -> m_pView1 = (cView1 *) ((cMAINFRAME *) m_pmainwnd) -> getActiveView ());
3, Add Message Handler
In Head file
AFX_MSG Void OnViewChange (uint ncmdid);
In CPP File
Begin_MESSAGE_MAP (CMAINFRAME, CFRAMEWND) / / {{AFX_MSG_MAP (CMAINFRAME) ON_COMMAND_RANGE (IDM_VIEW1, IDM_VIEW2, ONVIEWCHANGE) / /}} AFX_MSG_MAPEND_MESSAGE_MAP ()
CMAINFRAME :: CMAINFRAME () {// Todo: add member initialization code herE m_pview2 = null; m_currentview = 1;}
Void CMAINFRAME :: OnViewChange (uint ncmdid) {// Todo: add your command handler code here
CView * pViewAdd; CView * pViewRemove; CDocument * pDoc = GetActiveDocument (); if ((nCmdID == IDM_VIEW1) && (m_currentView == 1)) return; if ((nCmdID == IDM_VIEW2) && (m_currentView == 2)) Return;
IF (ncmdid == idm_view2) {if (m_pView2 == null) {m_pView1 = (cView1 *) getActiveView (); m_pView2 = new cView2;
// 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 violations. M_pView2-> MyCreate (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 identifier of the active view to AFX_IDW_PANE_FIRST, // so that CFrameWnd :: RecalcLayout will allocate to this // first pane, that portion in a client area of the frame window that is // not allocated to control bars Set the child identifier of the // other view to anything besides AFX_IDW_PANE_FIRST;. this // example switches the child identifiers 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; pViewRemove-> showwindow (sw_hide);
// Connect the newly active view to the document and // disconnect the inactive view. Pdoc-> addview; pdoc-> removeView;
SetActiveView (PVIEWADD); RecalcLayout ();
}
void CView2 :: MyCreate (LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT & rect, CWnd * pParentWnd, UINT nID, CCreateContext * pContext) {CFormView :: Create (lpszClassName, lpszWindowName, dwStyle, rect, pParentWnd, nID, pContext) }
DON NOTGETGET Edit Menu Resource.
I make MFC SDI Project by Default Setting, In Step 6 CView1 Derived from CFormview
And INSERT DIALOG WITH CHILD, BORDER NONE Style, The Add CView2 Into Project.
Second, MDI
IS Similarly with SDI
The replaceview () Function U Could Found in msdn but not the same
I Don Not Destroy View and review View.
Now i ctr v the core code
Mainframe.cpp
BOOL CMainFrame :: ReplaceView (CView * pNewView) {CMDIFrameWnd * pMainWnd = (CMDIFrameWnd *) AfxGetMainWnd (); // Get the active MDI child window CMDIChildWnd * pChild = (CMDIChildWnd *) pMainWnd-> MDIGetActive ().;
// Get the active view attached to the active MDI child window CView * pOldActiveView = pChild-> GetActiveView ();.. // Set flag so that document will not be deleted when view is dettached CDocument * pDoc = pOldActiveView-> GetDocument ( );
Bool bautodelete = pdoc-> m_bautodelete; pdoc-> m_bautodelete = false;
// Dettach EXISTING VIEW PDOC-> RemoveView (POLDACTIVEVIEW);
// restore flag pdoc-> m_bautode = bautodelete; // show the newly active view and hide the inactive view. pnewview-> showwindow (sw_show); PoldActiveView-> showwindow (sw_hide);
// attach new view pdoc-> addview (pnewview); pchild-> recalclayout (); pnewview-> updateWindow (); pchild-> setActiveView (PNewView); Return True;
}
void CMainFrame :: OnViewChange (UINT nID) {// TODO: Add your command handler code here switch (nID) {case IDM_VIEW1: ReplaceView (m_pView1); break; case IDM_VIEW2: CMainFrame * pMainWnd = (CMainFrame *) AfxGetMainWnd (); // Get the Active MDI Child Window. Cmdichildwnd * pchild = (cmdichildwnd *) PMainWnd-> mdigetActive ();
IF (! m_pview2) {// create the new view m_pview2 = new cView2; m_pview2-> mycreate (null, null, afx_ws_default_view, crect (0, 0, 0), pchild, afx_idw_pane_first, null);}