CFileDialog Tutorial Step by Step 

Submitted by date of submission user level 

Mahesh Chand Oct 25, 2000 Beginner 

Calling a file dialog from an application is every programmer's basic need. This article explains how to use CFileDialog MFC class to call a file dialog and how to get selected file name. 

MFC's CFileDialog incapsulate the Windows common file dialog box which can be used to open a file or save a file. First image shows Opening a File and second shows Saving a file.

There are three steps to use CFileDialog

Create an instance of CFileDialg 

Set or modify m_ofn structure values 

Call DoModal to display the dialog 

Sample Code:

Write this below code on any button's click handler or menu's handler where you want to call this above dialog.

fileDlg.m_ofn.lpstrTitle = "My File Dialog"; line sets the title for file dialog.

// Create an instance  

CFileDialog fileDlg( TRUE, NULL, NULL, OFN_ALLOWMULTISELECT | OFN_HIDEREADONLY, "All Files (*.*)|*.*||", this);

// Initializes m_ofn structure 

fileDlg.m_ofn.lpstrTitle = "My File Dialog";

// Call DoModal

if ( fileDlg.DoModal() == IDOK)

{

    CString szlstfile = fileDlg.GetPathName(); // This is your selected file name with path

    AfxMessageBox("Your file name is :" +szlstfile );

}

 

Getting a File Extension

What if you want to browse only certain types of files. Say bmp only. 

CFileDialog bitmapDlg(TRUE, NULL, NULL, OFN_HIDEREADONLY|OFN_FILEMUSTEXIST, "Bitmap Files(*.bmp)|*.bmp||",this); 

CFileDialog In Details

Let's see what else CFileDialog can do for you. CFileDialog's constructor creates an instance of CFileDialog. After creating an instance, you can set or modify values in the m_ofn structure. After initialization, you can DoModal to display the dialog.

You use CFileDialog constructor to create an instance of the CFileDialog.

CFileDialog( BOOL bOpenFileDialog, LPCTSTR lpszDefExt = NULL, LPCTSTR lpszFileName = NULL, DWORD dwFlags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, LPCTSTR lpszFilter = NULL, CWnd* pParentWnd = NULL ); 

bOpenFileDialog : Set to TRUE to construct a File Open dialog box or FALSE to construct a File Save As dialog box. 

lpszDefExt : The default filename extension. If the user does not include an extension in the Filename edit box, the extension specified by lpszDefExt is automatically appended to the filename. If this parameter is NULL, no file extension is appended.

lpszFileName : The initial filename that appears in the filename edit box. If NULL, no filename initially appears.

dwFlags: A combination of one or more flags that allow you to customize the dialog box. For a description of these flags, see the OPENFILENAME structure in the Win32 SDK documentation. If you modify the m_ofn.Flags structure member, use a bitwise-OR operator in your changes to keep the default behavior intact.

lpszFilter :A series of string pairs that specify filters you can apply to the file. If you specify file filters, only selected files will appear in the Files list box. See the Remarks section for more information on how to work with file filters.

pParentWnd :A pointer to the file dialog-box object¡¯s parent or owner window.

Here are some more useful functions:

DoModal Displays the dialog box and allows the user to make a selection. 

GetPathName Returns the full path of the selected file. The path of the filename includes the file¡¯s title plus the entire directory path. For example, GetPathName will return "C:\FILES\TEXT.DAT" for the file C:\FILES\TEXT.DAT. 

GetFileName Returns the filename of the selected file. The name of the file includes both the prefix and the extension. For example, GetFileName will return "TEXT.DAT" for the file C:\FILES\TEXT.DAT. 

GetFileExt Returns the file extension of the selected file. If the name of the file entered is DATA.TXT, GetFileExt returns "TXT". 

GetFileTitle Returns the title of the selected file. The title of the file includes only its prefix, without the path or the extension. For example, GetFileTitle will return "TEXT" for the file C:\FILES\TEXT.DAT. 

GetNextPathName Returns the full path of the next selected file. The path of the filename includes the file¡¯s title plus the entire directory path. For example, GetNextPathName will return "C:\FILES\TEXT.DAT" for the file C:\FILES\TEXT.DAT. You can use GetNextPathName in a forward iteration loop if you establish the initial position with a call to GetStartPosition. 

GetReadOnlyPref Returns the read-only status of the selected file. 

GetStartPosition Returns the position of the first element of the filename list. 

m_ofn is a structure of type OPENFILENAME. Use this structure to initialize the appearance of a File Open or File Save As dialog box after it is constructed but before it is displayed with the DoModal member function. 

NOTE: See OPENFILENAME structure in MSDN for more details about settings.

External Info

The CFileDialog class is defined in #include  header file and implementation is in COMMDLG.DLL. 

ID: Q200421: How to Enhance File Dialog with Multiple Extension Filters. 

 

Alternative: You can use GetOpenFileName and GetSaveFileName if you don't want to use MFC.

Source: MSDN.

Did you like this article? Please send me your feedback on Mahesh. Your feedback helps me to improve the quality of my next article.

About the Author:

Mahesh is Admin and the founder of this site. He has been programming in C++, MFC, Visual Basic, COM, ATL, Database Programming over 4 years. He can be reached on Mahesh. His  background includes Master's in Computer Science and Batchelor's in Mathematics and Physics. See members for more details.