写一个单文档程序,要求有多种视图,而且能够让用户点击一个button就切换另一种视图.就是像eMule那种样子的界面.
请问要用什么方式实现比较好?给点思路啊,谢谢!

解决方案 »

  1.   

    我改造过楼主说得那样的代码,不过涉及到版权问题,不可能给你。不过,它实现过程中,大都是自己实现的。
    可以将所有的视图句并存储在映射指针中,然后比较点击的button与映射指针中某个视图句并对应,然后将其显示显示的时候,需要切换很多东西,如CCreateContext,ToolBar,MenuBar等等
      

  2.   

    有段代码,仅代参考,参数nForm是CFormView对象所对应的对话框的ID
    void CMainFrame::SwitchView(int nForm)
    {
        CView *pOldActiveView=GetActiveView();   //保存旧视图
        CView *pNewActiveView=(CView*)GetDlgItem(nForm);   //取得新视图
        if(pNewActiveView == NULL)
        {
            switch(nForm)
            {
            case IDD_DIALOG1 :
                pNewActiveView = (CView*)new CAllDevView;
                break;
            case IDD_DIALOG2 :
                pNewActiveView = (CView*)new COneDevView;

                break;
            }
            CCreateContext context;   //将文挡和视图相连
            context.m_pCurrentDoc=pOldActiveView->GetDocument();
    if(pOldActiveView->GetRuntimeClass() == RUNTIME_CLASS(CAllView))
    pNewActiveView->Create(NULL, NULL, WS_BORDER|WS_CHILD ,
    CFrameWnd::rectDefault, this, nForm, &context);
            pNewActiveView->OnInitialUpdate();
        }
        SetActiveView(pNewActiveView);        //改变活动的视图
        pNewActiveView->ShowWindow(SW_SHOW);  //显示新的视图
        pOldActiveView->ShowWindow(SW_HIDE);  //隐藏旧的视图    if(pOldActiveView->GetRuntimeClass() == RUNTIME_CLASS(CAllDevView))
              pOldActiveView->SetDlgCtrlID(IDD_DIALOG1);    else if(pOldActiveView->GetRuntimeClass() == RUNTIME_CLASS(COneDevView))
              pOldActiveView->SetDlgCtrlID(IDD_DIALOG2);    pNewActiveView->SetDlgCtrlID(AFX_IDW_PANE_FIRST);
        
        delete pOldActiveView;   //删除旧视图    RecalcLayout();          //调整框架窗口
      

  3.   

    void SwitchViewInFrame(CFrameWnd* pFrame,CRuntimeClass* pViewClass )
    {
    ASSERT_VALID( pFrame );
    ASSERT( pViewClass != NULL ); ASSERT( pViewClass->IsDerivedFrom( RUNTIME_CLASS( CView ) ) ); // 1 - Find the currently active view//发现当前的活动类
    CView* pActiveView = pFrame->GetActiveView();
    if( pActiveView == NULL )
    {
    TRACE0( "Unable to switch: no active view\n" );
    return;
    } if( pActiveView->IsKindOf( pViewClass ) )
    {
    // No need to switch for same view class
    return;
    } // 2 - Store current view position//存贮当前活动视图的位子
    CRect rcView;
    pActiveView->GetWindowRect( &rcView ); // 3 - Find the associated document//发现联系的文档
    CDocument* pDoc = pActiveView->GetDocument();
    ASSERT_VALID( pDoc ); // 4 - Make sure the document won't self-destruct//当活动视图被销毁是保证文档不被破坏
    // when active view is destroyed
    BOOL bSaveAutoDelete = pDoc->m_bAutoDelete;
    pDoc->m_bAutoDelete = FALSE; // 5 - Destroy the active view//销毁活动视图
    pActiveView->DestroyWindow(); // 6 - Restore document to initial state//恢复文档的初始化状态
    pDoc->m_bAutoDelete = bSaveAutoDelete; // 7 - Initialize creation context used by CreateView//初始化CCreateContext被 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
    CView* pNewView = static_cast<CView*>( pFrame->CreateView( &context ) ); ASSERT_VALID( pNewView ); // 9 - Position the new view like the old one
    pFrame->ScreenToClient( &rcView );
    pNewView->MoveWindow( &rcView, TRUE );

    // 10 - Send WM_INITIALUPDATE to the view
    pFrame->InitialUpdateFrame( pDoc, TRUE );
    }
    希望对你有帮助