如何在不使用CSplitterWnd :: Create的情况下在MFC中动态拆分Window

时间:2012-04-21 03:34:51

标签: mfc csplitterwnd

我创建了一个MFC MDI应用程序,并希望通过右键单击并选择“AddSplitWnd”弹出菜单项,一次动态地将一个窗口拆分为两个部分。我尝试使用CSplitterWnd :: CreateStatic来实现它,一旦窗口被拆分,它需要创建一个新的视图,但我想改用以前的视图,所以有人知道如何实现它。谢谢。

1 个答案:

答案 0 :(得分:0)

以下是在SDI环境中的分割器中交换视图的代码段。这应该适用于MDI中的工作。

CView* CDoc::SwitchToView(CView* pNewView)
{
    CFrameWndEx* pMainWnd = (CFrameWndEx*)AfxGetMainWnd();
    CView* pOldActiveView;
    pOldActiveView = pMainWnd->GetActiveView();
    CSplitterWnd* pSplitter = (CSplitterWnd *)pOldActiveView->GetParent();

    // in this case Pane 0,0 is exchanged
    pOldActiveView = (CView*) pSplitter->GetPane(0,0);

    // set flag so that document will not be deleted when view is destroyed
    m_bAutoDelete = FALSE;    
    // Dettach existing view
    RemoveView(pOldActiveView);
    // set flag back to default 
    m_bAutoDelete = TRUE;

    // Set the child window ID of the active view to the ID of the corresponding
    // pane. Set the child ID of the previously active view to some other ID.
    ::SetWindowLong(pOldActiveView->m_hWnd, GWL_ID, 0);
    ::SetWindowLong(pNewView->m_hWnd, GWL_ID, pSplitter->IdFromRowCol(0,0));

    // Show the newly active view and hide the inactive view.
    pNewView->ShowWindow(SW_SHOW);
    pOldActiveView->ShowWindow(SW_HIDE);

    // Attach new view
    AddView(pNewView);

    // Set active 
    pSplitter->GetParentFrame()->SetActiveView(pNewView);
    pSplitter->RecalcLayout(); 
    return pOldActiveView;
}

HTH