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;
CCreateContext ccc;
ccc.m_pNewViewClass = RUNTIME_CLASS(CDialog1);
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),
::LoadIcon(NULL, IDI_APPLICATION));
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);
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;
GetDlgItem(IDC_CUTSOM_WINDOW)->GetWindowRect(&cRect);
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;
}