MFC 通用对话框之文件对话框

这篇具有很好参考价值的文章主要介绍了MFC 通用对话框之文件对话框。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

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 用于自定义通用文件对话框。有以下选项:

MFC 通用对话框之文件对话框

MFC 通用对话框之文件对话框

MFC 通用对话框之文件对话框MFC 通用对话框之文件对话框一般情况下,使用缺省值即可。 

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

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

OPENFILENAME 结构体原型如下:

MFC 通用对话框之文件对话框

 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 类还有以下成员函数:

MFC 通用对话框之文件对话框

 MFC 通用对话框之文件对话框

 通用文件对话框示例

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

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

MFC 通用对话框之文件对话框

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

MFC 通用对话框之文件对话框

代码如下:

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菜单下的“保存文件”子菜单,弹出通用文件对话框如下:

MFC 通用对话框之文件对话框

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

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();
}

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

MFC 通用对话框之文件对话框

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

MFC 通用对话框之文件对话框

 代码如下:

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菜单下的“另存文件”子菜单,弹出通用文件对话框如下:

MFC 通用对话框之文件对话框

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

MFC 通用对话框之文件对话框

 代码如下:

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菜单下的“打开文件”子菜单,弹出通用文件对话框如下:

MFC 通用对话框之文件对话框

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

MFC 通用对话框之文件对话框

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

MFC 通用对话框之文件对话框

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

MFC 通用对话框之文件对话框

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

MFC 通用对话框之文件对话框

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

MFC 通用对话框之文件对话框

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

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菜单下的“保存文件”子菜单,弹出通用文件对话框如下:

MFC 通用对话框之文件对话框

文件名编辑框内时空的,可否给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菜单下的“保存文件”子菜单,结果出现如下情况:

MFC 通用对话框之文件对话框

 在上面代码中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菜单下的“保存文件”子菜单,结果如下:

MFC 通用对话框之文件对话框

 不再发生错误。结果表明,要给fdlg.m_ofn.lpstrFile赋值,必须先给fdlg.m_ofn.nMaxFile赋值,否则会出错。文章来源地址https://www.toymoban.com/news/detail-452092.html

到了这里,关于MFC 通用对话框之文件对话框的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • MFC 对话框与控件的使用

    1、 熟悉 Windows 对话框应用程序开发的基本过程; 2、 学习标准控件的使用; 3、 学些 CommonDialog ActiveX 控件的使用; 4、 练习较复杂的交互式操作的控制流程; 5、 练习文件输入/输出流的使用。 实现一个具有一定实用价值的通讯录程序设计。界面如图所示 ①界面的基本设计

    2024年02月05日
    浏览(28)
  • MFC 给对话框添加图片背景

    在windows开发当中做界面的主要技术之一就是使用MFC,通常我们看到的QQ,360,暴风影音这些漂亮的界面都可以用MFC来实现。今天我们来说一下如何用MFC美化对话框,默认情况下,对话框的背景如下: 那么,我们如何将它的背景变成如下界面呢,而且还要保留对话框的移动功能,

    2024年02月06日
    浏览(31)
  • MFC为对话框资源添加类

    VC6新建一个对话框类型的工程; 建立之后资源中默认有2个对话框,一个是主对话框,About这个是默认建立的关于版权信息的; 然后主对话框有对应的.h和.cpp文件;可以在其中进行编程;   默认建立的有一个 关于 对话框;   在资源中新插入一个对话框,IDD_DIALOG1是对话框I

    2024年02月01日
    浏览(34)
  • Opencv显示图片(MFC基于对话框窗口)

    #1.编辑对话框按钮,修改各个按钮窗口ID和绑定变量 picture 控件 “打开图片”按钮控件 #2.编写显示图片函数 void DrawMat(cv::Mat img, UINT nID); .h文件进行声明 .cpp进行定义 #3.定义CString fun_LoadImg();函数 .h文件进行声明 .cpp文件进行定义 #4.双击“打开图片”按钮 系统自动弹出响应函数

    2024年04月15日
    浏览(37)
  • MFC--对话框的一个画图控件-HighSpeedChart

    下载链接在最下面  (免费) 可在vs2022,vs2019等vs版本使用  成果展示 下载好文件后,将  整个文件夹,添加到项目中,  1.之后,将控件拖到对话框里,修改属性到与图片相同            2.为对话框添加类,我这里添加的是  3.为控件增加变量,因为不是VS自带的控件 4.点

    2024年02月04日
    浏览(69)
  • MFC 单文档弹出对话框 标题图标

    创建MFC时 1、应用程序类型:以单个文档-项目样式选择MFC标准 菜单关联对话框  2、菜单关联-资源视图中的Menu-IDR_MAINFRAME(此处看想关联谁,如果是想关联一个对话框,那就先创建一个对话框再进行关联,如果是想关联一个类,可以看函数在哪个类下面从而进行关联) a.关联

    2024年02月06日
    浏览(29)
  • MFC基于对话框使用dll进行多语言切换

    title: MFC基于对话框使用dll进行多语言切换 categories:[MFC] tags:[音视频编程] date: 2021/12/15 作者:hackett 微信公众号:加班猿 Qt使用qm文件切换有两种加载方式,比较容易一些 在资源文件中加载(这个比较好): 优点: 在程序发布时不用把最新的.qm文件拷贝到加载路径中,降低了修

    2024年02月08日
    浏览(25)
  • MFC第十八天 非模式对话框、对话框颜色管理、记事本项目(查找替换、文字和背景色、Goto(转到)功能的开发)

    a)调用CDialog::Create函数实现,例如:QQ的聊天框,记事本的查找等。 b)非阻塞,对象的生命期必须足够,否则就析构时摧毁了。 c)可以采用堆空间申请多例模式,也可以用全局或者成员。 d)EndDialog只对模式对话框有效,对非模式对话框无效,如果要摧毁调用DestroyWindow (只是隐

    2024年02月17日
    浏览(36)
  • MFC为资源对话框添加消息处理函数和初始化控件

    现在我VC6新建了一个对话框工程;又在资源添加了一个新的对话框,并为新的对话框添加了名为CTestDlg的类; 在主对话框的cpp文件包含#include \\\"TestDlg.h\\\"; 在主对话框的cpp文件的OnInitDialog()成员函数中,添加2句,     CTestDlg tdlg;     tdlg.DoModal(); 就可以弹出这个对话框; 在新

    2024年01月18日
    浏览(35)
  • MFC基于对话框——仿照Windows计算器制作C++简易计算器

    目录 一、界面设计 二、设置成员变量 三、初始化成员变量  四、初始化对话框 ​五、添加控件代码 1.各个数字的代码(0~9) 2.清除功能的代码 3.退格功能的代码 4.加减乘除功能的代码 5.小数点功能的代码 6.正负号功能的代码 7.等于功能的代码 六、源码领取方式 制作好之后

    2024年02月05日
    浏览(44)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包