gpt4 book ai didi

c++ - 从 CFileDialog 调用时,IFileOpenDialog 指针为空

转载 作者:行者123 更新时间:2023-11-28 01:33:56 28 4
gpt4 key购买 nike

我试图将 CFileDialogIFileOpenDialog 包装在一个类中,这是代码:

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

然后我尝试直接调用CFileDialogIFileOpenDialog:

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

最佳答案

来自help :

Use this function only under Windows Vista with an object that has bVistaStyle set to true. This function returns NULL if the CFileDialog is not an Open dialog box or if bVistaStyle is set to false.

当您构建测试 对象时:

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

关于c++ - 从 CFileDialog 调用时,IFileOpenDialog 指针为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50239332/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com