写了东西,一个doc对应好多种不同的view
现在需要在么多种view中动态的现实某一个
就是说,现在需要这个view就显示这个,需要那个就显示那个
不需要的就delete掉
怎么实现呢?

解决方案 »

  1.   

    CDocTemplate::CreateNewFrame()函数创建MFC MDI应用程序中的文档的附加视图。为了调用该函数,要指定一个指向CDocument对象(指将为之建立视图的文档)的指针和一个指向可从中复制属性的框架窗口的指针。一般情形下,该函数的第二个参数为NULL。 当应用程序调用函数CreateNewFrame()时,该函数就创建一个框架窗口和在该窗口内的视图。框架窗口和它的视图的类型由与CreateNewFrame()函数调用指定的文档相关的文档摸板(CDocTemplate)决定。 CreateNewFrame()函数建立了一个框架和一个视图,而不仅仅是一个视图。假如CreateNewFrame()函数不能完全符合你的需要,可参考CreateNewFrame()函数的源程序来了解对建立结构和视图所必须的步骤。 
    辅助函数:
    ///////////////////////////////////////////////////////////
    // Create a new view based on the document template
    // parameter and associated with the given document objectCFrameWnd* UserCreateNewWindow( CDocTemplate* pTemplate,
                                  CDocument* pDocument )
    {
    ASSERT_VALID( pTemplate );
    ASSERT_VALID( pDocument );// 1 - Create the new frame window
    // (will in turn create the associated view)
    CFrameWnd* pFrame = pTemplate->CreateNewFrame(pDocument, NULL );
    if( pFrame == NULL )
    {
    // Window creation failed
    TRACE0( "Warning: failed to create new frame.\n" );
    return NULL;
    }
    ASSERT_KINDOF( CFrameWnd, pFrame );// 2 - Tell the frame to update itself
    // (and its child windows)
    pTemplate->InitialUpdateFrame( pFrame, pDocument );// 3 - Return a pointer to the new frame window object
    return pFrame;
    }
      

  2.   

    教你更换视图:要看你具体想如何实现,如果你想在一个窗口里同时显示两个视的话,那就要拆分。建
    立SDI时选择window Style里的use split window,然后调用
    CSplitterWnd::CreatStatic来放置你的view
    BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT ,CCreateContext*pContext)
    {
    VERIFY(m_wndSplitter.CreateStatic(this,2,1));
    VERIFY(m_wndSplitter.CreateView(0,0.RUNTIME_CLASS(ViewA),CSize(100,100),pCon
    text);
    VERIFY(m_wndSplitter.CreateView(1,0.RUNTIME_CLASS(ViewB),CSize(100,100),pCon
    text);
    return TRUE;
    }
    如果你想切换,一次只显示一个视(例如UltraEdit那样,在16进和ASCII间切换),可以
    使用SwitchToView
    (但我没在MSDN里见过它),它有个enum参数告诉函数切换到那个视图.
    enum eView { ViewA = 1, ViewB = 2 };
    void SwitchToView(eView nView);
    void CMainFrame::SwitchToView(eView nView)
    {
    CView* pOldActiveView = GetActiveView();
    CView* pNewActiveView = (CView*) GetDlgItem(nView);
    if (pNewActiveView == NULL) {
    switch (nView) {
    case ViewA:
    pNewActiveView = (CView*) new CViewA;
    break;
    case ViewB:
    pNewActiveView = (CView*) new CViewB;
    break;
    }
    CCreateContext context;
    context.m_pCurrentDoc = pOldActiveView->GetDocument();
    pNewActiveView->Create(NULL, NULL, WS_BORDER,
    CFrameWnd::rectDefault, this, nView, &context);
    pNewActiveView->OnInitialUpdate();
    }
    SetActiveView(pNewActiveView);
    pNewActiveView->ShowWindow(SW_SHOW);
    pOldActiveView->ShowWindow(SW_HIDE);
    pOldActiveView->SetDlgCtrlID(
    pOldActiveView->GetRuntimeClass() ==
    RUNTIME_CLASS(CViewA) ? ViewA : View B);
    pNewActiveView->SetDlgCtrlID(AFX_IDW_PANE_FIRST);
    RecalcLayout();
    }
    当然,你也可以用CDocument::AddView和CDocument::RemoveView做,具体可以参见
    MSDN的Sample
    BOOL CMainFrame::OnViewChange(UINT nCmdID)
    {
    CView* pViewAdd;
    CView* pViewRemove;
    CDocument* pDoc = GetActiveDocument();
    UINT nCmdID;
    nCmdID = LOWORD(GetCurrentMessage()->wParam);
    if((nCmdID == ID_VIEW_VIEW1) && (m_currentView == 1))
    return;
    if((nCmdID == ID_VIEW_VIEW2) && (m_currentView == 2))
    return;
    if (nCmdID == ID_VIEW_VIEW2)
    {
    if (m_pView2 == NULL)
    {
    m_pView1 = GetActiveView();
    m_pView2 = new CMyView2;
    m_pView2->Create(NULL, NULL, AFX_WS_DEFAULT_VIEW,
    rectDefault, this, AFX_IDW_PANE_FIRST + 1, NULL);
    }
    pViewAdd = m_pView2;
    pViewRemove = m_pView1;
    m_currentView= 2;
    }
    else
    {
    pViewAdd = m_pView1;
    pViewRemove = m_pView2;
    m_currentView= 1;
    }
    // 将活动视的child id设置为AFX_IDW_PANE_FIRST
    // 将其它视设置为AFX_IDW_PANE_FIRST以外的值,
    // 这样当调用 CFrameWnd::RecalcLayout重新布局窗口时,
    // 才会得到正确的视
    int nSwitchChildID = pViewAdd->GetDlgCtrlID();
    pViewAdd->SetDlgCtrlID(AFX_IDW_PANE_FIRST);
    pViewRemove->SetDlgCtrlID(nSwitchChildID);
    // 显示活动视而隐藏非活动视
    pViewAdd->ShowWindow(SW_SHOW);
    pViewRemove->ShowWindow(SW_HIDE);
    // 将新的活动视连接到文档,并断开原来的视与文档的连接
    pDoc->AddView(pViewAdd);
    pDoc->RemoveView(pViewRemove);
    SetActiveView(pViewAdd);
    RecalcLayout();
    }
      

  3.   

    1、从文档类取得视图类的指针
     CView* CTestDoc::GetView(CRuntimeClass* pClass)
    {
    CView* pView;
    POSITION pos=GetFirstViewPosition(); while(pos!=NULL){
    pView=GetNextView(pos);
    if(!pView->IsKindOf(pClass))
    break;
    } if(!pView->IsKindOf(pClass)){
    AfxMessageBox("Connt Locate the View.\r\n http://www.msdn.com");
    return NULL;
    } return pView;
    }2、从一个视图类取得另一视图类的指针
    (假设要从CTestAView中取得指向其它视图类的指针)
    CView* CTestAView::GetView(CRuntimeClass* pClass)
    {
    CTestDoc* pDoc=(CTestDoc*)GetDocument();
    CView* pView;
    POSITION pos=pDoc->GetFirstViewPosition();
    while(pos!=NULL){
    pView=pDoc->GetNextView(pos);
    if(!pView->IsKindOf(pClass))
    break;
    }
    if(!pView->IsKindOf(pClass)){
    AfxMessageBox("Connt Locate the View.");
    return NULL;
    } return pView;
    }