精通
英语
和
开源
,
擅长
开发
与
培训
,
胸怀四海
第一信赖
锐英源精品开源心得,转载请注明:“锐英源www.wisestudy.cn,孙老师作品,电话13803810136。”需要全文内容也请联系孙老师。
BOOL CFrame::ReplaceView(CRuntimeClass * pViewClass)
{
CCreateContext context;
CView * pCurrentView;
// If no active view for the frame, return FALSE because this
// function retrieves the current document from the active view.
//由于这个函数从当前视图里获取文档,如果子框架内没有激活视图,返回FALSE。
if ((pCurrentView=GetActiveView())==NULL)
return FALSE;
// If you're already displaying this kind of view, no need to go
// further.如果你显示的视图是目标视图,不需要再处理了
if ((pCurrentView->IsKindOf(pViewClass))==TRUE)
return TRUE;
// Get pointer to CDocument object so that it can be used in the
// creation process of the new view.获取文档指针,在创建时使用
CDocument * pDoc= pCurrentView->GetDocument();
// Set flag so that document will not be deleted when view is
// destroyed.设置标志防止在视图销毁时,文档会被删除
BOOL bAutoDelete=pDoc->m_bAutoDelete;
pDoc->m_bAutoDelete=FALSE;
// Delete existing view删除当前视图
pCurrentView->DestroyWindow();
// restore flag恢复标志
pDoc->m_bAutoDelete=bAutoDelete;
// Create new view and redraw.创建新视图和重绘
context.m_pNewViewClass=pViewClass;
context.m_pCurrentDoc=pDoc;
context.m_pNewDocTemplate=NULL;
context.m_pLastView=NULL;
context.m_pCurrentFrame=this;
CView * pNewView = (CView *) pViewClass->CreateObject();
if (pNewView == NULL)
{
TRACE1("Warning: Dynamic create of view type %Fs failed\n",
pViewClass->m_lpszClassName);
return FALSE;
}
if (!pNewView->Create(NULL, NULL, AFX_WS_DEFAULT_VIEW,
CRect(0,0,0,0), this, AFX_IDW_PANE_FIRST, &context))
{
TRACE0("Warning: couldn't create view for frame\n");
return FALSE; // Programmer can assume FALSE return value
// from this function means that there
// isn't a view.返回FALSE表示没有视图
}
// WM_INITIALUPDATE is define in AFXPRIV.H. WM_INITIALUPDATE是在AFXPRIV.H内定义
pNewView->SendMessage(WM_INITIALUPDATE, 0, 0);
RecalcLayout();
pNewView->UpdateWindow();
//设置激活视图为新建视图
SetActiveView(pNewView);
return TRUE;
}
The function receives a pointer to CRuntimeClass object for the new view that is desired. It destroys the old view and replaces it with a new view of the same CDocument. When DestroyWindow() is called for the old view, this causes a "delete this" in the CView::PostNcDestroy() function. Also, the CView::~CView destructor calls CView::RemoveView(), which removes the view from the document's view list.
这个函数需要一个指向服务于欲创建视图CRuntimeClass对象的指针。它销毁旧的视图,用新的来替换。当用旧视图调用DestroyWindow()函数,会导致CView::PostNcDestroy()内的"delete this"。同样CView::~CView析构函数会调用CView::RemoveView(),这个函数会把视图从文档的视图列表里删除视图。