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菜单下的“保存文件”子菜单,结果如下:
文章来源:https://www.toymoban.com/news/detail-452092.html
不再发生错误。结果表明,要给fdlg.m_ofn.lpstrFile赋值,必须先给fdlg.m_ofn.nMaxFile赋值,否则会出错。文章来源地址https://www.toymoban.com/news/detail-452092.html
到了这里,关于MFC 通用对话框之文件对话框的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!