Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Simple MFC independent Open-Save File Dialog class

0.00/5 (No votes)
13 Mar 2007 1  
OpenSaveFile Dialog Class

osfdialog1

Introduction

This article introduces a simple class "COSFDialog" that provides a File Open/Save dialog functionality using Win32SDK. This is an alternative to similar MFC classes. But the main purpose of this code is to avoid MFC dependencies and to provide a simple and clear (I hope) mechanism to get file paths from the user.

Using the code

Everything is simple.To put it to work you need to:

  1. Add files "OSFDlg.h" and "OSFDlg.cpp" to your project.
  2. Include OSFDlg header file in your module:
     #include "OSFDlg.h" 
  3. Declare and initialize three variables:
    //-- filters
    
    TCHAR szFilter[] = TEXT ("Text Files (*.TXT)\0*.txt\0")  \
                           TEXT ("All Files (*.*)\0*.*\0\0") ;
    //-- default file extention
    
    TCHAR szDefExtention[] = TEXT("txt\0");
    
    //-- dialog box
    
    static COSFDialog osfDlg;
    

Of course you can specify any subset of file extensions as per your requirements.

And then, in your code you should simply call COSFDialog's member functions FileOpenDlg and FileSaveDlg to present the appropriate dialog box to the user. FileOpenDlg function provides the possibility to create FileOpen dialog that allows multiple or single selection. I will explain about it shortly. To retrieve paths to selected file, you need to call appropriate member functions: GetFileName(for dialog version with single selection) or GetNextFileName(for multiple selection version). The following code examples demonstrate all these actions:

/*------------- open file dialog example --------------*/

    //--- multiple selection version(pass TRUE for fourth argument)

    //--- and call GetNextFileName to retrieve paths to selected files


if(osfDlg.FileOpenDlg(szFilter, szDefExtention, NULL, TRUE))
{
   PCTSTR strFile;
    // opening files one by one till GetNextFileName return '\0'

   while((strFile = osfDlg.GetNextFileName()) != TCHAR('\0'))
   {
       ifstream ifs;
       ifs.open(strFile);
       // processing the file


       ifs.close();
   }
}

//---- single selection version(pass FALSE for fourth argument)

//---- and call GetFileName to retrieve path to selected file


if(osfDlg.FileOpenDlg(szFilter, szDefExtention, NULL, FALSE))
{
    ifstream ifs;
    ifs.open(osfDlg.GetFileName());

    // processing the file


    ifs.close();
}

/*------------ save file dialog example --------------*/

if(osfDlg.FileSaveDlg(szFilter, szDefExtention, NULL))
{
    // create the file

    HANDLE hFile = CreateFile(osfDlg.GetFileName(),
                                     GENERIC_WRITE,
                                     0,
                                     NULL,
                                     CREATE_ALWAYS,
                                     FILE_ATTRIBUTE_NORMAL |
                                     FILE_FLAG_OVERLAPPED,
                                     NULL);
    // save the file to disk


    CloseHandle(hFile);
}

I have to explain a little about arguments that we pass to member functions FileOpenDlg and FileSaveDlg. As you can see from the code example, FileOpenDlg function has four arguments. Here is how this function is declared in "OSFDialog.h"

BOOL FileOpenDlg(PTSTR szFilter,  // filter string defined above

                 PTSTR szDefExt,  // default extention string defined above

                 PTSTR szDlgTitle,// dialog title(pass NULL if you want 

                                  // default title)

                 BOOL fMultSel);  // flag that specify possibility of 

                                  // Multiple selection

                                  // If this value is TRUE - we will get 

                                  // File Open Dialog box

                                  // that allows multiple selection.

                                  // If this value is FALSE - we will have 

                                  // only possibility to make

                                  // single selection

                            

You can specify the dialog title as a third parameter:

if(osfDlg.FileOpenDlg(szFilter, szDefExtention,
                    TEXT("OPEN MY FAVORITE FILES:)"), TRUE))
{
    // some actions

}

Or simply pass NULL for this parameter.

FileSaveDlg function has three arguments that have the same meaning as the first three arguments in the function FileOpenDlg

BOOL FileSaveDlg(PTSTR szFilter, PTSTR szDefExt, PTSTR szDlgTitle);

Return values from Open/Save functions reflect successful or unsuccessful selection.

I have added a small demo program just to demonstrate how it should work in a real application.

About the author

Denis Ponomarenko is a Ukrainian developer, now working in United Arab Emirates, Sharjah

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here