MFC单文档分割区(CSplitterWnd)
用VS08程序向导,单文档程序,默认设置生成的。工程名为3view;
其中默认生成的视图类CMy3viewView,对应3viewView.h,3viewView.cpp;
在Resourse View添加个新对话框,在对话框的属性中,Border设置成None;Style设置成Child;
生成的新类中Base class设置为CFormView; 本程序中定义类名为CDefFormView;
默认生成的文档类为:3viewDoc.h,3viewDoc.cpp;
在主框架头文件MainFrm.h中添加如下头文件; //为了调用程序默认的视类CMy3viewView;
#include "3viewDoc.h"
#include "3viewView.h"
并添加如下变量
CSplitterWnd m_wndSplitter;
CSplitterWnd m_wndSPlitterTB;
CSplitterWnd m_wndSPlitterTB2;
BOOL m_is_spilit //用来判断分割窗口是否成功,做为窗口自适应大小的变量
并重载OnCreateClient函数
BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext) { // TODO: Add your specialized code here and/or call the base class CRect ct; GetClientRect(&ct); //主框架左右分成1行3列 m_wndSplitter.CreateStatic(this,1,3); //在m_wndSplitter的第1行第1列分割区成2行1列 m_wndSPlitterTB.CreateStatic(&m_wndSplitter,2,1,WS_CHILD|WS_VISIBLE,m_wndSplitter.IdFromRowCol(0,0)); //在m_wndSplitter的第1行第2列分割区成2行1列 m_wndSPlitterTB2.CreateStatic(&m_wndSplitter,3,1,WS_CHILD|WS_VISIBLE,m_wndSplitter.IdFromRowCol(0,1)); //在m_wndSPlitterTB中创建第1行第1列视图 m_wndSPlitterTB.CreateView(0,0,RUNTIME_CLASS(CDefFormView),CSize(ct.Width()/3,ct.Height()/2),pContext); //在m_wndSPlitterTB中创建第2行第1列视图 m_wndSPlitterTB.CreateView(1,0,RUNTIME_CLASS(CDefFormView),CSize(ct.Width()/3,ct.Height()/2),pContext); //在m_wndSPlitterTB2中创建第1行第1列视图 m_wndSPlitterTB2.CreateView(0,0,RUNTIME_CLASS(CDefFormView),CSize(ct.Width()/3,ct.Height()/3),pContext); //在m_wndSPlitterTB2中创建第2行第1列视图 m_wndSPlitterTB2.CreateView(1,0,RUNTIME_CLASS(CMy3viewView),CSize(ct.Width()/3,ct.Height()/3),pContext); //在m_wndSPlitterTB2中创建第3行第1列视图 m_wndSPlitterTB2.CreateView(2,0,RUNTIME_CLASS(CDefFormView),CSize(ct.Width()/3,ct.Height()/3),pContext); //在m_wndSPlitter中创建第1行第3列的视图
m_wndSplitter.CreateView(0,2,RUNTIME_CLASS(CMy3viewView),CSize(ct.Width()/3,0),pContext);
m_wndSplitter.SetColumnInfo(0,ct.Width()/3,50);
m_wndSplitter.SetColumnInfo(1,ct.Width()/3,50);
m_wndSplitter.SetColumnInfo(2,ct.Width()/3,50);
//获取对应视图的对象 CMy3viewView* view = (CMy3viewView *)m_wndSplitter.GetPane(0,2); CDefFormView* view2 = (CDefFormView*)m_wndSPlitterTB.GetPane(0,0); return TRUE; //return CFrameWnd::OnCreateClient(lpcs, pContext); }
重载OnSize函数:(主框架大小变化是分割窗口自适应大小)
void CMainFrame::OnSize(UINT nType, int cx, int cy) { CFrameWnd::OnSize(nType, cx, cy); // TODO: Add your message handler code here if(m_is_spilit) { CRect ct; GetClientRect(&ct); m_wndSplitter.SetRowInfo(0,ct.Height(),0); m_wndSplitter.SetColumnInfo(0,ct.Width()/3,50); m_wndSplitter.SetColumnInfo(1,ct.Width()/3,50); m_wndSplitter.SetColumnInfo(2,ct.Width()/3,50); m_wndSPlitterTB.SetRowInfo(0,ct.Height()/2,50); m_wndSPlitterTB.SetRowInfo(1,ct.Height()/2,50); m_wndSPlitterTB2.SetRowInfo(0,ct.Height()/3,50); m_wndSPlitterTB2.SetRowInfo(1,ct.Height()/3,50); m_wndSPlitterTB2.SetRowInfo(2,ct.Height()/3,50); m_wndSplitter.RecalcLayout(); } }
生成的程序如图显示: