17.3.1 记录文档修改(2)
调用视图类的继承成员GetDocument()可以访问指向文档对象的指针,然后使用这个指针调用SetModifiedFlag()函数。
在文档中可以进行修改的所有地方现在就介绍完毕。文档对象还存储了元素类型、元素颜色和线宽,所以也需要跟踪它们的修改。下面更新OnColorBlack():
- void CSketcherDoc::OnColorBlack()
- {
- m_Color = ElementColor::BLACK; // Set the drawing color to black
- SetModifiedFlag(); // Set the modified flag
- }
给其他颜色和元素类型的处理程序添加相同的语句。设置线宽的处理程序需要修改:
- void CSketcherDoc::OnPenWidth()
- {
- CPenDialog aDlg; // Create a local dialog object
- aDlg.m_PenWidth = m_PenWidth; // Set pen width as that in the document
- if(aDlg.DoModal() == IDOK) // Display the dialog as modal
- {
- m_PenWidth = aDlg.m_PenWidth; // When closed with OK, get the pen width
- SetModifiedFlag(); // Set the modified flag
- }
- }
如果在构建和运行Sketcher程序时修改文档或者添加元素,那么在退出这个程序时,将出现保存文档的提示。当然,除了可以清除修改标志以及把空文件保存到磁盘中以外,File | Save菜单选项现在还不能进行其他操作。为了可以正确地把文档连续写入磁盘,必须实现序列化,下面对此进行介绍。