7,540
社区成员
发帖
与我相关
我的任务
分享
///////////////////////////////////////////////////////////
// Replace the current view in the splitter pSplitter
// at pane (row, col) by a new view of class pViewClass
void SwitchViewInSplitter( CSplitterWnd* pSplitter,
int row, int col,
CRuntimeClass* pViewClass )
{
ASSERT_VALID( pSplitter );
ASSERT( pViewClass != NULL );
ASSERT( pViewClass->
IsDerivedFrom( RUNTIME_CLASS( CView ) ) );
// 1 - Find the view to be replaced
CWnd* pPaneWnd = pSplitter->GetPane( row, col );
if( !pPaneWnd->IsKindOf( RUNTIME_CLASS( CView ) ) )
{
TRACE2( "Unable to switch: pane (%d,%d) is not a view\n",
row, col );
return;
}
CView* pCurrentView = static_cast<CView*>( pPaneWnd );
ASSERT_VALID( pCurrentView );
ASSERT_KINDOF( CView, pCurrentView );
if( pCurrentView->IsKindOf( pViewClass ) )
{
// No need to switch for same view class
return;
}
// 2 - Store current view position and activation state
CRect rcView;
pCurrentView->GetWindowRect( &rcView );
CView* pActiveView = pSplitter->
GetParentFrame()->GetActiveView();
BOOL bSaveActive = ( pActiveView == NULL )
|| ( pActiveView == pCurrentView );
// 3 - Find the associated document
CDocument* pDoc = pCurrentView->GetDocument();
ASSERT_VALID( pDoc );
// 4 - Make sure the document won't self-destruct
// when current view is destroyed
BOOL bSaveAutoDelete = pDoc->m_bAutoDelete;
pDoc->m_bAutoDelete = FALSE;
// 5 - Destroy the current view
pCurrentView->DestroyWindow();
// 6 - Restore document to initial state
pDoc->m_bAutoDelete = bSaveAutoDelete;
// 7 - Initialize creation context used by CreateView()
CCreateContext context;
context.m_pNewDocTemplate = NULL;
context.m_pLastView = NULL;
context.m_pCurrentFrame = NULL;
context.m_pNewViewClass = pViewClass;
context.m_pCurrentDoc = pDoc;
// 8 - Create the new view
pSplitter->CreateView( row, col, pViewClass,
rcView.Size(), &context );
CView* pNewView = static_cast<CView*>
( pSplitter->GetPane( row, col ) );
ASSERT_VALID( pNewView );
ASSERT_KINDOF( CView, pNewView );
// 9 - Position the new view like the old one and
// activate it if needed
pSplitter->ScreenToClient( &rcView );
pNewView->MoveWindow( &rcView, TRUE );
if( bSaveActive )
{
pSplitter->GetParentFrame()->SetActiveView( pNewView );
}
// 10 - Send WM_INITIALUPDATE to the view
pNewView->GetParentFrame()->InitialUpdateFrame( pDoc, TRUE );
}