经过多方寻找资料和反复试验,终于实现了在单文档程序中分割窗口,并且可以对视图进行随意切换,以下是比较详尽的步骤:
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);
}