views:

24

answers:

2

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 ?

+3  A: 

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.

rep_movsd
So, do I have to make a CFileDialog object as a member of my class, or can I put the entire declaration and usage under the OnBtnClicked() Event Handler?
shan23
You can just declare and use it in your event handler.
rep_movsd
Got what I was looking for : http://funnotes.net/File-open-dialog-box-using-CFileDialog.php
shan23
+1  A: 

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.

Jerry Coffin
I looked at CEditView, and I guess that it has a lot of functionality that I'm not looking for - I have a EditControl in my dialog box, whose contents I want to save when the user clicks the "Save As" Button. I found a class (SaveFileDialog) which could potentially solve my problem, but I'm not able to find a good example on the web that shows how to use this. I don't have a MFC book , so google is all I've got !! I told you this since from your response, u look someone very experienced with MFC, and could maybe point me at such an example. Thanks !!
shan23