Recently, when working on a lab project, you need to generate a single-document multi-view structure under MFC, after several hours of searching and checking on the internet, I finally found a feasible and understandable solution:
First, because it is a static creation, it must first initialize the view you want to create. Note that all my operations are performed in the ** App class.
1 private:
2 CView * m_pView [4];
In the App class, I declare a view array, initialize it in InitInstance, and create a view.
View class initialization
1 CView * m_pActiveView = (CFrameWnd *) m_pMainWnd)-> GetActiveView ();
2 m_pView [0] = m_pActiveView; // assign it directly because m_pView [0] is displayed at the beginning
3 m_pView [1] = new CProductionFormView;
4 m_pView [2] = new CEventsListView;
5 m_pView [3] = new CRunningFormView;
6 // initialize the context, mainly the document pointer, and create other views
7 CCreateContext context;
8 context. m_pCurrentDoc = (CFrameWnd *) m_pMainWnd)-> GetActiveDocument ();
9 m_pView [1]-> Create (NULL, NULL, (AFX_WS_DEFAULT_VIEW &~ WS_VISIBLE), CFrameWnd: rectDefault, m_pMainWnd, AFX_IDW_PANE_FIRST + 1, & context );
10 m_pView [2]-> Create (NULL, NULL, (AFX_WS_DEFAULT_VIEW &~ WS_VISIBLE), CFrameWnd: rectDefault, m_pMainWnd, AFX_IDW_PANE_FIRST + 2, & context );
11 m_pView [3]-> Create (NULL, NULL, (AFX_WS_DEFAULT_VIEW &~ WS_VISIBLE), CFrameWnd: rectDefault, m_pMainWnd, AFX_IDW_PANE_FIRST + 3, & context );
12 // We need to manually call the update function
13 for (int I = 0; I <4; I ++)
14 m_pView [I]-> OnInitialUpdate ();
I add the Command Message response view switch to the menu. The entire switch is completed in the SwitchView function. Its definition is as follows:
SwitchView
1 void CCardSystemApp: SwithToView (int nView)
2 {
3 ASSERT (nView> = 0 & nView <= 4 );
4 CView * pOldActiveView = (CFrameWnd *) m_pMainWnd)-> GetActiveView ();
5 CView * pNewActiveView = m_pView [nView];
6 ASSERT (pNewActiveView );
7 if (pNewActiveView = pOldActiveView)
8 return;
9
10 // switch the window ID of the view so that RecalcLayout () can work
11 UINT temp =: GetWindowLong (pOldActiveView-> m_hWnd, GWL_ID );
12: SetWindowLong (pOldActiveView-> m_hWnd, GWL_ID,: GetWindowLong (pNewActiveView-> m_hWnd, GWL_ID ));
13: SetWindowLong (pNewActiveView-> m_hWnd, GWL_ID, temp );
14
15 pOldActiveView-> ShowWindow (SW_HIDE );
16 pNewActiveView-> ShowWindow (SW_SHOW );
17
18 (CFrameWnd *) m_pMainWnd)-> SetActiveView (pNewActiveView );
19 (CFrameWnd *) m_pMainWnd)-> RecalcLayout ();
20 pNewActiveView-> Invalidate ();
21
22}