1. 程式人生 > >Doc/View結構中的多檢視的切換

Doc/View結構中的多檢視的切換

{
    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
        }

    }

}