[摘要]!m_viewList.IsEmpty()); POSITION pos = GetFirstViewPosition(); while (pos != NULL) ...
!m_viewList.IsEmpty());
POSITION pos = GetFirstViewPosition();
while (pos != NULL)
{
CView* pView = GetNextView(pos);
ASSERT_VALID(pView);
//不调用发送者的OnUpdate函数
if (pView != pSender)
pView->OnUpdate(pSender, lHint, pHint);
}
}
在视图的OnUpdate函数里默认的实现仅是通知视图进行重画:
Invalidate(TRUE);
我们一般重载这个更新视图的某些数据或进行其他操作,比如更新视图滚动条的滚动范围。
(四)框架窗口与文档、视图之间的联系
在框架窗口被创建的时候,创建了视图,相关的函数如下:
int CFrameWnd::OnCreate(LPCREATESTRUCT lpcs)
{
CCreateContext* pContext = (CCreateContext*)lpcs->lpCreateParams;
return OnCreateHelper(lpcs, pContext);
}
int CFrameWnd::OnCreateHelper(LPCREATESTRUCT lpcs, CCreateContext* pContext)
{
if (CWnd::OnCreate(lpcs) == -1)
return -1;
// create special children first
if (!OnCreateClient(lpcs, pContext))
{
TRACE0("Failed to create client pane/view for frame.\n");
return -1;
}
// post message for initial message string
PostMessage(WM_SETMESSAGESTRING, AFX_IDS_IDLEMESSAGE);
// make sure the child windows have been properly sized
RecalcLayout();
return 0; // create ok
}
BOOL CFrameWnd::OnCreateClient(LPCREATESTRUCT, CCreateContext* pContext)
{
// default create client will create a view if asked for it
if (pContext != NULL && pContext->m_pNewViewClass != NULL)
{
if (CreateView(pContext, AFX_IDW_PANE_FIRST) == NULL)
return FALSE;
}
return TRUE;
}
CWnd* CFrameWnd::CreateView(CCreateContext* pContext, UINT nID)
{
CWnd* pView = (CWnd*)pContext->m_pNewViewClass->CreateObject();
if (pView == NULL)
{
return NULL;
}
ASSERT_KINDOF(CWnd, pView);
if (!pView->Create(NULL, NULL, AFX_WS_DEFAULT_VIEW,
CRect(0,0,0,0), this, nID, pContext))
{
return NULL; // can't continue without a view
}
if (afxData.bWin4 && (pView->GetExStyle() & WS_EX_CLIENTEDGE))
{
ModifyStyleEx(WS_EX_CLIENTEDGE, 0, SWP_FRAMECHANGED);
}
return pView;
}
在文档模板的OpenDocumentFile函数发生了如下的调用:
InitialUpdateFrame(pFrame, pDocument, bMakeVisible);
文档模板的这个函数的实现为:
pFrame->InitialUpdateFrame(pDoc, bMakeVisible);
实际是调用了框架窗口的同名函数:
void CFrameWnd::InitialUpdateFrame(CDocument* pDoc, BOOL bMakeVisible)
{
CView* pView = NULL;
if (GetActiveView() == NULL)
{
//取主视图
CWnd* pWnd = GetDescendantWindow(AFX_IDW_PANE_FIRST, TRUE);
if (pWnd != NULL && pWnd->IsKindOf(RUNTIME_CLASS(CView)))
{
//主视图存在且合法,把当前的主视图设置为活动视图
pView = (CView*)pWnd;
SetActiveView(pView, FALSE);
}
}
if (bMakeVisible)
{
SendMessageToDescendants(WM_INITIALUPDATE, 0, 0, TRUE, TRUE);
if (pView != NULL)
pView->OnActivateFrame(WA_INACTIVE, this);
……
ActivateFrame(nCmdShow);
if (pView != NULL)
pView->OnActivateView(TRUE, pView, pView);
}
// update frame counts and frame title (may already have been visible)
if (pDoc != NULL)
pDoc->UpdateFrameCounts();
OnUpdateFrameTitle(TRUE);
}
上面的函数中对视图的操作主要是用SetActiveView设置了活动视图,并且调用了视图的OnActivateFrame函数。在CFrameWnd类中维护着一个保护成员:CView* m_pViewActive;,SetAcitveView函数主要就是对它进行操作:
void CFrameWnd::SetActiveView(CView* pViewNew, BOOL bNotify)
{
CView* pViewOld = m_pViewActive;
if (pViewNew == pViewOld)
return; // do not re-activate if SetActiveView called more than once
m_pViewActive = NULL; // no active for the following processing
// deactivate the old one
if (pViewOld != NULL)
pViewOld->OnActivateView(FALSE, pViewNew, pViewOld);
if (m_pViewActive != NULL)
return; // already set
m_pViewActive = pViewNew;
// activate
if (pViewNew != NULL && bNotify)
pViewNew->OnActivateView(TRUE, pViewNew, pViewOld);
}
CFrameWnd还有另一个函数返回这个成员:
CView* CFrameWnd::GetActiveView() const
{
ASSERT(m_pViewActive == NULL
关键词:文档/视图结构中的各个局部是如何联系到一起的