Jump to content

A tutorial on CFileDialog

- - - - -

  • Please log in to reply
No replies to this topic

#1
mnirahd

mnirahd

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 330 posts
Introduction

Being a developer, we often need to use CFileDialog class; this class helps us to open a File Open/Save dialog box, that helps users to select a filename to open or save. This tutorial would give a step by step instruction how can we use CFileDialog to facilitate our users.

MFC's CFileDialog incapsulate the Windows common file dialog box which can be used to open a file or save a file.

How to use CFileDialog

Using CFileDialog consists of following steps!

1. Create an instance of CFileDialog
2. Set or Modify m_ofn structure.
3. Call DoModal function of CFileDialog
4. When DoModal returns, we can call GetPathName() function to retrieve the selected filename.

Sample Code

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




char szFilters[]= "Text Files (*.NC)|*.NC|Text Files (*.txt)|*.txt|All Files (*.*)|*.*||";

// Create an Open dialog; the default file name extension is ".my".

CFileDialog fileDlg (TRUE, "txt", "*.txt",

      OFN_FILEMUSTEXIST| OFN_HIDEREADONLY, szFilters, this);


// Display the file dialog. When user clicks OK, fileDlg.DoModal()

// returns IDOK.

if( fileDlg.DoModal ()==IDOK )

{

	CString m_strPathname = fileDlg.GetPathName();

	m_left.SetWindowText(m_strPathname);

		

}


Using CFileDialog For specific File Extension:


In case if you want CFileDialog to browse only certain types of files. Say jpg only. Following is example code:


CFileDialog JPGDlg(TRUE, NULL, NULL,

 OFN_HIDEREADONLY|OFN_FILEMUSTEXIST, "JPG Files(*.jpg)|*.jpg||",this);


More about CFileDialog

CFileDialog's constructor creates an instance of CFileDialog that sets or modifies values in the m_ofn structure. After initialization, calling DoModal would display the File Open DialogBox.

Following is a prototype for CFileDialog


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



bOpenFileDialog : If set to TRUE, the CFileDialog would be constructed as Open File DialogBox, otherwise it would be Save File DialogBox.


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 should appear in the filename edit box. If this argument is set to NULL, no filename would appear initially.

dwFlags: A combination of one or more flags that allow you to customize the dialog box. A more detail about these flags can be viewed over msdn.microsoft.com/en-us/library/ms646839(v=vs.85).aspx

lpszFilter :Its a multi 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.

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

Important method of CFileDialog

DoModal
This method displays the dialog box and allows the user to make a selection.

GetPathName

When called, it returns the full path of the selected file, for example, C:\Tutorial\CPlusPlus\Tutorials\CFileDialog.txt

GetFileName

When called, it returns only the filename of the selected file, for example, tutorial.txt.

GetFileExt

It returns the file extension of the selected file. If the name of the file entered is Tutorial.Dat, GetFileExt returns "Dat".

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.

A more detail about CFileDialog can be found over CFileDialog Class (MFC)

References

1.CFileDialog Tutorial : Step by step

2. CFileDialog Usage

3. CFileDialog, MFC Examples, MFC Solution, Open Multiple Files, OFN_OVERWRITEPROMPT, OFN_HIDEREADONLY, VC++ Source Codes




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users