Click here to Skip to main content
15,900,461 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
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.
Posted

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.
 
Share this answer
 
Comments
Brian Bennett 30-Sep-10 7:48am    
Thanks, Aamir. That was fast! I had tried setting that flag in OnCloseDocument() but I guess it was too late at that point. So I tested it in View::Draw() instead and it worked. Thanks for the help.
Instead of overriding OnSaveDocument, you should put your code in the Serialze method of the document class. The default Serialize method has
SQL
if (ar.IsStoring())
{
    ...
}
else
{
    ....
}

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

Hope that helps.
 
Share this answer
 
Comments
Brian Bennett 2-Oct-10 4:22am    
I tend to want to believe you but it would have been nice to hear why this would make a difference? Can't wait to hear the benefits. Thank you.
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.
 
Share this answer
 
Comments
Brian Bennett 2-Oct-10 18:18pm    
That's kind of cool. I'll assume it's OK to still use std::ofstream inside the Serialize function even though it's not mfc. What is it, CArchive object that mfc uses? Thank you, it really helps to know how things are supposed to be done.
krmed 2-Oct-10 18:24pm    
Yes, of course you can use ofstream (or anything else you want to). MFC provides the CArchive for your use - but you don't have to.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900