Creating a Splitter Window in a Dialog Box in Three Easy Steps


This article was contributed by Charles Bonneville.

Environment: Visual C++

How to implement a CSplitterWnd into a CDialogBox in three easy steps without overriding any function, writing new classes, etc.

Because I'm not a writer, this article will take you directly to the main subject: how to implement a CSplitterWnd into a CDialogBox without overriding any MFC functions or writing new classes. The solution is simple; you just need to follow these steps:

  1. In the OnCreate function or your CDialog, register a new WindowClass by calling "AfxRegisterWndClass".
  2. Create a new CFrameWnd by using the "new" operator and initialize it.
  3. Create your splitter by using the new CFrameWnd you just created as the parent.

By using this technique, you don't need to override anything because the parent of the splitter is still a CFrameWnd. In the following sample, we create a splitter with two panes (with the same view type).

Sample :
  int CMyDialog::OnCreate(LPCREATESTRUCT lpCreateStruct)
  {
    if (CDialog::OnCreate(lpCreateStruct) == -1)
      return -1;

    // Initialize a context for the view. CDialog1 is my view and
    // is defined as :  class CDIalog1 : public CTreeView.
    CCreateContext ccc;
    ccc.m_pNewViewClass   = RUNTIME_CLASS(CDialog1);
    ccc.m_pCurrentDoc     = NULL;
    ccc.m_pNewDocTemplate = NULL;
    ccc.m_pLastView       = NULL;
    ccc.m_pCurrentFrame   = NULL;

    // Because the CFRameWnd needs a window class, we will create
    // a new one. I just copied the sample from MSDN Help.
    // When using it in your project, you may keep CS_VREDRAW and
    // CS_HREDRAW and then throw the other three parameters.
    CString strMyClass = AfxRegisterWndClass(CS_VREDRAW |
                         CS_HREDRAW,
                         ::LoadCursor(NULL, IDC_ARROW),
                         (HBRUSH) ::GetStockObject(WHITE_BRUSH),
                         ::LoadIcon(NULL, IDI_APPLICATION));

    // Create the frame window with "this" as the parent
    m_pMyFrame = new CFrameWnd;
    m_pMyFrame->Create(strMyClass,"", WS_CHILD,
                       CRect(0,0,1,1), this);
    m_pMyFrame->ShowWindow(SW_SHOW);
    m_pMyFrame->MoveWindow(0,0,300,300);

    // and finally, create the splitter with the frame as
    // the parent
    m_cSplitter.CreateStatic(pMyFrame,1, 2);
    m_cSplitter.CreateView(0,0, RUNTIME_CLASS(CDialog1),
                                CSize(100,100), &ccc);
    m_cSplitter.CreateView(0,1, RUNTIME_CLASS(CDialog1),
                                CSize(100,100), &ccc);
}

BOOL CMyDialog::OnInitDialog()
{
  CDialog::OnInitDialog();
  CRect cRect;

  // Get the rectangle of the custom window. The custom window
  // is just a a big button that is not visible and is disabled.
  // It's a trick to not use coordinates directly.
  GetDlgItem(IDC_CUTSOM_WINDOW)->GetWindowRect(&cRect);

  // Move the splitter
  ScreenToClient(&cRect);
  m_pFrameWnd->MoveWindow(&cRect);
  m_pFrameWnd->ShowWindow(SW_SHOW);
  m_cSplitter.MoveWindow(0,0, cRect.Width(), cRect.Height());
  m_cSplitter.ShowWindow(SW_SHOW);

  return TRUE;  // return TRUE unless you set the focus to a
                // control
                // EXCEPTION: OCX Property Pages should return
                // FALSE
}


Comments

  • I needed it

    Posted by Evgeni on 02/21/2014 01:40am

    respect

    Reply
  • good

    Posted by jiangyu198558 on 09/09/2010 05:36am

    diffrent

    Reply
  • Use in dialog instanced in non dialog app

    Posted by jfmedeirosneto on 09/15/2005 08:07am

    In dialog instanced in non dialog app I need to change the following code :
    
    void CSplitterControl::SetRange(int nSubtraction, int nAddition, int nRoot)
    {
    	if (nRoot < 0)
    	{
    		CRect rcWnd;
    		GetWindowRect(rcWnd);
    		GetParent()->ScreenToClient(&rcWnd); // Need for dialog instanced in non dialog app
    		if (m_nType == SPS_VERTICAL)
    			nRoot = rcWnd.left + rcWnd.Width() / 2;
    		else // if m_nType == SPS_HORIZONTAL
    			nRoot = rcWnd.top + rcWnd.Height() / 2;
    	}
    	m_nMin = nRoot - nSubtraction;
    	m_nMax = nRoot + nAddition;
    }

    Reply
  • My views don't receive any messages which I have declared in my message map!

    Posted by Kyr on 11/02/2004 08:47am

    My dialog appears with the splitterWnd and the views. Looks very nice but when I show a context menu in one of the views the menu items are disabled (because the corresponding message handler isn't found). How can I solve this problem? Greetings, Ruben

    Reply
  • if add CListView and CEditView, how add CDocument

    Posted by Legacy on 09/09/2003 12:00am

    Originally posted by: ludean

    the CView need CDocument, but how can I add CDocument with CView in CDialog? It is diffrent with SDI and MDI

    • diffrent?

      Posted by 541883216 on 05/29/2007 01:22pm

      what?

      Reply
    Reply
  • Assertion Failed _CrtIsValidHeapPointer(pUserData)

    Posted by Legacy on 05/10/2003 12:00am

    Originally posted by: activecode

    It's realy a good idea, but I'm getting a _CrtIsValidHeapPointer(pUserData) error right when I quit my application. I put most of the code here:
    CCreateContext ccc;
    ccc.m_pNewViewClass = RUNTIME_CLASS(CLeftTreeView);
    ccc.m_pCurrentDoc = NULL;
    ccc.m_pNewDocTemplate = NULL;
    ccc.m_pLastView = NULL;
    ccc.m_pCurrentFrame = NULL;

    CString strMyClass = AfxRegisterWndClass(CS_VREDRAW |
    CS_HREDRAW,
    ::LoadCursor(NULL, IDC_ARROW),
    (HBRUSH) ::GetStockObject(WHITE_BRUSH),
    NULL);

    m_wndFrame.Create(strMyClass,_T("inner frame"), WS_CHILD,
    CRect(0,0,1,1), this);
    m_wndSplitter.CreateStatic(&m_wndFrame, 1, 2);
    m_wndSplitter.CreateView(0, 0, RUNTIME_CLASS(CLeftTreeView),
    CSize(100,100), &ccc);
    ccc.m_pNewViewClass = RUNTIME_CLASS(CRightListView);
    ccc.m_pCurrentDoc = NULL;
    ccc.m_pNewDocTemplate = NULL;
    ccc.m_pLastView = NULL;
    ccc.m_pCurrentFrame = NULL;
    m_wndSplitter.CreateView(0, 1, RUNTIME_CLASS(CRightListView),
    CSize(100,100), &ccc);

    Is there a problem?

    Reply
  • Very Good!

    Posted by Legacy on 05/07/2003 12:00am

    Originally posted by: woodenbell

    Beside some writing errors, your code and thinking is inpressed me! i like you work!

    Reply
  • Nice work

    Posted by Legacy on 05/05/2003 12:00am

    Originally posted by: Jibesh

    Good Work Charles..
    
    

    By the way i worked with Two way splitter ie spliting the Right View Horizontally,and it was so simple ..

    // Creating Left View

    m_cSplitter.CreateStatic(pMyFrame,1, 2);
    m_cSplitter.CreateView(0,0, RUNTIME_CLASS(CDialog1),
    CSize(100,100), &ccc);

    m_cSplitter2.CreateStatic( &m_cSplitter,2,1,
    WS_CHILD|WS_VISIBLE|WS_BORDER,
    m_cSplitter.IdFromRowCol(0,1));//ie use Second
    // pane to split Horizontally

    m_cSplitter2.CreateView(0,0, RUNTIME_CLASS(CDialog1),
    CSize(100,100), &ccc);
    m_cSplitter2.CreateView(1,0, RUNTIME_CLASS(CDialog1),
    CSize(100,100), &ccc);


    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: December 11, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Market pressures to move more quickly and develop innovative applications are forcing organizations to rethink how they develop and release applications. The combination of public clouds and physical back-end infrastructures are a means to get applications out faster. However, these hybrid solutions complicate DevOps adoption, with application delivery pipelines that span across complex hybrid cloud and non-cloud environments. Check out this …

  • On-demand Event Event Date: October 29, 2014 It's well understood how critical version control is for code. However, its importance to DevOps isn't always recognized. The 2014 DevOps Survey of Practice shows that one of the key predictors of DevOps success is putting all production environment artifacts into version control. In this webcast, Gene Kim discusses these survey findings and shares woeful tales of artifact management gone wrong! Gene also shares examples of how high-performing DevOps …

Most Popular Programming Stories

More for Developers

RSS Feeds