可以建立多视,用ShowWindow(SW_SHOW),或ShowWindow(SW_HIDE)切换。

解决方案 »

  1.   

    <<VC技术内幕>>第20章"拆分窗口和多重视图"有例子
      

  2.   

    我有例子
    void CChildFrame::OnViewDesign() 
    {
    // TODO: Add your command handler code here
    CWBuilderDoc * pDoc=(CWBuilderDoc *)this->GetActiveDocument();
    if (NULL==m_pDesignView){
    m_pSourceView=(CWmlEditView *)GetActiveView();
    m_pDesignView=new CWBuilderView();
    m_pDesignView->Create(NULL,NULL,AFX_WS_DEFAULT_VIEW,rectDefault,this,AFX_IDW_PANE_FIRST+1,NULL);
    }
    if ((pDoc->IsModified()==TRUE)  and  and  (pDoc->GetPathName()=="")){
    AfxMessageBox("必须首先保存文件!");
    return;
    }
    pDoc->AddView(m_pDesignView);
    m_pDesignView->SetDlgCtrlID(AFX_IDW_PANE_FIRST);
    m_pSourceView->SetDlgCtrlID(AFX_IDW_PANE_FIRST+1); m_pDesignView->ShowWindow(SW_SHOW);
    m_pSourceView->ShowWindow(SW_HIDE); this->SetActiveView(m_pDesignView); pDoc->RemoveView(m_pSourceView);
    m_pDesignView->m_card_num=0;
    m_pDesignView->m_tree_ctrl.DeleteAllItems();
    MSXML::IXMLDocumentPtr xmlDoc;
    MSXML::IXMLElementPtr xmlElem;
    MSXML::IXMLElementCollectionPtr xmlElemCol;
    CoInitialize(NULL);
    xmlDoc.CreateInstance(__uuidof(MSXML::XMLDocument));
    xmlDoc->url=_bstr_t(pDoc->GetPathName());
    while (xmlDoc->readyState!=READYSTATE_COMPLETE);
    xmlElem=xmlDoc->root; CString str="<";
    str+=xmlElem->tagName;
    str+=">";
    str+=xmlElem->text; HTREEITEM ti1=m_pDesignView->m_tree_ctrl.InsertItem(str,TVI_ROOT); m_pDesignView->AddSubTree(ti1,xmlElem); xmlElem.Release();
    xmlDoc.Release(); for (int i=0;i<=pDoc->m_file_content.GetUpperBound();i++){
    CString & str=pDoc->m_file_content.ElementAt(i);
    m_pDesignView->m_file_content+=str;
    m_pDesignView->m_file_content+=" ";
    } while (m_pDesignView->m_file_content.Replace("  "," "));
    m_pDesignView->m_file_content.Replace("< ","<");
    m_pDesignView->m_file_content.Replace("</ ","</");
    m_pDesignView->m_file_content.Replace(" >",">");
    int card_start=m_pDesignView->m_file_content.Find("<card");
    int card_end=m_pDesignView->m_file_content.Find("</card");
    m_pDesignView->m_file_content=m_pDesignView->m_file_content.Mid(card_start,card_end-card_start);
    m_pDesignView->m_file_content.Replace("<card","<body");
    m_pDesignView->m_file_content+="</body>";
    CStdioFile file("c:\\temp.tmp",CFile::modeCreate and CFile::modeWrite);
    file.WriteString(m_pDesignView->m_file_content);
    file.Close();
    m_pDesignView->m_html_view->Navigate("c:\\temp.tmp");
    CoUninitialize();
    RecalcLayout();
    }void CChildFrame::OnViewSource() 

    // TODO: Add your command handler code here
    CDocument * pDoc=this->GetActiveDocument(); pDoc->AddView(m_pSourceView);
    m_pSourceView->SetDlgCtrlID(AFX_IDW_PANE_FIRST);
    m_pDesignView->SetDlgCtrlID(AFX_IDW_PANE_FIRST+1); m_pSourceView->ShowWindow(SW_SHOW);
    m_pDesignView->ShowWindow(SW_HIDE); this->SetActiveView(m_pSourceView); pDoc->RemoveView(m_pDesignView);
    RecalcLayout();
    ::CreateCaret(m_pSourceView->m_hWnd,NULL,2,16);
    m_pSourceView->ShowCaret();
    }void CChildFrame::OnUpdateViewDesign(CCmdUI* pCmdUI) 
    {
    // TODO: Add your command update UI handler code here
    pCmdUI->Enable(GetActiveView()->IsKindOf(RUNTIME_CLASS(CWmlEditView)));
    }void CChildFrame::OnUpdateViewSource(CCmdUI* pCmdUI) 
    {
    // TODO: Add your command update UI handler code here
    pCmdUI->Enable(GetActiveView()->IsKindOf(RUNTIME_CLASS(CWBuilderView)));
    }
      

  3.   

    我有例子
    void CChildFrame::OnViewDesign() 
    {
    // TODO: Add your command handler code here
    CWBuilderDoc * pDoc=(CWBuilderDoc *)this->GetActiveDocument();
    if (NULL==m_pDesignView){
    m_pSourceView=(CWmlEditView *)GetActiveView();
    m_pDesignView=new CWBuilderView();
    m_pDesignView->Create(NULL,NULL,AFX_WS_DEFAULT_VIEW,rectDefault,this,AFX_IDW_PANE_FIRST+1,NULL);
    }
    if ((pDoc->IsModified()==TRUE)  and  and  (pDoc->GetPathName()=="")){
    AfxMessageBox("必须首先保存文件!");
    return;
    }
    pDoc->AddView(m_pDesignView);
    m_pDesignView->SetDlgCtrlID(AFX_IDW_PANE_FIRST);
    m_pSourceView->SetDlgCtrlID(AFX_IDW_PANE_FIRST+1); m_pDesignView->ShowWindow(SW_SHOW);
    m_pSourceView->ShowWindow(SW_HIDE); this->SetActiveView(m_pDesignView); pDoc->RemoveView(m_pSourceView);
    m_pDesignView->m_card_num=0;
    m_pDesignView->m_tree_ctrl.DeleteAllItems();
    MSXML::IXMLDocumentPtr xmlDoc;
    MSXML::IXMLElementPtr xmlElem;
    MSXML::IXMLElementCollectionPtr xmlElemCol;
    CoInitialize(NULL);
    xmlDoc.CreateInstance(__uuidof(MSXML::XMLDocument));
    xmlDoc->url=_bstr_t(pDoc->GetPathName());
    while (xmlDoc->readyState!=READYSTATE_COMPLETE);
    xmlElem=xmlDoc->root; CString str="<";
    str+=xmlElem->tagName;
    str+=">";
    str+=xmlElem->text; HTREEITEM ti1=m_pDesignView->m_tree_ctrl.InsertItem(str,TVI_ROOT); m_pDesignView->AddSubTree(ti1,xmlElem); xmlElem.Release();
    xmlDoc.Release(); for (int i=0;i<=pDoc->m_file_content.GetUpperBound();i++){
    CString & str=pDoc->m_file_content.ElementAt(i);
    m_pDesignView->m_file_content+=str;
    m_pDesignView->m_file_content+=" ";
    } while (m_pDesignView->m_file_content.Replace("  "," "));
    m_pDesignView->m_file_content.Replace("< ","<");
    m_pDesignView->m_file_content.Replace("</ ","</");
    m_pDesignView->m_file_content.Replace(" >",">");
    int card_start=m_pDesignView->m_file_content.Find("<card");
    int card_end=m_pDesignView->m_file_content.Find("</card");
    m_pDesignView->m_file_content=m_pDesignView->m_file_content.Mid(card_start,card_end-card_start);
    m_pDesignView->m_file_content.Replace("<card","<body");
    m_pDesignView->m_file_content+="</body>";
    CStdioFile file("c:\\temp.tmp",CFile::modeCreate and CFile::modeWrite);
    file.WriteString(m_pDesignView->m_file_content);
    file.Close();
    m_pDesignView->m_html_view->Navigate("c:\\temp.tmp");
    CoUninitialize();
    RecalcLayout();
    }void CChildFrame::OnViewSource() 

    // TODO: Add your command handler code here
    CDocument * pDoc=this->GetActiveDocument(); pDoc->AddView(m_pSourceView);
    m_pSourceView->SetDlgCtrlID(AFX_IDW_PANE_FIRST);
    m_pDesignView->SetDlgCtrlID(AFX_IDW_PANE_FIRST+1); m_pSourceView->ShowWindow(SW_SHOW);
    m_pDesignView->ShowWindow(SW_HIDE); this->SetActiveView(m_pSourceView); pDoc->RemoveView(m_pDesignView);
    RecalcLayout();
    ::CreateCaret(m_pSourceView->m_hWnd,NULL,2,16);
    m_pSourceView->ShowCaret();
    }void CChildFrame::OnUpdateViewDesign(CCmdUI* pCmdUI) 
    {
    // TODO: Add your command update UI handler code here
    pCmdUI->Enable(GetActiveView()->IsKindOf(RUNTIME_CLASS(CWmlEditView)));
    }void CChildFrame::OnUpdateViewSource(CCmdUI* pCmdUI) 
    {
    // TODO: Add your command update UI handler code here
    pCmdUI->Enable(GetActiveView()->IsKindOf(RUNTIME_CLASS(CWBuilderView)));
    }
      

  4.   

    单文档多视的问题,我在前两天刚作了一个例子:
    首先在应有程序类中声明:
          CView *pOldView;
          CView *pNewView;
    然后在实现文件中的 ProcessShellCommand()后加入
       CView* pActiveView = ((CFrameWnd*)m_pMainWnd)->GetActiveView();
    m_pOldView = pActiveView; //保存旧视
    m_pNewView = (CView*) new CTrainForm; //新视
    CDocument* pCurrentDoc=
                 ((CFrameWnd*)m_pMainWnd)->GetActiveDocument();//取得活动文档
     /* 初始化CCreateContext对象,使其指向活动文档。用它为文档增加在
                     CView::OnCreate()函数中生成的新视 */
    CCreateContext newContext;
    newContext.m_pNewViewClass = NULL;
    newContext.m_pNewDocTemplate = NULL;
    newContext.m_pLastView = NULL;
    newContext.m_pCurrentFrame = NULL;
    newContext.m_pCurrentDoc = pCurrentDoc; //指向活动文档
    // 初始活动视的ID为AFX_IDW_PANE_FIRST,该值按1递增赋予新视
    UINT viewID = AFX_IDW_PANE_FIRST + 1;

    // 创建新视
    CRect rect(0, 0, 0, 0);  //注意初始化参数为0
    m_pNewView->Create(NULL, "AnyWindowName", WS_CHILD, rect,       m_pMainWnd, viewID, &newContext);
    //发送WM_INITIALUPDATE消息
    m_pNewView->SendMessage(WM_INITIALUPDATE, 0, 0);然后在利用函数 SwitchView(CView * pNewView)来实现切换
    它的定以为:
       CView* CHhApp::SwitchView(CView * pNewView)
    {
       //取得活动视
       CView* pActiveView = ((CFrameWnd*) m_pMainWnd)->GetActiveView();
              
              //交换视窗口的标识
       #ifndef _WIN32 //不是Win32应用,注意使用函数为Word型
       UINT temp =::GetWindowWord(pActiveView->m_hWnd, GWW_ID);
       ::SetWindowWord(pActiveView->m_hWnd, GWW_ID,
                      ::GetWindowWord(pNewView->m_hWnd, GWW_ID));
       ::SetWindowWord(pNewView->m_hWnd, GWW_ID, temp);
       #else //不是Win32应用,注意使用函数为Long型
       UINT temp =::GetWindowLong(pActiveView->m_hWnd, GWL_ID);
       ::SetWindowLong(pActiveView->m_hWnd, GWL_ID,
                      ::GetWindowLong(pNewView->m_hWnd, GWL_ID));
       ::SetWindowLong(pNewView->m_hWnd, GWL_ID, temp);
       #endif
       pActiveView->ShowWindow(SW_HIDE); //将当前视图隐藏
       pNewView->ShowWindow(SW_SHOW); //显示新视图
       ((CFrameWnd*) m_pMainWnd)->SetActiveView(pNewView); //设置新视图为活动视图
       ((CFrameWnd*) m_pMainWnd)->RecalcLayout(); //重新布局
       pNewView->Invalidate(); //重绘
       return pActiveView; //返回活动视
    }