如何设置 CFileDialog 的默认文件类型?

发布于 2024-10-13 11:42:09 字数 471 浏览 6 评论 0原文

我正在使用 CFileDialog 来显示打开的文件对话框。我已按如下方式设置过滤器:

static TCHAR BASED_CODE szFilter[] = _T("Chart Files (*.xlc)|*.xlc|")
                                     _T("Worksheet Files (*.xls)|*.xls|Data Files (*.xlc;*.xls)|")
                                     _T("*.xlc; *.xls|All Files (*.*)|*.*||");

每当我 DoModal 对话框时,我都需要将默认文件类型设置为“工作表文件”。我不知道该怎么做。 MS Paint 正在做的事情是,当我们打开打开文件对话框时,它会选择“所有图片文件”。

请让我知道该怎么做。

I am using CFileDialog for displaying the open file dialog. I have set the filter as follows:

static TCHAR BASED_CODE szFilter[] = _T("Chart Files (*.xlc)|*.xlc|")
                                     _T("Worksheet Files (*.xls)|*.xls|Data Files (*.xlc;*.xls)|")
                                     _T("*.xlc; *.xls|All Files (*.*)|*.*||");

I need to set the default file type to be "Worksheet Files" whenever I DoModal the dialog box. I am unable to figure out how to do it. MS Paint is doing, it selects the "All Picture files" when we open the open file dialog.

Please let me know how to do it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清君侧 2024-10-20 11:42:09

You're looking for the SetDefExt function. This allows you to specify the default file extension for an open/save file dialog box. Remember that the string you specify should not contain a period (.).

Of course, you could also just specify this in the constructor. The second parameter is the default extension (lpszDefExt).

疯到世界奔溃 2024-10-20 11:42:09

你应该读和写
这段代码将在程序运行时完成这项工作。为了能够在下次运行程序时显示上次使用的选择,您可以将 LastIndex 的值存储在注册表中。

// A dialog box with several filters for various media file types
static int LastIndex = -1;          // Holds the last used filter. You can store it in the Registry to use it during next run.

const TCHAR szFilter[] = _T("Video Files (*.mpg, *.mov, *.mp4)|*.mpg;*.mov;*.mp4|Audio Files (*.wav, *.mp3, *.m4a, *.flac)|*.wav;*.mp3;*.m4a;*.flac|MXF Files (*.mxf)|*.mxf|All Files (*.*)|*.*||");

CFileDialog dlg(TRUE, _T("Select Media File"), NULL, OFN_HIDEREADONLY | OFN_FILEMUSTEXIST, szFilter, this);

if(LastIndex != -1) dlg.m_ofn.nFilterIndex = LastIndex; // restore last used index 
                                                        // from last time

if (dlg.DoModal() == IDOK)
{
    LastIndex = dlg.m_ofn.nFilterIndex; // Store last used index for next time
    CString sFilePath = dlg.GetPathName();
}

You should read and write
This code will do the job during the run time of your program. To be able to display the last used selection next time you run your program, you can store the value of LastIndex in the Registry.

// A dialog box with several filters for various media file types
static int LastIndex = -1;          // Holds the last used filter. You can store it in the Registry to use it during next run.

const TCHAR szFilter[] = _T("Video Files (*.mpg, *.mov, *.mp4)|*.mpg;*.mov;*.mp4|Audio Files (*.wav, *.mp3, *.m4a, *.flac)|*.wav;*.mp3;*.m4a;*.flac|MXF Files (*.mxf)|*.mxf|All Files (*.*)|*.*||");

CFileDialog dlg(TRUE, _T("Select Media File"), NULL, OFN_HIDEREADONLY | OFN_FILEMUSTEXIST, szFilter, this);

if(LastIndex != -1) dlg.m_ofn.nFilterIndex = LastIndex; // restore last used index 
                                                        // from last time

if (dlg.DoModal() == IDOK)
{
    LastIndex = dlg.m_ofn.nFilterIndex; // Store last used index for next time
    CString sFilePath = dlg.GetPathName();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文