CFileExportDialog Class

The MFC CFileDialog class only has provision for one default file extension, which is added to a filename if no extension is given. A file export functionality deals with possibly many different file extensions, that have to be added depending on the filetype chosen. The CFileExportDialog class does this. Moreover, when the user selects another filetype from the combobox, the extension in the filename editcontrol is automatically changed.

The usage of the dialog is simple because it mimics the usage of the parent class CFileDialog. Apart from the constructor and destructor, there is only one extra public function, GetFilterIndex(), which returns the index of the selected filetype. This is useful if one extension is shared by two or more filetypes, so the filetype cannot be determined from the resulting filename. In keeping with standard Windows practice, this index starts at 1.
One thing to note is the format of the filter string. This string MUST be of the form "Word File (*.doc)|Text File (*.txt)" .i.e one extension per format, and the formats separated by pipes. Every extension must be put between parentheses and be of the form *.ext.
The initial filetype is determined from the default filename, if it is given. This filename may also be "*.ext". If no default filename is given, the first filetype in the filter string is used.

The CFileExportDialog class is typically used like this:

CFileExportDialog dialog("Save As Type",                          // title
  "Windows Bitmap (*.bmp)|JPEG Compressed Image (*.jpg)|"\        // filter string..
  "Windows Meta File (*.wmf)|Windows Enhanced Meta File (*.emf)", // with file types
  "visualization.jpg");                                           // default filename (may be omitted)

if (dialog.DoModal() == IDOK) {
  CString filename = dialog.GetPathName();
  int filetype = dialog.GetFilterIndex();
  SaveFile(filename, filetype); // defined somewhere in the application
}


Created with MSVC 6, tested under Win95 OSR2.

Download demo project - 20 KB

Download source - 5 KB

Date Last Updated: April 3, 1999



Comments

  • Why different format for filter string?

    Posted by Legacy on 11/17/2003 12:00am

    Originally posted by: _plug

    Why did you make your class use a different format for the filter string than the CFileDialog class? Why not use the same format ("|File Type (*.ext)|*.ext|") and therefore make it compatible with the rest of the worlds code. Also, why not support multiple file extensions per format? When the user selects a format, the first extension would be used.

    Reply
  • How to empty the file name input box initialized filedialog?

    Posted by Legacy on 03/14/2002 12:00am

    Originally posted by: Gempin

    I don't wanna the file name input box has a file name string of the filedialog initialized, that mean there is empty, how to do that? please...
    

    Reply
  • Code Page Error

    Posted by Legacy on 07/27/1999 12:00am

    Originally posted by: Cris Padilla Tagle

    I'm sorry but I could not open your demo program. I am receiving a code page error. It seems to me that the resource needs a Code Page 1252 instead.

    I am thinking if you could send me a copy for English code page.

    Thanks.

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

  • Live Event Date: November 20, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT Are you wanting to target two or more platforms such as iOS, Android, and/or Windows? You are not alone. 90% of enterprises today are targeting two or more platforms. Attend this eSeminar to discover how mobile app developers can rely on one IDE to create applications across platforms and approaches (web, native, and/or hybrid), saving time, money, and effort and introducing apps to market faster. You'll learn the trade-offs for gaining long …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds