一、新建项目:项目名称为:MySplitterDlg,其他保持默认选项即可。
二、 创建对话框类
分别创类CMyFormView0 和CMyFormView1,基类别选CDialog,一定要选择CFormView。
类向导->添加类(MFC类):
三、 增加WM_CREATE消息响应函数OnCreate
通过“类向导”为“CMySpliterDlgDlg类(不要选择其它类)添加WM_CREATE的消息响应函数:
四、编辑OnCreate
添加了消息函数OnCreate之后,编辑其内容如下:
int CMySplitterDlgDlg::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CDialogEx::OnCreate(lpCreateStruct) == -1)
return -1;
// TODO: 在此添加您专用的创建代码
// 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,_T(""), WS_CHILD, CRect(0,0,300,300), this);
m_pMyFrame->ShowWindow(SW_SHOW);
// and finally, create the splitter with the frame as the parent
m_cSplitter.CreateStatic(m_pMyFrame,1, 2); //在Frame里切分视图窗口为1×2,就是一行两列
m_cSplitter.CreateView(0,0, RUNTIME_CLASS(CMyFormView0), CSize(100,100), NULL);//第一行一列
m_cSplitter.CreateView(0,1, RUNTIME_CLASS(CMyFormView1), CSize(100,100), NULL);//第一行二列
return 0;
}
五、添加CFrameWnd变量
由于上述程序中用到变量m_pMyFrame,因此需要在类CMySplitterDlgDlg中添加
public:
CFrameWnd *m_pMyFrame;
六、为了防止内存泄露,在onDestroy()中添加
if (m_pMyFrame) delete m_pMyFrame;
七、添加Dialog头文件
此外,由于上述程序还用到了3.3小结所创建的类CMyFormView0和CMyFormView1,因此需要在MySplitterDlgDlg.cpp中添加其头文件。
#include "MyFormView0.h"
#include "MyFormView1.h"
八、添加CSplitterWnd变量
由于分割窗口时使用到了CSplitterWnd类(见上述OnCreate函数),因此需要在类CMySplitterDlgDlg中添加
CSplitterWnd m_cSplitter;
九、编辑OnSize消息
添加了消息函数OnSize之后,编辑其内容如下:
void CMySplitterDlgDlg::OnSize(UINT nType, int cx, int cy)
{
CDialogEx::OnSize(nType, cx, cy);
// TODO: 在此处添加消息处理程序代码
CRect cRect;
GetWindowRect(&cRect);
ScreenToClient(&cRect);
m_pMyFrame->MoveWindow(&cRect);
m_pMyFrame->ShowWindow(SW_SHOW);
}
十、运行程序