1. 创建对话框工程
2. 对话框类添加成员变量
CFrameWnd * m_pMyFrame; // 一个框架类对象(指针,也可以静态创建,注意命名!)
// 在构造函数中 赋值 NULL
// 在OnDestroy()中 delete 资源(动态创建)
CSplitterWnd m_SplitterWnd; // 分割条对象
3. 类视图添加两个 派生自 CView 的类
在类向导中添加 MFC 类,只要基于 根据需要选择功能不同的 CView 类,如果选择的是 CFormView ,此时在资源视图会为这两个类自动添加对话框资源,可以手动在这两个对话框中添加控件。
4. dlg.cpp 代码修改:
4.1 OnCreate()
GetClientRect(m_recClient);
// 注册主框架窗口类
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, _T(""), WS_CHILD | WS_VISIBLE, m_recClient, this);
// 创建分割条作为框架窗口的子窗口
m_SplitterWnd.CreateStatic(m_pMyFrame, 1, 2); // 在 Frame 里切分视图窗口为1×2,就是一行两列
m_SplitterWnd.CreateView(0, 0, RUNTIME_CLASS(CFrameViewTree), CSize((int)(m_recClient.Width()*0.5), m_recClient.Height()), NULL);//第一行一列
m_SplitterWnd.CreateView(0, 1, RUNTIME_CLASS(CFrameViewList), CSize((int)(m_recClient.Width()*0.5), m_recClient.Height()), NULL);//第一行二列
// 设置当前活动窗口
m_SplitterWnd.SetActivePane(0, 0);
4.2 OnSize()
// TODO: 在此处添加消息处理程序代码
GetClientRect(m_recClient);
m_pMyFrame->MoveWindow(&m_recClient);
// 调整窗口划分比例 // 这里也可以直接用传进来的 cx cy,不用 GetClientRect
m_SplitterWnd.SetColumnInfo(0, (int)(m_recClient.Width() * 0.25), 50);//改变大小
m_SplitterWnd.SetColumnInfo(1, (int)(m_recClient.Width() * 0.75), 50);
m_SplitterWnd.SetRowInfo(0, m_recClient.Height(), 50);
// 上述参数调整完了以后,重新绘制分割条, 防止缩小后,分割条位置错乱
m_SplitterWnd.RecalcLayout();
4.3 OnDestroy()
if (m_pMyFrame)
{
delete m_pMyFrame;
}
4.4 OnInitDialog()
如果没有这一步会发现,刚开始生成的对话框没有显示子窗口,必须改变大小才能绘制出来,这里手动绘制了一下。
// TODO: 在此添加额外的初始化代码
// 设置窗口屏幕居中
CRect recTemp;
CClientDC dc(this);
int width = dc.GetDeviceCaps(HORZRES);
int height = dc.GetDeviceCaps(VERTRES);
recTemp.left = (int)(width * 0.3);
recTemp.right = (int)(width * 0.7);
recTemp.top = (int)(height * 0.1);
recTemp.bottom = (int)(height * 0.9);
MoveWindow(recTemp);