【问题标题】:Allow IFileOpenDialog to pick a file that is not created yet允许 IFileOpenDialog 选择尚未创建的文件
【发布时间】:2018-07-04 07:05:26
【问题描述】:

我在许多应用程序中都看到了这一点。你可以选择打开一个文件,如果它不存在,它会被创建,你不会得到任何抱怨。全部来自同一个打开文件对话框。 我使用IFileOpenDialog 打开一个文件,如果我输入一个不存在的文件,它会显示一个错误并且我无法获取该文件的路径。

我想要的不是得到错误,而是接受不存在的文件名。稍后我将创建它。这可能吗?

if (SUCCEEDED(hr))
{
    IFileOpenDialog *pFileOpen;

    // Create the FileOpenDialog object
    hr = CoCreateInstance(
        CLSID_FileOpenDialog,
        NULL, 
        CLSCTX_ALL, 
        IID_IFileOpenDialog, 
        reinterpret_cast<void**>(&pFileOpen)
    );

    if (SUCCEEDED(hr))
    {
        // Show the Open dialog box.
        hr = pFileOpen->Show(NULL);

        // Get the file name from the dialog box.
        if (SUCCEEDED(hr))
        {
            IShellItem *pItem;
            hr = pFileOpen->GetResult(&pItem);
            if (SUCCEEDED(hr))
            {
                PWSTR pszFilePath;
                hr = pItem->GetDisplayName(SIGDN_FILESYSPATH, &pszFilePath);

                // Check if file actually exists
                if (SUCCEEDED(hr))
                {
                    // Create file if not found
                    if (PathFileExists(pszFilePath) != 1)
                    {

                    }

                    CoTaskMemFree(pszFilePath);
                }
                pItem->Release();
            }
        }
        pFileOpen->Release();
    }
    CoUninitialize();
}

【问题讨论】:

    标签: c++ winapi openfiledialog


    【解决方案1】:

    call IFileDialog::GetOptions 获取默认选项,删除 FOS_PATHMUSTEXIST 和 FOS_FILEMUSTEXIST 位,然后使用 IFileDialog::SetOptions 设置新选项。

    【讨论】:

      猜你喜欢
      • 2012-01-10
      • 2011-04-08
      • 1970-01-01
      • 2017-07-06
      • 2016-01-04
      • 2017-10-26
      • 2020-09-16
      • 2019-12-11
      • 1970-01-01
      相关资源
      最近更新 更多