實現單文檔程式中分割視窗

來源:互聯網
上載者:User

經過多方尋找資料和反覆實驗,終於實現了在單文檔程式中分割視窗,並且可以對視圖進行隨意切換,以下是比較詳盡的步驟:

  1,嚮導第四步,選進階,“使用分割欄”挑勾,原始VIEW類為CMyView,派生自CVIEW類
  2,要想加入從其他VIEW類派生的類,如CFormView等,應該在StdAfx.h中加入#include <afxcview.h>
  3,加入新類CTView,派生自CTreeView,編輯初始化CTView::OnInitialUpdate() 代碼如下
  
    void CTView::OnInitialUpdate()
{
 CTreeView::OnInitialUpdate();

 // TODO: Add your specialized code here and/or call the base class
 CTreeCtrl &treeCtrl=GetTreeCtrl();
 DWORD dwStyle=::GetWindowLong(treeCtrl.m_hWnd,GWL_STYLE);
 dwStyle|=TVS_HASBUTTONS|TVS_HASLINES|TVS_LINESATROOT;
 ::SetWindowLong(treeCtrl.m_hWnd,GWL_STYLE,dwStyle);
 HTREEITEM hRoot,hCurPos;
 TV_INSERTSTRUCT tInsert;
 tInsert.hParent=TVI_ROOT;
 tInsert.hInsertAfter=TVI_LAST;
 tInsert.item.mask=TVIF_TEXT|TVIF_PARAM;
    tInsert.item.pszText="分類";
 tInsert.item.lParam=0;
 hRoot=treeCtrl.InsertItem(&tInsert);
 char *plant[4]={"編程","小說","科學","人文"};
 char *cell[4][5]={
        {"VC++","Delphi","BCB","",""},//主系統作業記錄
  {"武俠","偵探","言情","恐怖","懸疑"},
  {"天文","地理","自然","",""},
  {"社會科學","","","",""}
 };
 int i,j;
 for(i=0;i<4;i++)
 {
  tInsert.hParent=hRoot;
  tInsert.item.pszText=plant[i];
  hCurPos=treeCtrl.InsertItem(&tInsert);
  for(j=0;j<5;j++)
  {
   tInsert.hParent=hCurPos;
   if(cell[i][j]!="")
   {
        tInsert.item.pszText=cell[i][j];
     treeCtrl.InsertItem(&tInsert);}
  }
  //treeCtrl.Expand(hCurPos,TVE_EXPAND);

 }
 treeCtrl.Expand(hRoot,TVE_EXPAND);
  
}

  4,加入CFormView衍生類別,先加入一個對話方塊資源,在對話方塊屬性中,指定STYLES->STYLE:child
BORDER:NONE,不選Title Bar 在More Styles中不選可見。然後在建立的CFormView的衍生類別CFView中指定對話方塊為剛建的對話方塊

  5,因為每個CView衍生類別都已經繼承了GetDocument()函數,因此只要在調用時直接調用無需再在其中聲明GetDocument()函數了,調用後再進行類型強制轉換應該就可以了。比方,在cmyview.h中注釋掉
// Attributes
// CMyDoc* GetDocument();
在cmyview.cpp中注釋掉
//CMyDoc* CMyView::GetDocument() // non-debug version is inline
//{
 //ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMyDoc)));
 //return (CMyDoc*)m_pDocument;
//}

並在OnDraw中試用如下代碼
void CMyView::OnDraw(CDC* pDC)
{
 CMyDoc* pDoc =(CMyDoc*)CMyView:: GetDocument();
 ASSERT_VALID(pDoc);
 // TODO: add draw code for native data here
}

  7,為了使新加的view衍生類別能夠被方便的調用,可以將這些view衍生類別的建構函式由protected改為public

  8,加入一個新類CMySplitter,派生自CMDIChildWnd類,然後將代碼中所有的CMDIChildWnd改成CSplitterWnd
在MainFrm.h中加入#include "MySplitter.h",並將CSplitterWnd m_wndSplitter;改成
CMySplitter m_wndSplitter;

  9,給CMySplitter類加入一個publice型成員函數
   BOOL ReplaceView(int row, int col,CRuntimeClass * pViewClass,SIZE size);    
   編輯代碼如下
   

BOOL CMySplitter::ReplaceView(int row, int col, CRuntimeClass *pViewClass, SIZE size)
{
CCreateContext context;
  BOOL bSetActive;
       
  
  if ((GetPane(row,col)->IsKindOf(pViewClass))==TRUE)
       return FALSE;
       
  
   // Get pointer to CDocument object so that it can be used in the creation
   // process of the new view
   CDocument * pDoc= ((CView *)GetPane(row,col))->GetDocument();
   CView * pActiveView=GetParentFrame()->GetActiveView();
   if (pActiveView==NULL || pActiveView==GetPane(row,col))
      bSetActive=TRUE;
   else
      bSetActive=FALSE;

    // set flag so that document will not be deleted when view is destroyed
 pDoc->m_bAutoDelete=FALSE;   
    // Delete existing view
   ((CView *) GetPane(row,col))->DestroyWindow();
    // set flag back to default
    pDoc->m_bAutoDelete=TRUE;
 
    // Create new view                     
  
   context.m_pNewViewClass=pViewClass;
   context.m_pCurrentDoc=pDoc;
   context.m_pNewDocTemplate=NULL;
   context.m_pLastView=NULL;
   context.m_pCurrentFrame=NULL;
  
   CreateView(row,col,pViewClass,size, &context);
  
   CView * pNewView= (CView *)GetPane(row,col);
  
   if (bSetActive==TRUE)
      GetParentFrame()->SetActiveView(pNewView);
  
   RecalcLayout();
   GetPane(row,col)->SendMessage(WM_PAINT);
  
   return TRUE;
}

    10,在MainFrm.cpp前邊加上
    #include "AppDoc.h"
    #include "TView.h"
    #include "MyView.h"
    #include "FView.h"
在MainFrm.cpp 中編輯虛擬函數BOOL CMainFrame::OnCreateClient如下
    
BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT /*lpcs*/,
 CCreateContext* pContext)
{

 
if (!m_wndSplitter.CreateStatic(this,1,2))
 {
  TRACE(_T("failed to create the splitter"));
  return FALSE;
 }

 if (!m_wndSplitter.CreateView(0,0,RUNTIME_CLASS(CTView),CSize(100,100),pContext))
 {
  TRACE(_T("Failed to create view in first pane"));
  return FALSE;
 }

 if (!m_wndSplitter.CreateView(0,1,RUNTIME_CLASS(CMyView),CSize(100,100),pContext))
 {
  TRACE(_T("failed to create view in second pane"));
  return FALSE;
 }

 
 return TRUE;
}
  

  11,加入兩個功能表項目ID_VIEW_VIEW和ID_VIEW_FVIEW分別對應兩個視圖,在MainFrm.h中加入成員變數
   BOOL ShowView1; 在CMainFrame::OnCreate中return 0;前邊加上一行ShowView1=true;
  
   編輯兩個功能表項目代碼如下
  
   void CMainFrame::OnViewView()
{
 // TODO: Add your command handler code here
 CMainFrame *pMainFrame=(CMainFrame*)AfxGetMainWnd();
 pMainFrame->m_wndSplitter.ReplaceView(0,1,RUNTIME_CLASS(CSplView),CSize(100,100));
    ShowView1=true;
}

void CMainFrame::OnViewFview()
{
 // TODO: Add your command handler code here
 CMainFrame *pMainFrame=(CMainFrame*)AfxGetMainWnd();
 pMainFrame->m_wndSplitter.ReplaceView(0,1,RUNTIME_CLASS(CFView),CSize(100,100));
    ShowView1=false; 
}

void CMainFrame::OnUpdateViewView(CCmdUI* pCmdUI)
{
 // TODO: Add your command update UI handler code here
   pCmdUI->SetCheck(ShowView1); 
}

void CMainFrame::OnUpdateViewFview(CCmdUI* pCmdUI)
{
 // TODO: Add your command update UI handler code here
 pCmdUI->SetCheck(!ShowView1); 
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.