我试图包装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(); 在类声明之外)