关闭文档时保存 [英] Saving when closing a document

查看:90
本文介绍了关闭文档时保存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个MDI应用程序.关闭文件时,如果文件脏了,应该如何保存?我是否使用SetModifiedFlag()然后直接调用文件对话框,还是应该以某种方式自动发生?

我很困惑,因为使用菜单项会自动生成保存并另存为"对话框,但是关闭文档并将其标记为脏"并不会弹出保存或另存为"对话框.我应该在这里做什么?

我不确定这是否有所不同,但是我覆盖了OnSaveDocument( LPCTSTR lpszPathName )并使用std :: ofstream输出文件.

I have an MDI application. When I close a document, how am I supposed to implement saving the document if it is dirty? Do I use SetModifiedFlag() and then call the file dialog directly or is it supposed to happen automatically somehow?

I''m confused because using the menu item produces the save and save-as dialog automatically, but closing and marking the doc as dirty doesn''t bring up the save or save-as dialog. What am I supposed to do here?

I''m not sure if this makes a difference but I''m overriding OnSaveDocument( LPCTSTR lpszPathName ) and using std::ofstream to output the file.

推荐答案

通过调用SetModifiedFlag()来设置修改后的标志,这就是您要在哪个位置调用它的逻辑.其余的将由MFC自动处理.但是,您必须编写序列化/反序列化代码才能正确保存文件.
You have to set the modified flag by calling SetModifiedFlag() and this will be your logic that at which places you want to call it. Rest will be handled by MFC automatically. However, you will have to write the serialization/deserialization code to save the file properly.


您应该将代码放入文档类的Serialze方法中,而不是覆盖OnSaveDocument.默认的Serialize方法具有
Instead of overriding OnSaveDocument, you should put your code in the Serialze method of the document class. The default Serialize method has
if (ar.IsStoring())
{
    ...
}
else
{
    ....
}


只需将代码保存/加载到if/else的正确部分即可.

希望对您有所帮助.


Just put your code to save/load in the correct part of the if/else.

Hope that helps.


使用Serialize方法可以使框架以标准方式处理文档的保存,这意味着您不必覆盖OnFileSave.此外,当您尝试打开文档时,将再次调用Serialize,因此您可以打开现有数据而不必覆盖OnFileOpen.​​

使用Serialize是保存/加载文档的MFC正确方法,该框架照顾了基础知识.
Using the Serialize method allows the framework to handle the saving of your document in a standard way, meaning you don''t have to override the OnFileSave. Additionally, when you try to open the document, Serialize is called again, thus you can open the existing data without also overriding OnFileOpen.

Using Serialize is the proper MFC way to save/load documents, and the framework takes care of the basics.


这篇关于关闭文档时保存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆