3

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  |                   |
|          |                    |
|          |                    |
----------------------------------
ducva
  • 63
  • 1
  • 7

2 Answers2

3

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();
    
MasterHD
  • 2,264
  • 1
  • 32
  • 41
ducva
  • 63
  • 1
  • 7
  • This would be a good answer if you would tidy up your code tags. –  Nov 12 '10 at 00:08
  • You are casting a CREATESTRUCT to a CCreateContext. That's not a valid cast. It is only working by sheer luck that the CREATESTRUCT started with zeros. – Charles Sep 21 '14 at 19:03
0

Maybe this helps: http://www.codeproject.com/Articles/6181/Simple-splitter-with-CWnd-derived-panes

enter image description here

With this class you can make several panes arranged either horizontally or vertically.

Cristi Stoica
  • 169
  • 2
  • 7