使用CSplitterWnd在CChildFrame中创建多个视图

时间:2010-11-07 10:13:49

标签: mfc

我正在使用MFC MDI。我需要创建如下视图。我的ChildWnd分为两部分。它们是LeftView,它是一个CView和RightView,它是一个CScrollView。 LeftView分为两部分,TreeView和FormView。我怎么能这样做?

_________________________________
|          |                    |
|          |                    |
|CTreeView  |                   |
|          |                    |
|          |                    |
|          |        CScrollView |
|___________|                   |
|          |                    |
|          |                    |
|CFormView  |                   |
|          |                    |
|          |                    |
----------------------------------

2 个答案:

答案 0 :(得分:3)

我终于解决了这个问题。我为有类似问题的人写过解决方案。

  1. 在CChildFrame类中,声明CSplitterWnd m_SplitterWnd;

  2. 在CChildFrame :: OnCreateClient中输入以下代码:

    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是一个MFC CView派生类。

  3. 在CLeftView中声明成员变量CSplitterWnd m_SplitterWnd;

  4. 在CLeftView :: OnCreate中,添加以下代码:

    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是一个CFormView派生类,CPhong View是一个CTreeView类。

  5. 在CLeftView :: OnSize中,输入以下代码

    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();
    

答案 1 :(得分:0)

也许这会有所帮助: http://www.codeproject.com/Articles/6181/Simple-splitter-with-CWnd-derived-panes

enter image description here

  

使用此课程,您可以制作水平或垂直排列的多个窗格。