MFC中的几个常用类——CFileDialog

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

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.

 

转载于:https://my.oschina.net/u/1024767/blog/353720

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/545630.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Exchange Server2010部署完后的配置:CA、Outlook Anywhere、OWA域名简写

Exchange Server 2010邮件系统安装完成后&#xff0c;必须经过相应的配置后&#xff0c;才能使Exchange Server 2010邮件系统提供基本的访问、邮件收发等基本功能。下面我们逐一看看如何让Exchanger Server跑起来。Exchange Server2010产品授权&#xff1a;我们目前所安装的Exc…

STM32——PID恒温控制

原理 元件 stm32f103核心板、L298N模块(当然用MOS管更好)、led一个、NPN三极管一个、蜂鸣器一个、DHT11一个、LCD1602一个、电阻200欧两个、可调电阻10K一个、加热丝一个 功能描述 用DHT11检测当前环境温湿度&#xff0c;并将数据显示在LCD1602上&#xff0c;在用设定温度与当…

第 6-2 课:SpringMVC 核心 + 面试题

Spring MVC 介绍 Spring MVC(Spring Web MVC)是 Spring Framework 提供的 Web 组件,它的实现基于 MVC 的设计模式:Controller(控制层)、Model(模型层)、View(视图层),提供了前端路由映射、视图解析等功能,让 Java Web 开发变得更加简单,也属于 Java 开发中必须要…

Lync2013 升级错误总结8 Lync2013 日志总是提示进程 RtcHost(5724) 收到了一个无效的客户端证书...

错误提示&#xff1a;解决方法&#xff1a;1打开注册表引导到&#xff1a;HKLM\System\CurrentControlSet\Control\SecurityProviders\Schannel2 新建一个DWORD键值&#xff1a;值的名称&#xff1a;EnableSessionTicket3 右键这个值点编辑讲数值数据修改成&#xff1a;24 重新…

简易的遍历文件加密解密

功能描述 将生成的可执行程序放在指定的文件夹内&#xff0c;双击后将该目录下所有文件包括子文件夹内文件全部加密&#xff0c;再次双击运行后将进行解密。 加密解密实现 主要运用了异或与取反操作&#xff0c;异或&#xff1a;两个值不同为1&#xff0c;相同为0。取反就是将该…

安卓手机使用linux(含图形界面)——Aid Learning

以前再安卓手机上使用linux系统都是使用Termux&#xff0c;安装上很麻烦&#xff0c;而且还是黑乎乎的窗口&#xff0c;没有图形界面&#xff0c;对于初学linux者来说并不友好&#xff0c;而Aid Learning就更人性化了&#xff0c;他是一种模拟的linux,其安装十分简易&#xff0…

简易花式流水灯

先看看效果 具体思路 实现流水灯的效果其实就是控制相应的I/O口&#xff0c;以P2为例&#xff0c;通过有规律的改变P2各I/O口的状态就可实现相应规律的流水灯效果&#xff0c;这其中需要用到与、或、异或、左移、右移等操作。   流水灯向左闪烁点亮就是将P2最低位的1不断左移…

STM32——直流电机PI调速

所需元件 STM32F103开发板、L298N一个、带编码器的直流电机一个&#xff08;如下图所示&#xff0c;淘宝上有很多&#xff09; 系统框图 通过系统框图&#xff0c;我们需要做两件事&#xff0c;一是要测速&#xff0c;二是要调节。测速目前流行的就是通过编码器测速&#xff…

JAVA设计模式--简单介绍

2019独角兽企业重金招聘Python工程师标准>>> &#xfeff;一、简介 Design pattern 是众多软件开发人员经过漫长的试验和错误总结出来的在软件开发过程中面临一般问题的解决方案&#xff0c;代表着最佳实践。使用设计模式是为了重用代码、让代码更容易被他人理解、保…

为什么是PID控制

在进入正式话题之前需要引入四个概念&#xff1a;稳态误差、终值定理、幅角条件和系统稳定的充要条件。 稳态误差&#xff1a;系统达到稳定状态后&#xff0c;系统的实际输出量与系统希望的输出量之间的偏差。 终值定理&#xff1a;设有连续函数f(t)f(t)f(t)&#xff0c;当t趋于…

卡尔曼滤波器推导

注&#xff1a;受控制领域大牛CAN博士启发&#xff0c;受益匪浅&#xff0c;作此文以为笔记。 简介 设 卡尔曼滤波器是从测量值ZZZk的平均数开始的。开始推导&#xff1a; 由上式可知   也就是说随着kkk的增大&#xff0c;测量结果Zk不在重要&#xff0c;因为已经获得了足…

cocos2dx3.2文件结构和代码结构

既然选定了cocos2dxlua的原生方式来开发&#xff0c;首先要确定的是使用哪个版本的cocos2dx&#xff0c;先看看github上的changelog和releasenote&#xff0c;然后在google里搜索一下&#xff0c;参考了jacky的博客http://zengrong.net/post/2100.htm&#xff0c;最终选择了coc…

改进的PID算法

位置式PID算法 位置式PIDPIDPID算法是一种比较直观的的PIDPIDPID算法&#xff0c;如系统框图中所示&#xff0c;ininin表示设定值&#xff0c;errorerrorerror表示差值&#xff0c;uuu表示控制器输出值&#xff0c;outoutout表示被控量。算法表达式如下&#xff1a; 增量式PI…

几种简单电路知识汇总

这篇文章用于记录平时设计电路或者在书中遇到的一些电路方面的知识&#xff0c;会不定期更新。就先从运算放大器开始&#xff0c;对此做个简单的介绍。 运算放大器 说到运算放大器就不得不说两个概念&#xff0c;虚短与虚断。 虚短&#xff1a; 在理想情况下&#xff0c;运算…

51单片机——交通灯

原理图 功能描述 1、基本功能就是如同红绿灯一般&#xff0c;不做赘述。   2、红灯时长和绿灯时长可通过按键设置&#xff0c;即按键列中的上面4个&#xff0c;当这4个按键有一个按下后便进入时长设置功能&#xff0c;设置完成后按最下面两个按键&#xff08;紧急控制按钮&am…

设置TextField内文字距左边框的距离

2019独角兽企业重金招聘Python工程师标准>>> //设置文本框左边的viewUITextField *textField [[UITextField alloc]init];textField.frame CGRectMake(10, 30, 300, 30);[self.view addSubview:textField];textField.leftView [[UIView alloc]initWithFrame:CGRe…

类的三大特性

类有三大特性&#xff1a;继承&#xff0c;封装&#xff0c;多态&#xff0c;这个也是介绍类的时候&#xff0c;必须提到的话题&#xff0c;那么今天就来看一下OC中类的三大特性&#xff1a; 一、封装 学习过Java中类的同学可能都知道了&#xff0c;封装就是对类中的一些字段&a…

操作系统抢占式优先级调度_操作系统中的优先级调度(非抢先)

操作系统抢占式优先级调度Priority scheduling is a type of scheduling algorithm used by the operating system to schedule the processes for execution. The priority scheduling has both the preemptive mode of scheduling and the non-preemptive mode of scheduling…

数据结构pta选择判断复习

第一章绪论 1-3数据的逻辑结构是指数据的各数据项之间的逻辑关系。 错 是数据元素之间的逻辑关系 2-4以下属于逻辑结构的是&#xff08; &#xff09;。 (2分) 顺序表 散列表 有序表 单链表 有序表 2-12以下关于数据结构的说法中正确的是____。 (2分) A数据结构的逻辑结构独立于…

粗略的看JFinal的基于AOP的拦截器的实现

2019独角兽企业重金招聘Python工程师标准>>> 简单的说一下AOP的实现&#xff0c;所谓AOP&#xff0c;即&#xff08;Aspect Oriented Programming&#xff09;的缩写&#xff0c;体现在程序中就是你可以通过配置在任意的代码块前后插入你想插入的执行代码。例如日志…