How to split a Window dynamically in MFC without using CSplitterWnd::Create

生来就可爱ヽ(ⅴ<●) 提交于 2020-01-07 06:20:15

问题


I create a MFC MDI application, and want to split a window into two parts at a time dynamically by right click and choosing a "AddSplitWnd" pop menu item. I try to use CSplitterWnd::CreateStatic to implement it, once the window is split, it need to create a new view, but I want to use the previous view instead, so does anyone know how to implement it. Thank you.


回答1:


Here is a code snippet to exchange views in a splitter in a SDI environment. This should be adaptable to work in MDI as well.

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



来源:https://stackoverflow.com/questions/10255961/how-to-split-a-window-dynamically-in-mfc-without-using-csplitterwndcreate

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!