【发布时间】:2023-01-12 06:40:25
【问题描述】:
C++/MFC,Windows 11。Visual Studio 2022 17.4.3。
我正在使用 CFileDialog 来允许用户选择一个文件。通过创建一个派生自CFileDialog 的新类,每当用户更改目录(文件夹)时我都会收到通知。
我实现了这个,所以我可以控制应用于目录中文件列表的过滤器。但是,我在这方面没有成功。即使我不更改m_ofn,我也会收到错误消息。
这是示例代码:
// Caller
#include "Browsing_test.h"
P brTest(true, NULL, NULL, 0, fileTypes);
brTest.BrowseTest();
// Browsing_test.h
class P : CFileDialog
{
public:
P(BOOL bOpenFileDialog,
LPCTSTR lpszDefExt = NULL,
LPCTSTR lpszFileName = NULL,
DWORD dwFlags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
LPCTSTR lpszFilter = NULL,
CWnd *pParentWnd = NULL,
DWORD dwSize = 0,
BOOL bVistaStyle = TRUE) : CFileDialog(bOpenFileDialog,
lpszDefExt,
lpszFileName,
dwFlags,
lpszFilter,
pParentWnd,
dwSize,
bVistaStyle) {};
int BrowseTest(void);
#include "stdafx.h"
#include "Browsing_test.h"
int P::BrowseTest(void)
{
int resultDebug = (int)DoModal();
return resultDebug;
}
void P::OnFolderChange()
{
auto s = GetOFN(); // for modifying m_ofn member of the base class,
// but not used in this sample code
// Add modificatons to m_ofn here
ApplyOFNToShellDialog(); // Gets assert on updating flags
}
运行此代码会在 dlgfile.cpp(Microsoft 代码)中出现错误
hr = (static_cast<IFileDialog*>(m_pIFileDialog))->SetOptions(dwFlags);
返回 hr = E_UNEXPECTED Catastrophic failure. dwFlags 的值为十六进制 40。
【问题讨论】:
-
糟糕的错误代码,在 COM 中并不罕见。它试图告诉您当对话框处于活动状态时您不能修改选项。从技术上讲,您可以关闭该对话框并再次显示它,但这并不美观。
-
@HansPassant:这听起来像是解释,如果是的话,没有简单的方法可以做我正在寻找的事情。
标签: c++ fileopendialog cfiledialog