首先建立MFC AppWizard(exe),取名字为SplitterWnd,下一步,选择单文档“Single document”,其他默认,点击“完成”。
点击ResourceView,在Dialog下插入两个个Dialog窗口用于稍后的分割后的窗口,ID号分别改为“IDD_FORMVIEW1”和“IDD_FORMVIEW2”。记得及时保存。
单击ClassView标签,添加2个类,当然也可以从ClassWizard中添加,名字叫FormView1,类选择CFormView,Dialog ID选择IDD_FORMVIEW1,另一个类叫FormView2,ID选择IDD_FORMVIEW2。
在CMainFrame中添加两个成员变量,类型都为CSplitterWnd,变量名分别为m_splitter和m_splitter1,然后添加一个虚函数OnCreateClient,点击编辑代码,将如下代码更改
BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext) { // TODO: Add your specialized code here and/or call the base class return CFrameWnd::OnCreateClient(lpcs, pContext); }
改为
BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext) { //创建一个静态分栏窗口,分为一行二列 if(m_splitter.CreateStatic(this,1,2)==NULL) return FALSE;
//将CCSplitterWndView连接到0行0列窗格上 m_splitter.CreateView(0,0,RUNTIME_CLASS(CSplitterWndView),CSize(600,600), pContext);
if(m_splitter1.CreateStatic(&m_splitter,2,1,WS_CHILD|WS_VISIBLE, m_splitter.IdFromRowCol(0, 1))==NULL) return FALSE; //将第0行1列再分开2行1列 //将FormView1类连接到第二个分栏对象的0行0列 m_splitter1.CreateView(0,0,RUNTIME_CLASS(FormView1),CSize(200,400),pContext); //将FormView2类连接到第二个分栏对象的1行0列 m_splitter1.CreateView(1,0,RUNTIME_CLASS(FormView2),CSize(200,200),pContext); return TRUE;
}
添加头文件
#include "SplitterWndDoc.h" #include "SplitterWndView.h" #include "FormView1.h" #include "FormView2.h"
好了,现在编译运行,可以得到分栏的结果了