【问题标题】:C++ Microsoft docs - File handling / Get folder pathC++ Microsoft docs - 文件处理/获取文件夹路径
【发布时间】:2022-06-21 06:37:01
【问题描述】:

我学过 C/C++ 基础并练习过,但我很难理解 Microsoft 文档,发现它令人困惑Documention example

例如:我尝试创建应该让用户打开的命令行程序 文件夹对话框并选择文件夹,因此文件夹路径应存储在变量中 做了研究,发现有很多方法可以实现这个目标,但最好的方法是使用 IFileDialog::GetFolder 方法(shobjidl_core.h) 文件对话框有什么区别?

主要问题: 如何根据文件对话框中的用户选择获取文件夹路径作为字符串变量?

有实用教程的c++资源吗?

我尝试了解如何使用以下对话框: Folder dialog

它指的是我: BROWSEINFOA structure

如果有人能解释我如何使用这个文件夹对话框或更好的东西会非常有帮助

windows/linux 文件系统处理的精彩教程

【问题讨论】:

  • 据我了解 TCHAR 是遗留的,我如何才能专注于必须最近的 C++ 库而不是遗留的库
  • 您可以在任何地方使用en_US.UTF-8 localechar*,以实现简单性和便携性。所有现代平台都只能使用 UTF-8
  • 我尝试使用 std::locale::global(std::locale("en_US.UTF-8"));但是也出现了同样的错误,这也使我在代码中其他部分使用的字符变得混乱,据我所知,我将问题编辑到了更现代的库
  • @None 你需要更多的编译器标志才能使std::locale("en_US.UTF-8") 工作

标签: c++


【解决方案1】:

我以前用 wcout 打印路径

    TCHAR path[260];
    BROWSEINFO bi = { 0 };
    LPITEMIDLIST pidl = SHBrowseForFolder(&bi);
    SHGetPathFromIDList(pidl, path);
    wcout << path << '\n';

我们也可以使用com接口:

int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, PWSTR pCmdLine, int nCmdShow);
    {
        HRESULT hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);
        if (SUCCEEDED(hr))
        {
            IFileOpenDialog* pFileOpen;

            // Create the FileOpenDialog object.
            hr = CoCreateInstance(CLSID_FileOpenDialog, NULL, CLSCTX_ALL,IID_IFileOpenDialog, reinterpret_cast<void**>(&pFileOpen));
            // if object created ( that's com object )
            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);

                        // Display the file name to the user.
                        if (SUCCEEDED(hr))
                        {
                            MessageBoxW(NULL, pszFilePath, L"File Path", MB_OK);
                            CoTaskMemFree(pszFilePath);
                            cout << pszFilePath;

                            std::string stringtoconvert;

                            std::wstring temp = std::wstring(stringtoconvert.begin(), stringtoconvert.end());
                            LPCWSTR lpcwstr = temp.c_str();
                        }
                        pItem->Release();
                    }
                }
                pFileOpen->Release();
            }
            CoUninitialize();
        }
        return 0;
    }

重要的是要提到: 打开和另存为常用对话框 (commdlg.h) 已被 Common Item Dialog 取代

对于 linux 和其他平台,有 Qt 等跨平台库,这里是UI solutions的链接

关于微软文档:我认为应该有更简单的示例和解释,以这种方式学习它很复杂且令人沮丧。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-07-05
    • 1970-01-01
    • 2016-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多