1、在app的BOOL COutlookMultiViewsApp::InitInstance()
((CMainFrame*)m_pMainWnd)->InitViews ();//将所有要切换的视图提前生成,见3
// The one and only window has been initialized, so show and update it.
m_pMainWnd->ShowWindow(SW_SHOW);
m_pMainWnd->UpdateWindow();
return TRUE;
2、框架的菜单消息函数内:
CView* pNewView = m_pViews[nIndex];
CView* pActiveView =GetActiveView();
if ( !pActiveView ) // No currently active view
return;
if ( pNewView == pActiveView ) // Already there
return;
m_nCurView = nIndex; // Store the new current view's index
// exchange view window ID's so RecalcLayout() works
UINT temp = ::GetWindowLong(pActiveView->m_hWnd, GWL_ID);
::SetWindowLong(pActiveView->m_hWnd, GWL_ID,
::GetWindowLong(pNewView->m_hWnd, GWL_ID));
::SetWindowLong(pNewView->m_hWnd, GWL_ID, temp);
// Display and update the new current view - hide the old one
pActiveView->ShowWindow(SW_HIDE);
pNewView->ShowWindow(SW_SHOW);
SetActiveView(pNewView);
RecalcLayout();
pNewView->Invalidate();
3、InitViews 视图的初始化
m_nCurView = 0; // Save index of the currently active view class
CView* pActiveView = GetActiveView();
m_pViews[0] = pActiveView;
m_pViews[1] = (CView*) new CView1;
m_pViews[2] = (CView*) new CView2;
m_pViews[3] = (CView*) new CView3;
CDocument* pCurrentDoc = GetActiveDocument();
// Initialize a CCreateContext to point to the active document.
// With this context, the new view is added to the document
// when the view is created in CView::OnCreate().
CCreateContext newContext;
newContext.m_pNewViewClass = NULL;
newContext.m_pNewDocTemplate = NULL;
newContext.m_pLastView = NULL;
newContext.m_pCurrentFrame = NULL;
newContext.m_pCurrentDoc = pCurrentDoc;
CRect rect(0, 0, 0, 0); // gets resized later
for (int nView = 1; nView < NUMVIEWS; nView++)
{
// Create the new view. In this example, the view persists for
// the life of the application. The application automatically
// deletes the view when the application is closed.
m_pViews[nView]->Create(NULL, NULL,
(AFX_WS_DEFAULT_VIEW & ~WS_VISIBLE),
// views are created with the style of AFX_WS_DEFAULT_VIEW
// In MFC 4.0, this is (WS_BORDER | WS_VISIBLE | WS_CHILD)
rect, this,
AFX_IDW_PANE_FIRST + nView, &newContext);
// When a document template creates a view, the WM_INITIALUPDATE
// message is sent automatically. However, this code must
// explicitly send the message, as follows.
m_pViews [nView]->OnInitialUpdate();
}
}
本文介绍了一个应用程序中实现视图切换的技术细节,包括视图初始化、菜单消息处理及视图窗口交换过程。通过在启动时预生成所有视图,并在菜单选择时通过窗口ID交换来快速切换视图。

被折叠的 条评论
为什么被折叠?



