用MFC開發一個軟件界面中需要拆分多個試圖窗口時,使用CSplitterWnd類
創建步驟:
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則可能導致不可引用等錯誤
OK,做完了之后,碰到的問題就是如何控制分割出來窗口的大小了,我想做成固定窗口,現在還沒找到有效方法~~~