Splitter Window是一种分割栏,可以框架分成多个不同的部分。在添加Splitter Window之前,我们有必要正式的了解一下CSplitterWnd类。
CSplitterWnd在MSDN上是如下解释的;
Provides the functionality of a splitter window, which is a window that contains multiple panes。
为一个包含多个面板区的分割窗提供函数功能。
一个CSpliterWnd对象通常嵌入在CFrameWnd或者CMDIChildWnd对象中,创建一个CSplitterWnd一般有如下步骤:
1、在CFrameWnd中或者CMDIChildWnd中添加一个成员变量
2、重载父框架CFrameWnd或者CMDIChildWnd的OnCreateClient成员函数。
3.在OnCreateClient成员函数中使用CSplitterWnd的Create或者CreateStatic创建动态或者静态分割窗。
接下来,我们将先了解CSplitterWnd的一下成员函数,再在实际的例子中创建一个Splitter Window。
virtual BOOL Create(CWnd* pParentWnd,int nMaxRows,int nMaxCols,SIZE sizeMin,CCreateContext* pContext,
DWORD dwStyle = WS_CHILD | WS_VISIBLE | WS_HSCROLL | WS_VSCROLL | SPLS_DYNAMIC_SPLIT, UINT nID = AFX_IDW_PANE_FIRST);
virtual BOOL CreateStatic(CWnd* pParentWnd,int nRows,int nCols,DWORD dwStyle = WS_CHILD | WS_VISIBLE,UINT nID = AFX_IDW_PANE_FIRST);
virtual BOOL CreateView(int row,int col, CRuntimeClass* pViewClass,SIZE sizeInit,CCreateContext* pContext );
上述三个函数是比较常用的三个函数,分别是动态分割窗的创建函数、静态分割窗创建函数和视图填充函数。
下面我们来建立一个实际SDI的例子。首先创建一个SDI,我们可以有两种方式的来创建(我的环境是VS2008),一种在构建SDI中勾上Split Window选项,如下图。立。
CRect rect; GetClientRect(&rect); if (!m_CSplitterWnd.CreateStatic(this,1,3)) { return FALSE; } if (!m_CSplitterWnd.CreateView( 0, 0, RUNTIME_CLASS(CCameraManageDemoView), CSize(rect.Width()/6, rect.Height()), pContext ) ) { return FALSE; } if ( !m_CSplitterWnd.CreateView( 0, 1, RUNTIME_CLASS(CImageDispView), CSize(rect.Width()*2/3, rect.Height()), pContext ) ) { return FALSE; } if ( !m_CSplitterWnd.CreateView( 0, 2, RUNTIME_CLASS(CImageControlView), CSize(rect.Width()/6, rect.Height()), pContext ) ) { return FALSE; } return TRUE;
3.如代码所示,我们使用了两个三个视图,分别为CCameraManageDemoView、CImageDispView、CImageDispView。我们在右击添加类,base class为CFormView。
4、创建好CFormView类将他们include到CMainFrm的CPP文件中.
5、最后在新增加的视图类的头文件中include "CCameraManageDemoDoc.h"。因为View中需要使用document中的函数。
运行后就能出来split window。