0

我试图包装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

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(); 在类声明之外)

4

1 回答 1

2

帮助

此函数只能在 Windows Vista 下使用 bVistaStyle 设置为 true 的对象。如果 CFileDialog 不是打开对话框或 bVistaStyle 设置为 false,则此函数返回 NULL。

当你构造你的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
于 2018-05-08T18:30:32.983 回答