0

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.

ohluyao
  • 1
  • 1
  • 1

1 Answers1

0

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

dwo
  • 3,576
  • 2
  • 22
  • 39
  • Hi dwo,thanks for your answer.The key idea is to set the new view's control id as the id of old active view. But there's some problem when I try to do it. I create a new view by CSplitterWnd::CreateView(). Suppose that I have a split window created by m_splitter1, it has two view pView1 and pView2, next time I create another split window on pView1, and want to have a pView3, pView4,when I create pView3, it is very strange that I always get the same address as pView1, but pView1's member has been initialized again,so I can't get it again. Have you met some condition like this? – ohluyao Apr 24 '12 at 11:38
  • Create the view without CSplitterWnd::CreateView. Just make the constructor public and call new, Create and InitialUpdate yourself. – dwo Apr 24 '12 at 18:01
  • Thank you, I will have a try. – ohluyao Apr 25 '12 at 14:19