CFileExportDialog Class | CodeGuru

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 […]

Written By
CodeGuru Staff
CodeGuru Staff
Apr 3, 1999
2 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More


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

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.