在CMainFrame中定义变量:
CSplitterWnd m_wndSplitter;
BOOL CMainFrame::ReplaceView(int row, int col, CRuntimeClass *pViewClass, SIZE
size)
{
CCreateContext context;
BOOL bSetActive;
if ((this->m_wndSplitter.GetPane(row, col)->IsKindOf(pViewClass)) == TRUE)
return FALSE;
//获取文档对象的指针,以便在创建新视图的过程中能够使用它
CDocument *pDoc = ((CView*)m_wndSplitter.GetPane(row, col))->GetDocument();
CView *pActiveView = this->GetActiveView();
if (pActiveView == NULL || pActiveView == m_wndSplitter.GetPane(row, col))
bSetActive = TRUE;
else
bSetActive = FALSE;
pDoc->m_bAutoDelete = FALSE; //设置标志,这样当视图销毁时不会删除文档
((CView*)m_wndSplitter.GetPane(row, col))->DestroyWindow(); //删除存在的视图
pDoc->m_bAutoDelete = TRUE; //设回默认的标志
//创建新视图
context.m_pNewViewClass = pViewClass;
context.m_pCurrentDoc = pDoc;
context.m_pNewDocTemplate = NULL;
context.m_pLastView = NULL;
context.m_pCurrentFrame = NULL;
m_wndSplitter.CreateView(row, col, pViewClass, size, &context);
CView *pNewView = (CView*)m_wndSplitter.GetPane(row, col);
if (bSetActive == TRUE)
this->SetActiveView(pNewView);
m_wndSplitter.RecalcLayout(); //重新计算位置
// m_wndSplitter.GetPane(row,col)->SendMessage(WM_PAINT);
return TRUE;
}
BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext *pContext)
{
if (!m_wndSplitter.CreateStatic(this, 1, 2))
return FALSE;
if (!m_wndSplitter.CreateView(0, 0, RUNTIME_CLASS(CTView), CSize(200, 100),
pContext))
return FALSE;
if (!m_wndSplitter.CreateView(0, 1, RUNTIME_CLASS(CFView), CSize(100, 100),
pContext))
return FALSE;
m_bFormView = true;
return TRUE;
}
void CMainFrame::OnFormView()
{
ReplaceView(0, 1, RUNTIME_CLASS(CFView), CSize(100, 100));
m_bFormView = true;
}
void CMainFrame::OnListView()
{
ReplaceView(0, 1, RUNTIME_CLASS(CVVView), CSize(100, 100));
m_bFormView = false;
}
void CMainFrame::OnUpdateFormView(CCmdUI *pCmdUI)
{
pCmdUI->SetCheck(m_bFormView);
}
void CMainFrame::OnUpdateListView(CCmdUI *pCmdUI)
{
pCmdUI->SetCheck(!m_bFormView);
}