Creating a Splitter Window in a Dialog Box in Three Easy Steps
Posted
by Charles Bonneville
on May 5th, 2003
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:
- In the OnCreate function or your CDialog, register a new WindowClass by calling "AfxRegisterWndClass".
- Create a new CFrameWnd by using the "new" operator and initialize it.
- 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
good
Posted by jiangyu198558 on 09/09/2010 05:36amdiffrent
ReplyUse in dialog instanced in non dialog app
Posted by jfmedeirosneto on 09/15/2005 08:07amIn 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; }ReplyMy views don't receive any messages which I have declared in my message map!
Posted by Kyr on 11/02/2004 08:47amMy 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
Replyif add CListView and CEditView, how add CDocument
Posted by Legacy on 09/09/2003 12:00amOriginally posted by: ludean
the CView need CDocument, but how can I add CDocument with CView in CDialog? It is diffrent with SDI and MDI
-
Replydiffrent?
Posted by 541883216 on 05/29/2007 01:22pmwhat?
ReplyAssertion Failed _CrtIsValidHeapPointer(pUserData)
Posted by Legacy on 05/10/2003 12:00amOriginally 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?
ReplyVery Good!
Posted by Legacy on 05/07/2003 12:00amOriginally posted by: woodenbell
Beside some writing errors, your code and thinking is inpressed me! i like you work!
ReplyNice work
Posted by Legacy on 05/05/2003 12:00amOriginally posted by: Jibesh
Reply