CFileDialog 类 封装了Windows通用文件对话框,Windows通用文件对话框提供了轻松实现与Windows标准一致的打开文件、保存文件、另存文件对话框的方法。

当我们用CFileDialog类的构造函数生成一个对象后就修改m_ofn 结构体对象里的值,m_ofn的类型为OPENFILENAME。CFileDialog类构造函数的格式如下:

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

bOpenFileDialog 如果为值TRUE,构造打开对话框,反之为保存对话框

lpszDefExt 用于确定文件的默认扩展名,如果为NULL,没有扩展名被插入到文件名中。

lpszFileName 确定通用文件对话框中的文件名编辑框控件的初值,如果为NULL,编辑框为空,没有文件名。

dwFlags 用于自定义通用文件对话框。有以下选项:

一般情况下,使用缺省值即可。

lpszFilter 用于指定过滤文件的类型。

pParentWnd 标识通用文件对话框的父窗口的指针。

OPENFILENAME 结构体原型如下:

lpstrFilter

 Pointer to a buffer containing pairs of null-terminated filter strings. The last string in the buffer must be terminated by two NULL characters.The first string in each pair is a display string that describes the filter (for example, "Text Files"), and the second string specifies the filter pattern (for example, "*.TXT"). To specify multiple filter patterns for a single display string, use a semicolon to separate the patterns (for example, "*.TXT;*.DOC;*.BAK"). A pattern string can be a combination of valid file name characters and the asterisk (*) wildcard character. Do not include spaces in the pattern string.The system does not change the order of the filters. It displays them in the File Types combo box in the order specified in lpstrFilter.If lpstrFilter is NULL, the dialog box does not display any filters.

lpstrCustomFilter 

Pointer to a static buffer that contains a pair of null-terminated filter strings for preserving the filter pattern chosen by the user. The first string is your display string that describes the custom filter, and the second string is the filter pattern selected by the user. The first time your application creates the dialog box, you specify the first string, which can be any nonempty string. When the user selects a file, the dialog box copies the current filter pattern to the second string. The preserved filter pattern can be one of the patterns specified in the lpstrFilter buffer, or it can be a filter pattern typed by the user. The system uses the strings to initialize the user-defined file filter the next time the dialog box is created. If the nFilterIndex member is zero, the dialog box uses the custom filter.If this member is NULL, the dialog box does not preserve user-defined filter patterns.If this member is not NULL, the value of the nMaxCustFilter member must specify the size, in TCHARs, of the lpstrCustomFilter buffer. For the ANSI version, this is the number of bytes; for the Unicode version, this is the number of characters.

lpstrFile

Pointer to a buffer that contains a file name used to initialize the File Name edit control. The first character of this buffer must be NULL if initialization is not necessary. When the GetOpenFileName or GetSaveFileName function returns successfully, this buffer contains the drive designator, path, file name, and extension of the selected file.If the OFN_ALLOWMULTISELECT flag is set and the user selects multiple files, the buffer contains the current directory followed by the file names of the selected files. For Explorer-style dialog boxes, the directory and file name strings are NULL separated, with an extra NULL character after the last file name. For old-style dialog boxes, the strings are space separated 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 the user selects only one file, the lpstrFile string does not have a separator between the path and file name.If the buffer is too small, the function returns FALSE and the CommDlgExtendedError function returns FNERR_BUFFERTOOSMALL. In this case, the first two bytes of the lpstrFile buffer contain the required size, in bytes or characters.

lpstrFileTitle

Pointer to a buffer that receives the file name and extension (without path information) of the selected file. This member can be NULL.

lpstrInitialDir

Pointer to a null terminated string that can specify the initial directory. The algorithm for selecting the initial directory varies on different platforms.

要显示通用文件对话框,使用DoModal函数即可。DoModal函数的返回值为IDOK or IDCANCEL.如果用户在用文件对话框点击了OK按钮就返回IDOK,击了CANCELL按钮就返回IDCANCEL。

CFileDialog 类还有以下成员函数:

通用文件对话框示例

1. 打开VS2017,新建一个MFC单文档程序。

2. 在IDR_MAINFRAME菜单中,新建FileDialogTest菜单及子菜单,如下:

3. 给“保存文件”子菜单添加事件处理程序:

代码如下:

void CFileDialogTestView::OnSaveFile()
{// TODO: 在此添加命令处理程序代码CFileDialog fdlg(FALSE, NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,L"All files(*.txt)|*.TXT|*.JPG|*.PNG|*.BMP||" );fdlg.DoModal();
}

按Ctrl+F5运行程序,点击FileDialogTest菜单下的“保存文件”子菜单,弹出通用文件对话框如下:

通用文件对话框名称是“另存为”,缺省的路径为文档库。将代码修改如下:

void CFileDialogTestView::OnSaveFile()
{// TODO: 在此添加命令处理程序代码CFileDialog fdlg(FALSE, NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, L"All files(*.txt)|*.TXT|*.JPG|*.BMP||");fdlg.m_ofn.lpstrTitle = L"Save File";fdlg.m_ofn.lpstrInitialDir = L"D:\\360MoveData\\Users\\lys\\Documents";fdlg.DoModal();
}

执行同样的操作,弹出通用文件对话框如下:

给“另存文件”子菜单添加事件处理程序:

代码如下:

void CFileDialogTestView::OnSaveAsFile()
{// TODO: 在此添加命令处理程序代码CFileDialog fdlg(FALSE, NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, L"All files(*.txt)|*.TXT|*.JPG|*.PNG|*.BMP||");fdlg.m_ofn.lpstrTitle = L"Save File As";fdlg.m_ofn.lpstrInitialDir = L"D:\\360MoveData\\Users\\lys\\Documents";fdlg.DoModal();
}

按Ctrl+F5运行程序,点击FileDialogTest菜单下的“另存文件”子菜单,弹出通用文件对话框如下:

给“打开文件”子菜单添加事件处理程序:

代码如下:

void CFileDialogTestView::OnOpenFile()
{// TODO: 在此添加命令处理程序代码CFileDialog fdlg(TRUE, NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, L"All files(*.*)|*.*|*.JPG|*.PNG|*.BMP||");fdlg.m_ofn.lpstrTitle = L"Open File";fdlg.m_ofn.lpstrInitialDir = L"F:\\lesson\\Program\\c++\\MFC";if (fdlg.DoModal() == IDOK){CString str = fdlg.GetPathName();MessageBox(str);str = fdlg.GetFileName();MessageBox(str);str = fdlg.GetFileExt();MessageBox(str);str = fdlg.GetFileTitle();MessageBox(str);str = fdlg.GetFolderPath();MessageBox(str);}
}

按Ctrl+F5运行程序,点击FileDialogTest菜单下的“打开文件”子菜单,弹出通用文件对话框如下:

点击【STL】C++标准程序库STL.pdf,再点击打开,弹出信息框,如下:

点击确定后,弹出第二个 信息框,如下:

点击确定, 弹出第三个 信息框,如下:

点击确定, 弹出第四个 信息框,如下:

点击确定, 弹出第五个 信息框,如下:

现将“保存文件”子菜单的事件处理代码,修改如下:

void CFileDialogTestView::OnSaveFile()
{// TODO: 在此添加命令处理程序代码//CFileDialog fdlg(FALSE, NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, L"All files(*.txt)|*.TXT|*.JPG|*.PNG|*.BMP||");CFileDialog fdlg(FALSE);fdlg.m_ofn.lpstrDefExt = L"txt";fdlg.m_ofn.lpstrCustomFilter = L"*.txt";fdlg.m_ofn.lpstrFilter = L"*.txt";fdlg.m_ofn.lpstrTitle = L"Save File";fdlg.m_ofn.lpstrInitialDir = L"D:\\360MoveData\\Users\\lys\\Documents";fdlg.DoModal();
}

按Ctrl+F5运行程序,点击FileDialogTest菜单下的“保存文件”子菜单,弹出通用文件对话框如下:

文件名编辑框内时空的,可否给fdlg.m_ofn.lpstrFile赋值,在上面代码中加入fdlg.m_ofn.lpstrTitle = L"Save File As";

void CFileDialogTestView::OnSaveAsFile()
{// TODO: 在此添加命令处理程序代码CFileDialog fdlg(FALSE, NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, L"All files(*.txt)|*.TXT|*.JPG|*.PNG|*.BMP||");fdlg.m_ofn.nMaxFile = sizeof("myfile");fdlg.m_ofn.lpstrFile = L"myfile";fdlg.m_ofn.lpstrTitle = L"Save File As";fdlg.m_ofn.lpstrInitialDir = L"D:\\360MoveData\\Users\\lys\\Documents";fdlg.DoModal();
}

按Ctrl+F5运行程序,点击FileDialogTest菜单下的“保存文件”子菜单,结果出现如下情况:

在上面代码中fdlg.m_ofn.lpstrFile = L"myfile";前加入fdlg.m_ofn.nMaxFile的赋值语句,如下:

void CFileDialogTestView::OnSaveFile()
{// TODO: 在此添加命令处理程序代码//CFileDialog fdlg(FALSE, NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, L"All files(*.txt)|*.TXT|*.JPG|*.PNG|*.BMP||");CFileDialog fdlg(FALSE);fdlg.m_ofn.lpstrDefExt = L"txt";fdlg.m_ofn.lpstrCustomFilter = L"*.txt";fdlg.m_ofn.lpstrFilter = L"*.txt";fdlg.m_ofn.lpstrTitle = L"Save File";fdlg.m_ofn.nMaxFile = sizeof("myfile");fdlg.m_ofn.lpstrFile = L"myfile";fdlg.m_ofn.lpstrInitialDir = L"D:\\360MoveData\\Users\\lys\\Documents";fdlg.DoModal();
}

按Ctrl+F5运行程序,点击FileDialogTest菜单下的“保存文件”子菜单,结果如下:

不再发生错误。结果表明,要给fdlg.m_ofn.lpstrFile赋值,必须先给fdlg.m_ofn.nMaxFile赋值,否则会出错。

MFC 通用对话框之文件对话框相关推荐

  1. VS2010/MFC编程入门之十七(对话框:文件对话框)

    上一讲鸡啄米介绍的是消息对话框,本节讲解文件对话框.文件对话框也是很常用的一类对话框. 文件对话框的分类       文件对话框分为打开文件对话框和保存文件对话框,相信大家在Windows系统中经常见 ...

  2. python表白对话框-python tkinter-消息框、对话框、文件对话框

    消息框 导入 import tkinter import tkinter.messagebox #这个是消息框,对话框的关键 提示消息框 tkinter.messagebox.showinfo('提示 ...

  3. Matlab App Designer自学笔记(七):对话框的使用(提示对话框、提问对话框、文件对话框)

    一."提示"对话框 在"关于"按钮上添加回调: help 可以换成none.error.help.warn任何一个,提示语左边的图标会发生变化. matlab中 ...

  4. python打开文件夹对话框_文件对话框打开文件夹中的文件(tkinter)

    我想把它实现到我自己的代码中,但是当我运行这个(没有我的代码,只有你看到的代码)时,所有显示的文件夹都是空的,我实际上不能打开任何东西.在from tkinter import * from tkin ...

  5. MFC中的文件对话框类CFileDialog详解

    目录 文件对话框的分类 文件对话框类CFileDialog 对话框选项常用属性(实例:dialog) 对话框常用方法 文件对话框实例 文件对话框的分类       文件对话框分为打开文件对话框和保存文 ...

  6. Android开发笔记(二十三)文件对话框FileDialog

    日期和时间对话框 对话框是人机交互的有力工具,Android自带了几个常用的对话框,包括AlertDialog提示对话框.ProgressDialog进度对话框.DatePickerDialog日期选 ...

  7. java swing对话框_Java开发笔记(一百三十五)Swing的文件对话框

    除了常规的提示对话框,还有一种对话框也很常见,它叫做文件对话框.文件对话框又分为两小类:打开文件的对话框.保存文件的对话框,但在Swing中它们都用类型JFileChooser来表达.下面是JFile ...

  8. MFC实现打开、保存文件对话框和浏览文件夹对话框,把代码直接拷贝到要响应的按钮函数下面就行了

    MFC实现打开.保存文件对话框和浏览文件夹对话框,把代码直接拷贝到要响应的按钮函数下面就行了 一.打开.保存对话框 文件对话框属于通用对话框范畴(另外还有颜色,查找,查找替换,字体,打印等对话框). ...

  9. MFC程序打开文件对话框出错的问题解决

    前几天从网上下了个图像分析的mfc小程序,是VC6的 用VC6在本地编译生成都没问题.执行起来弹出一个未处理的错误,程序崩溃退出. 想起来原来遇到过打开文件对话框方面的问题,当时项目时间紧张未能深究. ...

  10. MFC中打开文件对话框:CFileDlg

    MFC中打开文件对话框:CFileDlg CFileDialog 文件选择对话框的使用:首先构造一个对象并提供相应的参数,构造函数原型如下: CFileDialog::CFileDialog( BOO ...

最新文章

  1. k-NN最近邻算法(k-nearest neighbors algorithm)
  2. python中list的反转_Python实现list反转实例汇总
  3. C++数据结构struct
  4. CSS节选——选择器
  5. 数据结构 算法与应用C 语言描述答案,数据结构算法与应用-C语言描述.pdf
  6. 前端学习(1557):安全问题
  7. C#多线程编程系列(三)- 线程同步
  8. 最大公约数(Greatest Common Divisor)
  9. c#调用c++ dll const char* String类型转换问题。传值,与接收返回值问题
  10. mysql 统计 1的数量_利用MySQL统计一列中不同值的数量方法示例
  11. 控制方法只有相应权限才可执行
  12. cisco将计算机配置为vlan2,Cisco交换机 VLAN 的建立与端口分配
  13. cadlisp点选面积标注_一个在CAD中标注坐标的LISP
  14. 两个mysql 数据库表结构_MYSQL对比两个数据库表结构
  15. usb接上计算机没反应怎么办,u盘插上去电脑没反应怎么办 u盘插上后无任何反应的解决教程...
  16. 绝地潜兵服务器不稳定,爽快的合作射爆游戏《绝地潜兵》评测评分汇总
  17. 采油工技能鉴定高级工计算机6,采油工技师、高级技师技能鉴定题库(宝典).doc...
  18. mysql 单表关联_MySQL 基础之 单表、多表联查
  19. 实现通过公网远程访问运行在服务器上的Python程序
  20. 捷克avast杀毒软件专业版

热门文章

  1. 关于图像处理中的矩阵卷积运算
  2. 什么是App加壳,以及App加壳的利与弊
  3. IP地址、子网掩码、网络数、主机数、广播地址及其计算方法
  4. 怎么检查计算机和打印机是否连接网络,如何检查电脑中是否已成功连接网络打印机...
  5. 支付宝用AR集五福,VR和MR怎么看?
  6. Unity WebGL基于js通信实现网页录音
  7. 查找算法之二分查找算法
  8. julia 调用python库_install julia with python
  9. 飞鸽传书——空号检测
  10. 明明是旅游小程序却做起了内容电商?