用MFC开发一个软件界面中需要拆分多个试图窗口时,使用CSplitterWnd类
CSplitterWnd类主要用在创建一个拆分试图窗口。通常嵌入在框架窗口中(CMainFrame)
创建步骤:
1.在框架类(CMainFrame)中定义一个CSplitterWnd成员;
2.重载父框架类中CFrameWnd::OnCreateClient函数;
3.在OnCreateClient()函数中调用CSplitterWnd类的Create或CreateStatic()函数;
例子:
CSplitterWnd m_wndSplitter;
BOOL CChildFrame::OnCreateClient( LPCREATESTRUCT lpcs,
CCreateContext* pContext)
{
BOOL bCreateSpltr = m_wndSplitter.CreateStatic( this, 2, 1);
// COneView and CAnotherView are user-defined views derived from CMDIView
m_wndSplitter.CreateView(0,0,RUNTIME_CLASS(COneView), CSize(0,0),
pContext);
m_wndSplitter.CreateView(1,0,RUNTIME_CLASS(CAnotherView), CSize(0,0),
pContext);
return (bCreateSpltr);
}
或者:
BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext)
{
// TODO: Add your specialized code here and/or call the base class
if (!m_wndSplitter.CreateStatic(this, 1, 2))
return FALSE;
if (!m_wndSplitter.CreateView(0, 0, RUNTIME_CLASS(CLeftView), CSize(160, 200), pContext) ||
!m_wndSplitter.CreateView(0, 1, RUNTIME_CLASS(CTestView), CSize(100, 200), pContext))
{
m_wndSplitter.DestroyWindow();
return FALSE;
}
return TRUE;
}
在创建了多个窗口之后,有时为了能够得到其中的某个窗口,进而对其进行操作控制,则:
不能简单使用GetActiveView,可从MainFrame的CSplitterWnd成员得到,如下
CMainFrame* pMF=(CMainFrame*)AfxGetApp()->m_pMainWnd;
CViewRes* pViewRes=(CViewRes*)pMF->m_wndSplitter.GetPane(0,1);
注意:1, 使用CMainFrame,要在调用的cpp文件中包含MainFrame.h
2, 注意在CMainFrame中,m_wndSplitter变量的类型,若定义为protected或private则可能导致不可引用等错误。
(创建3个分割窗口)
CSplitterWnd使用。(创建3个分割窗口)
xxxx::OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext)
{
//---------------------------------------------------------
//m_wndSpliter,m_wndSpliter2均为成员变量,CSplitterWnd类别
//CTest1,CTest2,CTest3均继承于CView
//---------------------------------------------------------
//第一次分割左右两个窗口
m_wndSpliter.CreateStatic( this, 1, 2 );
m_wndSpliter.CreateView( 0,1, RUNTIME_CLASS(CTest2), CSize(0,0), pContext );
m_wndSpliter.SetColumnInfo( 0, 700, 50 );
//第二次分割上下两个窗口
m_wndSpliter2.CreateStatic( &m_wndSpliter, 2, 1, WS_CHILD|WS_VISIBLE, m_wndSpliter.IdFromRowCol(0,0) );
m_wndSpliter2.CreateView( 0, 0, RUNTIME_CLASS(CTest1), CSize(0,0), pContext );
m_wndSpliter2.CreateView( 1, 0, RUNTIME_CLASS(CTest3), CSize(0,0), pContext );
m_wndSpliter2.SetRowInfo( 0, 300, 50 );
return TRUE; //重要
}
======================================================一个完整Demo:
Demo下载地址:FlatSplitter_src.zip