Doc/View结构中的多视图的切换

多文档、多视图的切换

void  CMainFrame::SwitchView(CMultiDocTemplate *  pDocTemplate)
{
    CDocument
* pDoc = NULL;
    POSITION pos    
= pDocTemplate->GetFirstDocPosition();
    CView
* pActiveView      = GetActiveFrame()->GetActiveView();

    
if( pos == NULL )
    
{
        
// create new document
        pDoc = (CTouchNetDoc*)pDocTemplate->OpenDocumentFile(NULL);
    }

    
else
    
{
        
while (pos) 
        
{
            
// get the document object
            pDoc = pDocTemplate->GetNextDoc(pos);
            POSITION pos2 
= pDoc->GetFirstViewPosition();
            
if (pos2) 
            
{
                CView 
*pView = pDoc->GetNextView(pos2);
                CFrameWnd
* pNewFrame = pView->GetParentFrame();

                ASSERT(pNewFrame);
                
if ( pNewFrame->GetParentFrame() == this )
                    
break;
            }

        }


        
if ( pDoc == NULL )
            pDoc 
= (CTouchNetDoc*)pDocTemplate->OpenDocumentFile(NULL);
    }


    
// find the target view object of pDoc
    CView *pTargetView = NULL;
    pos 
= pDoc->GetFirstViewPosition();
    
while (pos) 
    
{
        CView 
*pView = pDoc->GetNextView(pos);
        
if ( pView )
        
{
            pTargetView 
= pView;
            
break;
        }

    }


    
if (pTargetView)
    
{
        
if ( pActiveView != pTargetView )
        
{
            
// show the frame window of target view.
            CFrameWnd* pNewFrame = (CFrameWnd* )pTargetView->GetParentFrame();
            pNewFrame
->ActivateFrame();
            pNewFrame
->SetActiveView(pTargetView);
            
            pTargetView
->OnInitialUpdate();
            
//call pTargetView to do something
        }

    }

}

 

单文档、多视图的切换

void  CMainFrame::SwitchView( const   int  nViewId)
{
    CView
* pOldView      = GetActiveView();
    CDocument
* pDoc = pOldView->GetDocument();

    CCreateContext context;
    context.m_pCurrentDoc 
= pDoc;
    CView
* pNewView = NULL;

    
switch(nViewId)
    
{
    
case eBrowseView:
        context.m_pNewViewClass 
= RUNTIME_CLASS(CBView1);
        
break;

    
case eHttpView:
        context.m_pNewViewClass 
= RUNTIME_CLASS(CView2);
        
break;

    
default:
        
break;
    }


    pNewView 
= (CView*)CreateView(&context);
    SetActiveView(pNewView);
    pNewView
->ShowWindow(SW_SHOW);
    pOldView
->ShowWindow(SW_HIDE);

    
// call pNewView to do something 

    pOldView
->DestroyWindow();

    RecalcLayout();
}

你可能感兴趣的:(Doc/View结构中的多视图的切换)