单文档应用程序SDI的视图切换

本文介绍了一个应用程序中实现视图切换的技术细节,包括视图初始化、菜单消息处理及视图窗口交换过程。通过在启动时预生成所有视图,并在菜单选择时通过窗口ID交换来快速切换视图。

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();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值