在编程和实际开发过程中,我们经常需要将视图分割成像QQ那样的框架界面,在此,我将通过一个简单的例子说明视图分割的用法:
//以下例子是把视图分为左边一列,右边两行,共三个视图窗格
BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext)
{
//对整个主框架进行混合分割视图
BOOL bResult = m_wndSplitterV.CreateStatic(this, 1, 2);
ASSERT(bResult);
m_wndSplitterH.CreateStatic(&m_wndSplitterV, 2, 1, WS_CHILD | WS_VISIBLE, m_wndSplitterV.IdFromRowCol(0,1));
//创建各自子窗片的对应的视图
CRect rect;
GetClientRect(rect);
CSize paneSize1(rect.Width()/2, rect.Height());
m_wndSplitterV.CreateView(0, 0, RUNTIME_CLASS(CPractiseView), paneSize1, pContext);
m_wndSplitterV.GetPane(0, 0)->GetClientRect(rect);
CSize paneSize2(rect.Width(), rect.Height()/2);
m_wndSplitterH.CreateView(0, 0, RUNTIME_CLASS(CMyRightFirstView), paneSize2, pContext);
m_wndSplitterH.CreateView(1, 0, RUNTIME_CLASS(CMyRightSecondView), paneSize2, pContext);
m_bCreateSplitter=TRUE;
//激活PractiseView使得其可以接受命令消息
m_wndSplitterV.SetActivePane(0, 0, NULL);
return bResult;
}
//主框架窗体大小发生变化,调节相应的窗体大小
void CMainFrame::OnSize(UINT nType, int cx, int cy)
{
CFrameWnd::OnSize(nType, cx, cy);
if (m_bCreateSplitter)
{
CRect rect;
GetClientRect(rect);
m_wndSplitterV.SetColumnInfo(0, rect.Width() / 2, 10);
m_wndSplitterV.SetColumnInfo(1, rect.Width() / 2, 10);
m_wndSplitterV.GetPane(0, 0)->GetClientRect(rect);
m_wndSplitterH.SetRowInfo(0, rect.Height() / 2,10);
m_wndSplitterH.SetRowInfo(1, rect.Height() / 2,10);
m_wndSplitterV.RecalcLayout();
m_wndSplitterH.RecalcLayout();
}
}