MFC中的几个常用类——CFileDialog

1 简介

CFileDialog类封装了Windows常用的文件对话框。常用的文件对话框提供了一种简单的与Windows标准相一致的文件打开和文件存盘对话框功能。

可以用
构造函数提供的方式使用CFileDialog,也可以从CFileDialog派生出自己的对话类并编写一个构造函数来适应你的需要。每种情况下,对话框都与标准MFC对话框一样工作。因为它们都是
CCommonDialog类的
派生类

要使用CFileDialog,先用CFileDialog
构造函数构造一个对象,当创建了一个对话框后,可以设置或修改m_ofn结构中的任何值,以初始化对话框控件的值或状态。m_ofn结构是OPENFILENAME类型的。要了解更多信息,可参阅联机文档“Win32 SDK”中的OPENFILENAME结构。

初始化对话框控件后,调用DoModal成员函数显示对话框并使用户输入路径和文件。DoModal返回不论是用户选择了OK(IDOK)还是取消(IDCANCEL)按钮。

当DoModal返回IDOK,可以使用某一个CFileDIalog的公共成员函数获取用户输入的信息。

CFileDIalog包含许多保护成员,使你可以处理常用的共享冲突、文件名合法性检查、列表框改变通知。这些保护成员对许多应用来说用处不大,因为缺省处理是自动的。对这些函数来说,消息映射入口是不必要的,因为它们是标准
虚函数

可以使用Windows CommDlgExtendError函数判断在初始化对话框时是否是发生了错误,并获取关于错误的更多信息。

析构一个CFileDialog对象是自动,无须调用CDialog::EndDialog。

要使用户选用多个文件,可在调用DoModal之前设置OFN_ALLOWMULTISELECT标志。你应提供文件名
缓冲区来放置返回的多个文件名的列表,这通过用一个分配了的缓冲区
指针替换m_ofn.lpstrFile来实现,要在创建了CFileDialog之后调用DoModal之前进行此操作。另外,必须用m_ofn.lpstrFile指向的
缓冲区字节数来设置m_ofn.nMaxFile。

CFileDialog依赖于Windows3.1及以后版本中的COMMDLG.DLL。

如果从CFileDialog中派生出一个新类,可用消息映射处理。要扩展消息处理,从CWnd中派生一个类,向新类中加入一个消息映射并为新消息提供成员函数,无须提供一个
钩子函数来定制对话框。

要定制对话框,从CFileDialog中派生一个对象,提供一个定制对话模板,从扩展控件中加入一个消息映射,处理通知消息。任意未处理的消息将传递给
基类

无须定制
钩子函数

#include <afxdlgs.h>

CFileDialog类的成员

2 继承体系

CObject

└CCmdTarget

└CWnd

└CDialog


CCommonDialog

└CFileDialog

3 数据成员

m_ofn Windows OPENFILENAME结构,提供对基本文件对话框参数的访问

4 成员函数

CFileDialog构造一个CFileDialog对象操作

DoModal显示对话框并使用户可以进行选择

GetPathName返回选定文件的完整路径

GetFileName返回选定文件的文件名

GetFileExt返回选定文件的扩展文件名

GetFileTitle返回选定文件的标题

GetNextPathName返回下一个选定文件的完整路径

GetReadOnlyPref返回选定文件的只读状态

GetStartPosition返回文件名列表的第一个元素位置

可覆盖的函数

OnShareViolation发生共享冲突时调用

OnFileNameOK确认键入对话框中的文件名

OnLBSelChangedNotify当列表框选择改变时调用

OnInitDone处理WM_NOTIFY CDN_INITDONE消息

OnFileNameChange处理WM_NOTIFY CDN_SELCHANGE消息

OnFolderChange处理WM_NOTIFY CDN_FOLDERCHANGE消息

OnTypeChange处理WM_NOTIFY CDN_TYPECHANGE消息

文件选择对话框的使用:首先构造一个对象并提供相应的参数,
构造函数原型如下:

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

参数意义如下:

bOpenFileDialog 为TRUE则显示打开对话框,为FALSE则显示保存对话文件对话框。

lpszDefExt 指定默认的
文件扩展名

lpszFileName 指定默认的文件名。

dwFlags 指明一些特定风格。

lpszFilter 是最重要的一个参数,它指明可供选择的文件类型和相应的扩展名。参数格式如:

"Chart Files (*.xlc)|*.xlc|Worksheet Files (*.xls)|*.xls|Data Files (*.xlc;*.xls)|*.xlc; *.xls|All Files (*.*)|*.*||";文件类型说明和扩展名间用 | 分隔,同种类型文件的扩展名间可以用 ; 分割,每种文件类型间用 | 分隔,末尾用 || 指明。

pParentWnd 为父窗口指针。

创建文件对话框可以使用DoModal(),在返回后可以利用下面的函数得到用户选择:

CString CFileDialog::GetPathName( ) 得到完整的文件名,包括目录名和扩展名如:c:\ test\ test1.txt

CString CFileDialog::GetFileName( ) 得到完整的文件名,包括扩展名如:test1.txt

CString CFileDialog::GetExtName( ) 得到完整的
文件扩展名,如:txt

CString CFileDialog::GetFileTitle ( ) 得到完整的文件名,不包括目录名和扩展名如:test1

POSITION CFileDialog::GetStartPosition( ) 对于选择了多个文件的情况得到第一个文件位置。

CString CFileDialog::GetNextPathName( POSITION& pos ) 对于选择了多个文件的情况得到下一个文件位置,并同时返回当前文件名。但必须已经调用过POSITION CFileDialog::GetStartPosition( )来得到最初的POSITION
变量

例如

{

CString

FilePathName;

CFileDialog dlg(TRUE);///TRUE为OPEN对话框,FALSE为SAVE AS对话框

if(dlg.DoModal()==IDOK)

FilePathName=dlg.GetPathName();

}

相关信息:CFileDialog 用于取文件名的几个成员函数:

假如选择的文件是C:WINDOWSTEST.EXE

则:

(1)GetPathName();取文件名全称,包括完整路径。取回C:\WINDOWS\TEST.EXE

(2)GetFileName();取文件全名:TEST.EXE

(3)GetFileTitle();取回TEST

(4)GetFileExt();取扩展名EXE

补充: 在控制台下使用这个类需要设置在
静态库中使用MFC,然后构造 AfxSetResourceHandle(GetModuleHandle(NULL));

相关头文件 #include <Afxdlgs.h>

5 风格详解

dwFlags

Flags

A set of bit flags you can use to initialize the dialog box. When the dialog box returns, it sets these flags to indicate the user‘s input. This member can be a combination of the following flags.

OFN_ALLOWMULTISELECT

Specifies that the File Name list box allows multiple selections. If you also set the OFN_EXPLORER flag, the dialog box uses the Explorer-style user interface; otherwise, it uses the old-style user interface.

If the user selects more than one file, the lpstrFile buffer returns the path to the current directory followed by the file names of the selected files. The nFileOffset member is the offset, in bytes or characters, to the first file name, and the nFileExtension member is not used. For Explorer-style dialog boxes, the directory and file name strings are NULL separated, with an extra NULL character after the last file name. This format enables the Explorer-style dialog boxes to return long file names that include spaces. For old-style dialog boxes, the directory and file name strings are separated by spaces and the function uses short file names for file names with spaces. You can use the FindFirstFile function to convert between long and short file names.

If you specify a custom template for an old-style dialog box, the definition of the File Name list box must contain the LBS_EXTENDEDSEL value.

OFN_CREATEPROMPT

If the user specifies a file that does not exist, this flag causes the dialog box to prompt the user for permission to create the file. If the user chooses to create the file, the dialog box closes and the function returns the specified name; otherwise, the dialog box remains open. If you use this flag with the OFN_ALLOWMULTISELECT flag, the dialog box allows the user to specify only one nonexistent file.

OFN_DONTADDTORECENT

Windows 2000/XP: Prevents the system from adding a link to the selected file in the file system directory that contains the user‘s most recently used documents. To retrieve the location of this directory, call the
SHGetSpecialFolderLocation function with the CSIDL_RECENT flag.

OFN_ENABLEHOOK

Enables the hook function specified in the lpfnHook member.

OFN_ENABLEINCLUDENOTIFY

Windows 2000/XP: Causes the dialog box to send CDN_INCLUDEITEM notification messages to your
OFNHookProc hook procedure when the user opens a folder. The dialog box sends a notification for each item in the newly opened folder. These messages enable you to control which items the dialog box displays in the folder‘s item list.

OFN_ENABLESIZING

Windows 2000/XP, Windows 98/Me: Enables the Explorer-style dialog box to be resized using either the mouse or the keyboard. By default, the Explorer-style Open and Save As dialog boxes allow the dialog box to be resized regardless of whether this flag is set. This flag is necessary only if you provide a hook procedure or custom template. The old-style dialog box does not permit resizing.

OFN_ENABLETEMPLATE

Indicates that the lpTemplateName member is a pointer to the name of a dialog template resource in the module identified by the hInstance member. If the OFN_EXPLORER flag is set, the system uses the specified template to create a dialog box that is a child of the default Explorer-style dialog box. If the OFN_EXPLORER flag is not set, the system uses the template to create an old-style dialog box that replaces the default dialog box.

OFN_ENABLETEMPLATEHANDLE

Indicates that the hInstance member identifies a data block that contains a preloaded dialog box template. The system ignores lpTemplateName if this flag is specified. If the OFN_EXPLORER flag is set, the system uses the specified template to create a dialog box that is a child of the default Explorer-style dialog box. If the OFN_EXPLORER flag is not set, the system uses the template to create an old-style dialog box that replaces the default dialog box.

OFN_EXPLORER

Indicates that any customizations made to the Open or Save As dialog box use the new Explorer-style customization methods. For more information, see Explorer-Style Hook Procedures and Explorer-Style Custom Templates.

By default, the Open and Save As dialog boxes use the Explorer-style user interface regardless of whether this flag is set. This flag is necessary only if you provide a hook procedure or custom template, or set the OFN_ALLOWMULTISELECT flag.

If you want the old-style user interface, omit the OFN_EXPLORER flag and provide a replacement old-style template or hook procedure. If you want the old style but do not need a custom template or hook procedure, simply provide a hook procedure that always returns FALSE.

OFN_EXTENSIONDIFFERENT

Specifies that the user typed a file name extension that differs from the extension specified by lpstrDefExt. The function does not use this flag if lpstrDefExt is NULL.

OFN_FILEMUSTEXIST

Specifies that the user can type only names of existing files in the File Name entry field. If this flag is specified and the user enters an invalid name, the dialog box procedure displays a warning in a message box. If this flag is specified, the OFN_PATHMUSTEXIST flag is also used. This flag can be used in an Open dialog box. It cannot be used with a Save As dialog box.

OFN_FORCESHOWHIDDEN

Windows 2000/XP: Forces the showing of system and hidden files, thus overriding the user setting to show or not show hidden files. However, a file that is marked both system and hidden is not shown.

OFN_HIDEREADONLY

Hides the Read Only check box.

OFN_LONGNAMES

For old-style dialog boxes, this flag causes the dialog box to use long file names. If this flag is not specified, or if the OFN_ALLOWMULTISELECT flag is also set, old-style dialog boxes use short file names (8.3 format) for file names with spaces. Explorer-style dialog boxes ignore this flag and always display long file names.

OFN_NOCHANGEDIR

Restores the current directory to its original value if the user changed the directory while searching for files.

Windows NT 4.0/2000/XP: This flag is ineffective for GetOpenFileName.

OFN_NODEREFERENCELINKS

Directs the dialog box to return the path and file name of the selected shortcut (.LNK) file. If this value is not specified, the dialog box returns the path and file name of the file referenced by the shortcut.

OFN_NOLONGNAMES

For old-style dialog boxes, this flag causes the dialog box to use short file names (8.3 format). Explorer-style dialog boxes ignore this flag and always display long file names.

OFN_NONETWORKBUTTON

Hides and disables the Network button.

OFN_NOREADONLYRETURN

Specifies that the returned file does not have the Read Only check box selected and is not in a write-protected directory.

OFN_NOTESTFILECREATE

Specifies that the file is not created before the dialog box is closed. This flag should be specified if the application saves the file on a create-nonmodify network share. When an application specifies this flag, the library does not check for write protection, a full disk, an open drive door, or network protection. Applications using this flag must perform file operations carefully, because a file cannot be reopened once it is closed.

OFN_NOVALIDATE

Specifies that the common dialog boxes allow invalid characters in the returned file name. Typically, the calling application uses a hook procedure that checks the file name by using the FILEOKSTRING message. If the text box in the edit control is empty or contains nothing but spaces, the lists of files and directories are updated. If the text box in the edit control contains anything else, nFileOffset and nFileExtension are set to values generated by parsing the text. No default extension is added to the text, nor is text copied to the buffer specified by lpstrFileTitle. If the value specified by nFileOffset is less than zero, the file name is invalid. Otherwise, the file name is valid, and nFileExtension and nFileOffset can be used as if the OFN_NOVALIDATE flag had not been specified.

OFN_OVERWRITEPROMPT

Causes the Save As dialog box to generate a message box if the selected file already exists. The user must confirm whether to overwrite the file.

OFN_PATHMUSTEXIST

Specifies that the user can type only valid paths and file names. If this flag is used and the user types an invalid path and file name in the File Name entry field, the dialog box function displays a warning in a message box.

OFN_READONLY

Causes the Read Only check box to be selected initially when the dialog box is created. This flag indicates the state of the Read Only check box when the dialog box is closed.

OFN_SHAREAWARE

Specifies that if a call to the OpenFile function fails because of a network sharing violation, the error is ignored and the dialog box returns the selected file name. If this flag is not set, the dialog box notifies your hook procedure when a network sharing violation occurs for the file name specified by the user. If you set the OFN_EXPLORER flag, the dialog box sends the CDN_SHAREVIOLATION message to the hook procedure. If you do not set OFN_EXPLORER, the dialog box sends the SHAREVISTRING registered message to the hook procedure.

OFN_SHOWHELP

Causes the dialog box to display the Help button. The hwndOwner member must specify the window to receive the HELPMSGSTRING registered messages that the dialog box sends when the user clicks the Help button. An Explorer-style dialog box sends a CDN_HELP notification message to your hook procedure when the user clicks the Help button.

OFN_USESHELLITEM

Do not use.

时间: 2024-11-10 07:49:38

MFC中的几个常用类——CFileDialog的相关文章

java中必须知道的常用类

1.Java的包装类 基本数据类型我们都很熟悉,例如:int.float.double.boolean.char等,基本数据类型不具备对象的特征,不能调用方法,一般能实现的功能比较简单,为了让基本数据类型也具备对象的特性,Java为每个数据类型都提供了一个包装类,这样我们就可以像操作对象一样,操作这些基本数据类型了 常见的包装类和基本类型对应关系如下: 包装类主要提供了两类方法: 1.进行多个类型之间的转换 2.将字符串和本类型及包装类相互转换 比如下面代码: 1 int i = 2; 2 In

Java中的集合和常用类

Java中的常用类: ? Object类 ? Math类 ? String类和StringBuffer类(字符串) ? 8种基本类型所对应的包装类 ? java.util包中的类——Date类 Object类: Object类是Java语言程序中所有类的父类,即承自Object类.Object类中包含了Java语言类中的所有的公共属性. ? toString()方法 ? equals()方法 ? getClass()方法 ? clone()方法 ? finalize()方法 枚举类: 用于储存变

MFC中怎样完全删除一个类

测试环境:Windows7 32bit.VC++6.0 VC++6.0中添加类很方便,不过删除一个类就没有那么直观了.如果想删除了一个类,其实只需要这样做: 在FileView中的Source Files和Header Files中分别移除类对应的.cpp和.h文件(点击该文件,然后按Delete即可移除,文件仅从工程中移除,并没有在工程目录下被删除) 关闭VC++6.0,然后删除工程目录下的.clw文件,以及类对应的.cpp和.h文件 重新启动VC++6.0,选择View中ClassWizar

java中必须了解的常用类

java中的包装类 为何有包装类的出现? 基本数据类型不具备对象的特性的,比如基本类型不能调用方法.功能简单. 为了让基本数据类型也具备对象的特性, Java 为每个基本数据类型都提供了一个包装类,这样就可以像操作对象那样来操作基本数据类型. 每个基本类型都有相对应的包装类: 包装类主要提供了两大类方法: 将本类型和其他基本类型进行转换的方法. 将字符串和本类型及包装类互相转换的方法 下面看一个例子: public class HelloWorld { public static void ma

Java 中必的常用类(很实用)

Java中必须了解的常用类 一.包装类 相信各位小伙伴们对基本数据类型都非常熟悉,例如 int.float.double.boolean.char 等.基本数据类型是不具备对象的特性的,比如基本类型不能调用方法.功能简单...,为了让基本数据类型也具备对象的特性, Java 为每个基本数据类型都提供了一个包装类,这样我们就可以像操作对象那样来操作基本数据类型. 基本类型和包装类之间的对应关系: 注意:有两个包装类的名称比较特殊一个是Integer,另一个是Character,其他都是基本数据类首

MFC的运行机制 以及 MFC中的DC、CDC、HDC、句柄、设备上下文 [转]

在MFC程序中,我们并不经常直接调用Windows API,而是从MFC类创建对象并调用属于这些对象的成员函数.也就是说MFC封装了Windows API.你说你喜欢C++而MFC换一种说法就是一个用C++写的一个函数库 然后你来调用 只不过这个类不是你写的 MFC提供数百个类,最重要的.也是编写任何VC++应用程序都必不可少的两个类CWinApp和CFrameWnd,这两个类是编写复杂庞大应用程序的基石. 1>封装特性:构成MFC框架的是MFC类库而MFC类库又是C++的一个类库.这些类封装W

MFC中的GDI绘图&lt;转&gt;

一.关于GDI的基本概念 什么是GDI? Windows绘图的实质就是利用Windows提供的图形设备接口GDI(Graphics Device Interface)将图形绘制在显示器上. 在Windows操作系统中,动态链接库C:\WINDOWS\system32\gdi32.dll(GDI Client DLL)中定义了GDI函数,实现与设备无关的包括屏幕上输出像素.在打印机上输出硬拷贝以及绘制Windows用户界面功能.在Visual C++6.0中的头文件C:\Program Files

MFC中的GDI绘图(1)

一.关于GDI的基本概念 什么是GDI         Windows绘图的实质就是利用Windows提供的图形设备接口GDI(Graphics Device Interface)将图形绘制在显示器上. 在Windows操作系统中,动态链接库C:\WINDOWS\system32\gdi32.dll(GDI Client DLL)中定义了GDI函数,实现与设备无关的包括屏幕上输出像素.在打印机上输出硬拷贝以及绘制Windows用户界面功能.在Visual C++6.0中的头文件C:\Progra

MFC中的一般经验之谈5

MFC中提供了CString的类,可以用在一切使用字符串的地方.可以完美取代const char* 或者 LPCTSTR(经常在WinAPI)中传递参数.并且如果需要通过空间进行字符串的数据传递DDX机制,则必须通过CString类型传递. 使用编辑框控件 添加对话框上的编辑框下的控件类型,通过如下类向导的方式:将控件ID与控件类型的成员变量关联.在类别中定义的是空间类别,或者是值类别.注意如果是值类别的时,则需要定义如果使用string类型,则必须使用CString类型.