[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.
分享到:
相关推荐
CString CFileDialog::GetFileTitle ( ) 得到完整的文件名,不包括目录名和扩展名如:test1 66 CString CFontDialog::GetFaceName( ) 得到所选字体名字。.....................................................67 ...
CFileDialog的用法和简介 MFC中使用CFile 类和CFileDialog 可以很简单的载入和保存文件
CFileDialog 子类化英文显示, CFileDialog对话框中的文字进行修改
CFileDialog的继承类,可以实现文件夹对话框的选择
用CFileDialog打开多个文件用CFileDialog打开多个文件用CFileDialog打开多个文件用CFileDialog打开多个文件
这个例子能够在CFileDialog中增加一对按纽,需要使用Visual C++ 6.
CFileDialog类:封装了打开和保存文件的标准对话框 CFindReplaceDialog类:封装了标准查找/替换对话框 CFont类:封装了Windows图形设备接口(GDI)中的字体对象 CFontDialog类:封装了字体选择对话框 CGdiObject类:...
弹出窗口获取文件,可以选中对应的文件,获取路径。
资源中的源程序实现了VC++中CFileDialog文件对话框的使用、指定打开路径、打开文件等功能,具有指导性意义。
本程序实现了: (1)从指定位置打开图片,显示到picture控件中。...(3)摄像头视频截图,保存到指定目录(利用CFileDialog文件对话框)。 注:运行本程序前需要安装OpenCV,并在VC6中配置好包含目录。
MFC 打开对话框,查找文件,并进行排序,输出到列表。
MFC为我们提供了OFileDialog的简单易用,但毕竟“千篇一律,没有性格”,实际应用中往往需要具有个性特色的用户界面和个性化的文件对话框。要定制文件对话框,就必须获得对文件对话框中各个控件的控制。...
本文给出了登陆对话框的详细... 非模态对话框的详细代码实现; CFileDialog的详细参数设置、意义及各类方法意义; CColorDialog的详细参数设置、意义及各类方法意义; MessageBox的详细参数设置、意义及各类方法意义;
visual c++自定义文件打开对话框 在VC中定制CFileDialog打开对话框 在VC++中定制CFileDialog.zip
自己写的一个CFileDlg,可以根据需要改变对话框和按钮的颜色,类型等。Windows自带的CFileDlg太过于单一,追求高质量UI设计的可以看看。
clistview类 CFileDialog类 C++超级链接,Visual Studio 2005
一个定制CFileDialog对话框的实例,是用VC实现的,欢迎大家下载
对CFileDialog的初学者很有帮助,方便,简单
派生CFileDialog,使之不仅可选择文件,还可选择目录
这个工程用VS2008生成,实现了对文件对话框(CFileDialog)类的派生,可以在自己的文件对话框中加入自己想要的控件,并可定位原文件对话框位置(用ID为stc32的控件定位). 这样就可以方便地实现文件预览:比如文件预览,文本...