在 MFC MDI 程序中拆分子窗口 [英] Splitting Child Window in MFC MDI Program
问题描述
我正在尝试拆分我正在处理的 MFC MDI 程序的子窗口,但遇到了一些问题.我知道我必须使用 CSplitterWnd 类,并且一直在尝试按照此处帖子中的说明进行操作:
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:
使用 CSplitterWnd 在 CChildFrame 中创建多视图
但似乎无法使其正常工作,谁能就这些说明向我提供一些建议,我有一些具体问题:
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:
CRightView 是否也是 CView 派生类,如果有的话,应该在那里输入什么代码?
Is CRightView also a CView derived class and what code should go in there if any?
m_pLeftView、m_pRightView、m_pPhongView 和 m_pPhongInfo 是否都是相应类的变量,它们是否具有特定类型?
Are m_pLeftView, m_pRightView, m_pPhongView and m_pPhongInfo all variables of the appropriate classes and do they have any particular type?
CTreeView 是哪里来的,好像不是标准的基类?
Where does CTreeView come from, does not seem to be a standard base class?
CChildFrame::OnCreateClient 中的 rc.Width 给出了一个未声明的标识符错误,我应该在这里声明什么吗?
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.
谢谢
推荐答案
经过几天的努力,我已经设法解决了我自己的问题,我在这里添加了解决方案,供其他可能有同样的问题.
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.
声明两个视图类,在我的例子中,
CElement
View 是一个CWnd
派生类,而CSampleViewer3dView
是一个CView
派生类.
Declare the two view classes, in my case
CElement
View which is aCWnd
derived class andCSampleViewer3dView
which is aCView
derived class.
在CChildFrame
中添加一个访问private的变量,输入CSplitterWnd
并命名为m_wndSplitter
.
In CChildFrame
add a variable with access private, type CSplitterWnd
and name m_wndSplitter
.
为 CChildFrame
中的 OnCreateClient
函数添加覆盖,这应该添加代码:
Add an override for the OnCreateClient
function in CChildFrame
, this should add the code:
virtual BOOL OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext);
对于 ChildFrm.h,您还应该向 ChildFrm.h 添加一个布尔标志 m_bInitSplitter:
to ChildFrm.h, you should also add a boolean flag m_bInitSplitter to ChildFrm.h:
BOOL m_bInitSplitter;
您还必须添加:
m_bInitSplitter = false;
在 ChildFrm.cpp 的构造函数中,使用向导添加变量时,在 ChildFrm.cpp 中添加以下代码:
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)
{
}
将以下代码放入 CChildFrame::OnCreateClient:
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;
}
将 WM_SIZE
消息的覆盖添加到 CChildFrame
并插入以下代码:
Add an override for the WM_SIZE
message to CChildFrame
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();
}
}
您可以通过将 0.25 和 0.75 的值更改为您希望每个视图占据的屏幕所需百分比来编辑每个窗口的大小.
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.
- 将两个视图的头文件添加到
ChildFrm.cpp
,例如ElementView.h
和SampleViewer3dView.h
.
- Add header files for the two views to
ChildFrm.cpp
, e.g.ElementView.h
andSampleViewer3dView.h
.
然后您应该在 MDI 程序的子窗口中有两个独立的视图.
You should then have two independent views in the child window of an MDI program.
这篇关于在 MFC MDI 程序中拆分子窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!