我试图将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


  仅在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

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

10-15 04:47