guowee 发表于 2013-1-27 04:58:48

CFileDialog Tutorial : Step by step


http://www.dotnetheaven.com/Uploadfile/mahesh/CFileDialogTutorial05242005022553AM/CFileDialogTutorial.aspx?login=true&user=guowweeei
 
Thisarticle explains how to use CFileDialog MFC class to call a file dialogand how to get selected file name. A tutorial which tells you how touse CFileDialog step by step.
 
Callinga file dialog from an application is every programmer's basic need.This article explains how to use CFileDialog MFC class to call a filedialog and how to get selected file name.

MFC'sCFileDialog incapsulate the Windows common file dialog box which can beused to open a file or save a file. First image shows Opening a Fileand second shows Saving a file.
 
There are three steps to use CFileDialog:
<strong />

[*]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'ssee what else CFileDialog can do for you. CFileDialog's constructorcreates an instance of CFileDialog. After creating an instance, you canset 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 anextension 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 thedialog box. For a description of these flags, see the OPENFILENAMEstructure 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 thefile. If you specify file filters, only selected files will appear inthe Files list box. See the Remarks section for more information on howto work with file filters.
pParentWnd :A pointer to the file dialog-box object's parent or owner window.
Here are some more useful functions:


DoModalDisplays the dialog box and allows the user to make a selection.GetPathNameReturns the full path of the selected file. The pathof the filename includes the file's title plus the entire directorypath. For example, GetPathName will return "C:\FILES\TEXT.DAT" for the file C:\FILES\TEXT.DAT.GetFileNameReturns 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.GetFileExtReturns the file extension of the selected file. If the name of the file entered is DATA.TXT, GetFileExt returns "TXT".GetFileTitleReturns the title of the selected file. The title ofthe file includes only its prefix, without the path or the extension.For example, GetFileTitle will return "TEXT" for the file C:\FILES\TEXT.DAT.GetNextPathNameReturns the full path of the next selected file. Thepath of the filename includes the file's title plus the entiredirectory 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.GetReadOnlyPrefReturns the read-only status of the selected file.GetStartPositionReturns 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 FileSave As dialog box after it is constructed but before it is displayedwith the DoModal member function.

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

External Info:

<strong />

[*]The CFileDialog class is defined in #include <afxdlgs.h> 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.
页: [1]
查看完整版本: CFileDialog Tutorial : Step by step