Question

I'm writing my first app in MFC, and I was looking to include a very simple feature : Include a "Save To File" button that on being clicked will bring up the familiar "Save As.." Dialog box and will ultimately save the data in a text file. I couldn't find how to invoke this dialog box - can someone just point me at the right way to do it ?

Was it helpful?

Solution

What you need is CFileDialog :

CFileDialog d(FALSE);
if(IDOK == d.DoModal())
{
    CString sFileName = dlg.GetPathName();

    // ... Save your text to the file

}

There are many parameters in the CFileDialog constructor that you can change to affect how the dialog behaves, check the MSDN documentation for details.

OTHER TIPS

For what you've described, you should probably just use a CEditView instead of writing your own code. It's pre-written, tested, etc.

For most other situations, you should realize that MFC itself normally handles the details of creating the Save As... dialog and such, so all you normally have to do is put code into your document class' Serialize() member function to read and write your document's data. That's passed a reference to CArchive object, so all you have to deal with is writing or reading the data for your document object.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top