Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create multi views in a CChildFrame using CSplitterWnd

Tags:

mfc

I am working with MFC MDI. I need to create views as follow. My ChildWnd is splitted in 2 parts. They are LeftView which is a CView and RightView which is a CScrollView. The LeftView is splitted in 2 parts, the TreeView and the the FormView. How can I do that?

_________________________________
|          |                    |
|          |                    |
|CTreeView  |                   |
|          |                    |
|          |                    |
|          |        CScrollView |
|___________|                   |
|          |                    |
|          |                    |
|CFormView  |                   |
|          |                    |
|          |                    |
----------------------------------
like image 374
ducva Avatar asked Jun 18 '26 16:06

ducva


1 Answers

I finally solved this problem. I wrote the solution for anyone who has a similar problem.

  1. In CChildFrame class, declare CSplitterWnd m_SplitterWnd;

  2. In CChildFrame ::OnCreateClient put the following code:

    if(!m_SplitterWnd.CreateStatic(this, 1, 2))
    {
        TRACE("Failed to create splitter window");
        return FALSE;
    }
    pContext->m_pNewViewClass = RUNTIME_CLASS(CLeftView);
    m_SplitterWnd.CreateView(0, 0, pContext->m_pNewViewClass, CSize(0, 0), pContext);
    m_pLeftView=(CLeftView*)m_SplitterWnd.GetPane(0,0);
    CCreateContext context;
    context.m_pNewViewClass = pContext->m_pNewViewClass; //save original
    
    pContext->m_pNewViewClass = RUNTIME_CLASS(CRightView);
    m_SplitterWnd.CreateView(0, 1, pContext->m_pNewViewClass, CSize(0, 0), pContext);
    pContext->m_pNewViewClass = context.m_pNewViewClass; //return to original
    m_pRightView=(CRightView*)m_SplitterWnd.GetPane(0,1);
    
    int nWidth=rc.Width();
    m_SplitterWnd.SetColumnInfo(0, nWidth*0.25, 50);
    m_SplitterWnd.SetColumnInfo(1, nWidth*0.75, 50);
    

    CLeftView is an MFC CView derived class.

  3. In CLeftView declare a member variable CSplitterWnd m_SplitterWnd;

  4. In CLeftView::OnCreate, add the following code:

    CCreateContext *pContext = (CCreateContext*) lpCreateStruct->lpCreateParams;
    
    if(!m_SplitterWnd.CreateStatic(this, 2, 1, WS_CHILD | WS_VISIBLE, AFX_IDW_PANE_FIRST+8))
    {
        TRACE("Failed to create splitter window");
        return FALSE;
    }
    pContext->m_pNewViewClass = RUNTIME_CLASS(CPhongView);
    m_SplitterWnd.CreateView(0, 0, pContext->m_pNewViewClass, CSize(0, 0), pContext);
    m_pPhongView=(CPhongView*)m_SplitterWnd.GetPane(0, 0);
    CCreateContext context;
    context.m_pNewViewClass = pContext->m_pNewViewClass; //save original
    
    pContext->m_pNewViewClass = RUNTIME_CLASS(CPhongInfo);
    m_SplitterWnd.CreateView(1, 0, pContext->m_pNewViewClass, CSize(0, 0), pContext);
    pContext->m_pNewViewClass = context.m_pNewViewClass; //return to original
    m_pPhongInfo=(CPhongInfo*)m_SplitterWnd.GetPane(1, 0);
    

    CPhongInfo is a CFormView derived class, CPhong View is a CTreeView class.

  5. In CLeftView::OnSize, put the following code

    m_SplitterWnd.MoveWindow(0, 0, cx, cy);
    int nRow2 = 227;
    int nRow1 = cy - 227;
    m_SplitterWnd.SetRowInfo(0, nRow1, 0);
    m_SplitterWnd.SetRowInfo(1, nRow2, 0);
    m_SplitterWnd.RecalcLayout();
    
like image 146
ducva Avatar answered Jun 23 '26 14:06

ducva



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!