问题
I am trying to split the Child Window of an MFC MDI progarm that I am working on but am having some problems. I know I have to use the CSplitterWnd class and have been trying to follow the instructions on the post here:
Create multi views in a CChildFrame using CSplitterWnd
but can't seem to get it to work, would anyone be able to offer me some advice with regard to these instructions, I have some specific questions:
Is CRightView also a CView derived class and what code should go in there if any?
Are m_pLeftView, m_pRightView, m_pPhongView and m_pPhongInfo all variables of the appropriate classes and do they have any particular type?
Where does CTreeView come from, does not seem to be a standard base class?
rc.Width in CChildFrame::OnCreateClient gives an undeclared identifier error, is there something I should be declaring somewhere here?
I would appreciate any advice about this, really struggling to get the splitter to work.
Thanks
回答1:
After working on it for a couple of days I've managed to solve my own problem, I'm adding the solution here for anyone else who might have the same problem.
Declare the two view classes, in my case
CElement
View which is aCWnd
derived class andCSampleViewer3dView
which is aCView
derived class.In
CChildFrame
add a variable with access private, typeCSplitterWnd
and namem_wndSplitter
.Add an override for the
OnCreateClient
function inCChildFrame
, this should add the code:virtual BOOL OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext);
to ChildFrm.h, you should also add a boolean flag m_bInitSplitter to ChildFrm.h:
BOOL m_bInitSplitter;
you also have to add:
m_bInitSplitter = false;
to the constructor of ChildFrm.cpp, the following code is added to ChildFrm.cpp when you add the variable using the wizard:
BOOL CChildFrame::OnCreateClient(LPCREATESTRUCT /*lpcs*/, CCreateContext* pContext)
{
}
Put the following code into CChildFrame::OnCreateClient:
BOOL CChildFrame::OnCreateClient(LPCREATESTRUCT /*lpcs*/, CCreateContext* pContext) { CRect cr; GetWindowRect( &cr ); if (!m_wndSplitter.CreateStatic(this, 1, 2)) { MessageBox("Error setting up splitter frames!", "Init Error!", MB_OK | MB_ICONERROR); return FALSE; } if (!m_wndSplitter.CreateView( 0, 0, RUNTIME_CLASS(CElementView), CSize(cr.Width()/2, cr.Height()), pContext ) ) { MessageBox("Error setting up splitter frames!", "Init Error!", MB_OK | MB_ICONERROR); return FALSE; } if (!m_wndSplitter.CreateView( 0, 1, RUNTIME_CLASS(CSampleViewer3dView), CSize(cr.Width()/2, cr.Height()), pContext)) { MessageBox("Error setting up splitter frames!", "Init Error!", MB_OK | MB_ICONERROR); return FALSE; } m_bInitSplitter = TRUE; return TRUE; }
Add an override for the
WM_SIZE
message toCChildFrame
and insert the following code:void CChildFrame::OnSize(UINT nType, int cx, int cy) { CMDIChildWnd::OnSize(nType, cx, cy); CRect cr; GetWindowRect(&cr); if (m_bInitSplitter && nType != SIZE_MINIMIZED) { m_wndSplitter.SetRowInfo( 0, cy, 0 ); m_wndSplitter.SetColumnInfo(0, cr.Width()*0.25 / 2, 50); m_wndSplitter.SetColumnInfo(1, cr.Width()*0.75 / 2, 50); m_wndSplitter.RecalcLayout(); } }
You can edit the size of each window by changing the values of 0.25 and 0.75 to the required percentage of the screen that you want each view to take up.
- Add header files for the two views to
ChildFrm.cpp
, e.g.ElementView.h
andSampleViewer3dView.h
.
You should then have two independent views in the child window of an MDI program.
来源:https://stackoverflow.com/questions/5471536/splitting-child-window-in-mfc-mdi-program