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



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

下面是在 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;
}

相关内容

最新更新