最近遇到需要分割窗体的问题,查询了好几天,找了很多代码,但是很多都是不能用的,经过删选,选出几个比较合理的给大家参考下:
第一个比较好的代码:
//成员变量声明
CFixSplitterWnd m_wndSplitterH; //用于横向切割
CFixSplitterWnd m_wndSplitterV; //用于纵向切割
BOOL m_bCreateSplitter;
//分割窗体的实现
BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext)
{
//对整个主框架进行混合分割视图
BOOL bResult=m_wndSplitterV.CreateStatic(this,1,2);
ASSERT(bResult);
m_wndSplitterH.CreateStatic(&m_wndSplitterV,4,1,WS_CHILD | WS_VISIBLE,m_wndSplitterV.IdFromRowCol(0,1));
//创建各自子窗片的对应的视图
m_wndSplitterV.CreateView(0,0,RUNTIME_CLASS(CSceneView),CSize(600,600),pContext);
m_wndSplitterH.CreateView(0,0,RUNTIME_CLASS(CPitchView),CSize(100,100),pContext);
m_wndSplitterH.CreateView(1,0,RUNTIME_CLASS(CYawView),CSize(100,100),pContext);
m_wndSplitterH.CreateView(2,0,RUNTIME_CLASS(CRollView),CSize(100,100),pContext);
m_wndSplitterH.CreateView(3,0,RUNTIME_CLASS(CControlView),CSize(100,100),pContext);
//设置窗格的初始化的大小
m_wndSplitterV.SetRowInfo(0,IDEAL_RAWHEIGHT,0);
m_bCreateSplitter=TRUE;
//激活sceneview使得其可以接受命令消息
m_wndSplitterV.SetActivePane(0,0,NULL);
return bResult;
}
//主框架窗体大小发生变化,调节相应的窗体大小
void CMainFrame::OnSize(UINT nType, int cx, int cy)
{
CMDIFrameWnd::OnSize(nType, cx, cy);
CRect rect;
GetClientRect(rect);
if (m_bCreateSplitter)
{
m_wndSplitterV.SetColumnInfo(0,rect.Width() *3/4,10);
m_wndSplitterV.SetColumnInfo(1,rect.Width() *1/4,10);
m_wndSplitterH.SetRowInfo(0,rect.Height() /6,10);
m_wndSplitterH.SetRowInfo(1,rect.Height() /6,10);
m_wndSplitterH.SetRowInfo(2,rect.Height() /6,10);
m_wndSplitterH.SetRowInfo(3,rect.Height()/2,10);
}
m_wndSplitterV.RecalcLayout();
m_wndSplitterH.RecalcLayout();
}
这个代码是先分完后赋予View,分为水平分割和垂直分割,感觉思路不是很清晰。但是它的窗体大小随主框架大小变化的方法可以借鉴。
第二个比较合理的:
BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext) { m_wndSplitter.CreateStatic(this,1,2); //创建分割窗口 //创建视图 m_wndSplitter.CreateView(0,0,RUNTIME_CLASS(CRandomDivisionView), CSize(100,200), pContext); m_ChildWnd.CreateStatic(&m_wndSplitter,2,1,WS_CHILD|WS_VISIBLE, m_wndSplitter.IdFromRowCol(0,1)); //为子窗口创建分割窗口 m_ChildWnd.CreateView(0,0,RUNTIME_CLASS(CRandomDivisionView), CSize(100,100),pContext); m_ChildWnd1.CreateStatic(&m_ChildWnd,1,2,WS_CHILD|WS_VISIBLE, m_ChildWnd.IdFromRowCol(1,0)); //为子窗口创建分割窗口 m_ChildWnd1.CreateView(0,0,RUNTIME_CLASS(CRandomDivisionView), CSize(100,100),pContext); m_ChildWnd2.CreateStatic(&m_ChildWnd1,2,1,WS_CHILD|WS_VISIBLE, m_ChildWnd1.IdFromRowCol(0,1)); //为子窗口创建分割窗口 m_ChildWnd2.CreateView(0,0,RUNTIME_CLASS(CRandomDivisionView), CSize(100,100),pContext); m_ChildWnd3.CreateStatic(&m_ChildWnd2,1,2,WS_CHILD|WS_VISIBLE, m_ChildWnd2.IdFromRowCol(1,0)); //为子窗口创建分割窗口 m_ChildWnd3.CreateView(0,0,RUNTIME_CLASS(CRandomDivisionView), CSize(100,100),pContext); m_ChildWnd3.CreateView(0,1,RUNTIME_CLASS(CRandomDivisionView), CSize(100,100),pContext); return TRUE; //设置返回值 //return CFrameWnd::OnCreateClient(lpcs, pContext); //不调用基类的方法 } 这个ChildWnd应该是wndSplitter的对象,用这个比上面的m_wndSplitterH和m_wndSplitterV感觉要思路清晰的多,可以明确看出来哪部分代码是在对子窗口分割,而且它采用分完后先赋View,对需要的再次分割的窗体再用CreateStatic分割,而且ChildWnd标出1,2,3,感觉层次关系要好的多,这样分多少次,感觉也不乱。
第三个方法:
BOOL CMainFrame::OnCreateClient( LPCREATESTRUCT /*lpcs*/, CCreateContext* pContext) { //创建一个静态分栏窗口,分为三行一列 if(m_wndSplitter1.CreateStatic(this,3,1)==NULL) return FALSE; //将CCuteFTPView连接到0行0列窗格上 m_wndSplitter1.CreateView(0,0,RUNTIME_CLASS(CCuteFTPView),CSize(100,100), pContext); m_wndSplitter1.CreateView(2,0,RUNTIME_CLASS(CView4),CSize(100,100),pContext); //将CView4连接到0行2列 if(m_wndSplitter2.CreateStatic(&m_wndSplitter,1,2,WS_CHILD|WS_VISIBLE, m_wndSplitter.IdFromRowCol(1, 0))==NULL) return FALSE; //将第1行0列再分开1行2列 //将CView2类连接到第二个分栏对象的0行0列 m_wndSplitter2.CreateView(0,0,RUNTIME_CLASS(CView2),CSize(400,300),pContext); //将CView3类连接到第二个分栏对象的0行1列 m_wndSplitter2.CreateView(0,1,RUNTIME_CLASS(CView3),CSize(400,300),pContext); return TRUE; }说实话,兄弟没有看懂这个,其中的if语句的作用,兄弟没明白,希望过路的大侠给小弟解释下。