我试图将CFileDialog
和IFileOpenDialog
包装在一个类中,这是代码:
class ITest
{
public:
virtual ~ITest(){};
virtual INT_PTR DoModal() = 0;
virtual IFileOpenDialog* GetDlg() = 0;
};
class test : public ITest
{
public:
test(BOOL bOpenFileDialog, // TRUE for FileOpen, FALSE for FileSaveAs
LPCTSTR lpszDefExt = NULL,
LPCTSTR lpszFileName = NULL,
DWORD dwFlags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
LPCTSTR lpszFilter = NULL,
CWnd* pParentWnd = NULL,
DWORD dwSize = 0,
BOOL bVistaStyle = FALSE)
{
dlg = new CFileDialog(bOpenFileDialog, lpszDefExt, lpszFileName, dwFlags, lpszFilter, pParentWnd, dwSize, bVistaStyle);
};
~test(){};
INT_PTR DoModal(){ return S_OK; };
IFileOpenDialog* GetDlg(){ return dlg->GetIFileOpenDialog(); };
private:
CFileDialog* dlg;
};
然后当我称之为:
ITest* a = new test(TRUE, NULL, 0, OFN_ALLOWMULTISELECT | OFN_NOVALIDATE, LoadResourceString(IDS_MYSTRING), this);
fdlg = a->GetDlg();
fdlg->Show(NULL);//it crashes here since fdlg is NULL
然后我尝试直接致电CFileDialog
和IFileOpenDialog
:
IFileOpenDialog* fdlg = NULL;
CFileDialog* b = new CFileDialog(TRUE, NULL, 0, OFN_ALLOWMULTISELECT | OFN_NOVALIDATE, LoadResourceString(IDS_MYSTRING), this);
fdlg = b->GetIFileOpenDialog();
fdlg->Show(NULL);
这部分代码完美无缺。 谁知道崩溃发生的原因? 这让我很困惑,因为我觉得它们非常相似。
class test : public ITest
{
public:
test(BOOL bOpenFileDialog, // TRUE for FileOpen, FALSE for FileSaveAs
LPCTSTR lpszDefExt = NULL,
LPCTSTR lpszFileName = NULL,
DWORD dwFlags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
LPCTSTR lpszFilter = NULL,
CWnd* pParentWnd = NULL,
DWORD dwSize = 0,
BOOL bVistaStyle = FALSE)
{
dlg = new CFileDialog(bOpenFileDialog, lpszDefExt, lpszFileName, dwFlags, lpszFilter, pParentWnd, dwSize, bVistaStyle);
};
~test(){};
INT_PTR DoModal(){ return S_OK; };
IFileOpenDialog* GetDlg();
private:
CFileDialog* dlg;
};
IFileOpenDialog* test::GetDlg()
{
return dlg->GetIFileOpenDialog();
};
上面的代码也行不通(它定义了IFileOpenDialog * GetDlg();在类声明之外)
答案 0 :(得分:2)
来自help:
仅在具有对象的Windows Vista下使用此功能 bVistaStyle设置为true。如果CFileDialog,此函数返回NULL 不是“打开”对话框,或者如果bVistaStyle设置为false。
构建test
对象时:
ITest* a = new test(TRUE, NULL, 0, OFN_ALLOWMULTISELECT | OFN_NOVALIDATE, LoadResourceString(IDS_MYSTRING), this);
您要离开构造函数的最后一个参数bVistaStyle
,默认为FALSE
。所以只需添加它:
ITest* a = new test(TRUE, NULL, 0, OFN_ALLOWMULTISELECT | OFN_NOVALIDATE, LoadResourceString(IDS_MYSTRING), this, 0,
TRUE); // bVistaStyle