[FROM]
http://www.dotnetheaven.com/Uploadfile/mahesh/CFileDialogTutorial05242005022553AM/CFileDialogTutorial.aspx?login=true&user=guowweeei
This
article explains how to use CFileDialog MFC class to call a file dialog
and how to get selected file name. A tutorial which tells you how to
use CFileDialog step by step.
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 <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.
分享到:
相关推荐
`CFileDialog::CFileDialog(BOOL bOpenFileDialog, LPCTSTR lpszDefExt = NULL, LPCTSTR lpszFileName = NULL, DWORD dwFlags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, LPCTSTR lpszFilter = NULL, CWnd* ...
P 前几天领导问我一个问题:就是使用CFileDialog类在设置多选时选中的文件所放的文件缓冲区不知设置多大合适,设置小了DoModal返回为失败, 通过CommDlgExtendedError函数获取错误码为FNERR_BUFFERTOOSMALL(即缓冲...
然后,你需要覆盖`CFileDialog::Create()`方法,用自定义模板替换默认的。 6. **过滤器**:如果你想添加自定义文件类型过滤器,可以在构造函数中设置`lpszFilter`参数,或者在`DoDataExchange`方法中动态添加。 7....
return CFileDialog::OnCtlColor(pDC, pWnd, nCtlColor); } ``` ### 4. 修改`CFileDialog`中的文字 对于标题和描述中的“CFileDialog 子类化英文显示”问题,如果你需要将对话框中的英文文本改为中文,有以下几种...
CFileDialog::CFileDialog( BOOL bOpenFileDialog, // 如果为 TRUE,则显示打开对话框;如果为 FALSE,则显示保存对话文件对话框 LPCTSTR lpszDefExt = NULL, // 默认的文件扩展名 LPCTSTR lpszFileName = NULL,...
CFileDialog::OnInitDialog()) return FALSE; // 添加自定义的“选择目录”按钮 CButton* pButton = (CButton*)GetDlgItem(IDCANCEL); pButton->SetWindowTextW(L"选择目录"); // 隐藏默认的“取消”按钮 ...
return CFileDialog::OnInitDialog(); } }; ``` 3. **利用`SetPathName`函数**: 在`CFileDialog`对象创建后,也可以直接调用`SetPathName`函数来设置初始路径: ```cpp CFileDialog fileDlg(TRUE, NULL, ...
在Windows编程中,`CFileDialog`是MFC(Microsoft Foundation Classes)库中的一个关键类,用于创建标准的文件打开、保存对话框。标题"‘CFileDialog可以选择文件夹’"表明我们要探讨的是如何利用`CFileDialog`来...
CFileDialog::OnShowWindow(bShow, nStatus); // 获取期望的位置 CRect rect; // ... 设置rect // 设置对话框位置 ModifyStyle(0, WS_VISIBLE); SetWindowPos(NULL, rect.left, rect.top, 0, 0, SWP_NOSIZE...
在CFileDialog中增加一对按钮的实现方法是在Windows应用程序开发中一个实用且常见的需求,特别是在需要定制用户界面或者扩展基础对话框功能时。CFileDialog是Visual C++ 6中的一个内置类,主要用于弹出文件对话框,...
在MFC中,`CFileDialog`是用于创建标准打开或保存文件对话框的重要类。这个类使得用户能够方便地在文件系统中选择一个或多个文件,同时提供了定制和自定义的可能性。 `CFileDialog`继承自`CCommonDialog`,它是MFC...
### 在VC++中定制CFileDialog #### 背景与目的 在开发基于Microsoft Foundation Classes (MFC)的应用程序时,经常会用到`CFileDialog`类来实现文件打开或保存对话框的功能。虽然MFC提供的`CFileDialog`非常实用且...
CFileDialog dlg(TRUE, _T("*.bmp"), "", OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY, "image files (*.bmp;*.jpg)|*.bmp;*.jpg|" "AVI files (*.avi)|*.avi|" "All Files (*.*)|*.*||", NULL)...
`CFileDialog`是MFC(Microsoft Foundation Classes)库中的一个类,用于创建标准的Windows文件对话框,允许用户选择文件或目录。在Windows应用程序中,它通常被用来弹出一个界面,让用户选择打开、保存或者浏览文件...
标题"定制CFileDialog"指的是开发者为了满足特定的UI需求,对标准的`CFileDialog`类进行了自定义改造。`CFileDialog`是MFC(Microsoft Foundation Classes)库中的一个类,用于实现文件打开和保存对话框。在标准情况...
* CFileDialog类:CFileDialog类用于显示打开文件对话框。 * OnOpen函数:OnOpen函数用于处理打开图像的事件。 * OnDraw函数:OnDraw函数用于绘制图像到视图中。 总结 使用MFC单文档架构可以轻松地创建一个显示...
这个工程用VS2008生成,实现了对文件对话框(CFileDialog)类的派生,可以在自己的文件对话框中加入自己想要的控件,并可定位原文件对话框位置(用ID为stc32的控件定位). 这样就可以方便地实现文件预览:比如文件预览,文本...
### 使用CFileDialog打开多个文件 在Windows编程中,经常需要让用户选择一个或多个文件进行后续操作,例如导入数据、打开文档等。`CFileDialog`类是MFC(Microsoft Foundation Classes)提供的一种简单易用的方式,...
在Microsoft Visual C++环境中,`CFileDialog`是一个重要的类,用于创建标准的文件打开、保存或打印对话框。这个类是MFC(Microsoft Foundation Classes)库的一部分,它封装了Windows API中的`OPENFILENAME`结构,...