如果说要做一个多视图的程序,Document/View之间的通讯就变的稍微复杂一些。这时,就要用到UpdateAllViews和OnUpdate函数了。建立多视图程序的主要步骤如下:
1、定义CDocument派生类的成员变量,用于存储数据。
2、在CView派生类中重载OnUpdate函数,确保View中显示的内容与Document中的数据相同。
3、根据需要,组织Message Map。
4、在CView派生类的Command Message Handlers中根据需要更新Document的数据(用CView::GetDocument()函数),在最后要调用CDocument::UpdateAllViews函数以更新其他的View。
5、在CDocument派生类的Command Message Handlers中根据需要更新Document的数据,在最后同样要调用CDocument::UpdateAllViews函数以更新所有的View。
在通讯中将发生一下事件:
Application starts CMyDocument object constructed
CMyView object constructed
CMyView object constructed
Other view objects constructed
View windows created
View windows created
CMyView::OnCreate called (if mapped)
CDocument::OnNewDocument called
CView::OnInitialUpdate called
Calls CMyView::OnUpdate
Initializes the view
Calls CMyView::OnUpdate
Initializes the view
User executes view command CMyView functions update CMyDocument data members
Call CDocument::UpdateAllViews
Other views' OnUpdate functions called
User executes document command CMyDocument functions update data members
Call CDocument::UpdateAllViews
CMyView::OnUpdate called
Other views' OnUpdate functions called
Other views' OnUpdate functions called
User exits application View objects destroyed
CMyDocument object destroyed
CMyDocument object destroyed