简体   繁体   English

如何在 C++ 中打开文件浏览器对话框?

[英]How do you open a file explorer dialogue in C++?

I'm developing a desktop application with ImGui in C++, and I need the user to be able to search for a file on their machine with a file explorer.我正在 C++ 中使用 ImGui 开发桌面应用程序,我需要用户能够使用文件资源管理器在他们的机器上搜索文件。 It was extremely easy to do with Python tkinter, but how am I able to do this with C++?使用 Python tkinter 非常容易,但是我如何使用 C++ 做到这一点?

You could use NativeFileDialog its quite easy to implement, they have an example on they're GitHub too!你可以使用NativeFileDialog它很容易实现,他们也有一个例子,他们是 GitHub!

Native Windows Solution: This will open the "Windows File Selection" prompt, I included it in a custom function that will also store the result and file name to a string variable.原生 Windows 解决方案:这将打开“Windows 文件选择”提示,我将其包含在自定义 function 中,该 function 还将结果和文件名存储到字符串变量中。

I use a variation of the include method for my custom front end launcher.我为我的自定义前端启动器使用了 include 方法的变体。 Hope you make some use of it, even tho you asked this question 9 months ago希望你能利用它,即使你在 9 个月前问过这个问题

#include <Windows.h>
#include <string>
#include <shobjidl.h> 

std::string sSelectedFile;
std::string sFilePath;
bool openFile()
{
    //  CREATE FILE OBJECT INSTANCE
    HRESULT f_SysHr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);
    if (FAILED(f_SysHr))
        return FALSE;

    // CREATE FileOpenDialog OBJECT
    IFileOpenDialog* f_FileSystem;
    f_SysHr = CoCreateInstance(CLSID_FileOpenDialog, NULL, CLSCTX_ALL, IID_IFileOpenDialog, reinterpret_cast<void**>(&f_FileSystem));
    if (FAILED(f_SysHr)) {
        CoUninitialize();
        return FALSE;
    }

    //  SHOW OPEN FILE DIALOG WINDOW
    f_SysHr = f_FileSystem->Show(NULL);
    if (FAILED(f_SysHr)) {
        f_FileSystem->Release();
        CoUninitialize();
        return FALSE;
    }

    //  RETRIEVE FILE NAME FROM THE SELECTED ITEM
    IShellItem* f_Files;
    f_SysHr = f_FileSystem->GetResult(&f_Files);
    if (FAILED(f_SysHr)) {
        f_FileSystem->Release();
        CoUninitialize();
        return FALSE;
    }

    //  STORE AND CONVERT THE FILE NAME
    PWSTR f_Path;
    f_SysHr = f_Files->GetDisplayName(SIGDN_FILESYSPATH, &f_Path);
    if (FAILED(f_SysHr)) {
        f_Files->Release();
        f_FileSystem->Release();
        CoUninitialize();
        return FALSE;
    }

    //  FORMAT AND STORE THE FILE PATH
    std::wstring path(f_Path);
    std::string c(path.begin(), path.end());
    sFilePath = c;

    //  FORMAT STRING FOR EXECUTABLE NAME
    const size_t slash = sFilePath.find_last_of("/\\");
    sSelectedFile = sFilePath.substr(slash + 1);

    //  SUCCESS, CLEAN UP
    CoTaskMemFree(f_Path);
    f_Files->Release();
    f_FileSystem->Release();
    CoUninitialize();
    return TRUE;
}

bool result = FALSE;
int main()
{
    result = openFile();
    switch (result) {
        case(TRUE): {
            printf("SELECTED FILE: %s\nFILE PATH: %s\n\n", sSelectedFile.c_str(), sFilePath.c_str());
            system("pause");
        }
        case(FALSE): {
            printf("ENCOUNTERED AN ERROR: (%d)\n", GetLastError());
            system("pause");
        }
    }
    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM