c++ - GetOpenFileName() 正在干扰 SFML

标签 c++ windows winapi visual-c++ sfml

我创建一个 OPENFILENAME:

OPENFILENAME ofn;   
char szFile[260];      
HWND hwnd = NULL;             

// Initialize OPENFILENAME
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = hwnd;
ofn.lpstrFile = (LPWSTR)szFile;
ofn.lpstrFile[0] = '\0';
ofn.nMaxFile = sizeof(szFile);
ofn.lpstrFilter = L"PNG Files\0*.PNG*\0";
ofn.nFilterIndex = 1;
ofn.lpstrFileTitle = NULL;
ofn.nMaxFileTitle = 0;
ofn.lpstrInitialDir = NULL;
ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;

std::string input;
if (GetOpenFileName(&ofn))
{
    input = CW2A(ofn.lpstrFile);
    std::cout << input << std::endl;
}
else
    errorHandle("Open Dialog Problem");

但是当我尝试通过 SMFL 导入某些内容时,它显示“错误:无法打开文件。”:

sf::Texture _cursor;
    if (!_cursor.loadFromFile("Resources/Metal_Norm.png"))
        errorHandle("-Cursor Texture Couldn't Load");

不确定为什么会出现此错误,如果有人有任何可能的答案,我将不胜感激。

最佳答案

GetOpenFileName changes the current directory当您在浏览器中导航时。

您可以设置一个标志,OFN_NOCHANGEDIR,它应该可以防止这种情况发生,但我注意到 MSDN docs在某些时候已更新为它不适用于 GetOpenFileName

您可以尝试这样做,但如果确实如此,则它不起作用,解决方案是在调用 GetOpenFileName 之前保存当前目录(使用 GetCurrentDirectory),然后恢复之后使用 SetCurrentDirectory

关于c++ - GetOpenFileName() 正在干扰 SFML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30951203/

相关文章:

c++ - thrust::complex with thrust reduce 无法编译

c++ - 如何初始化具有三个 channel 的垫子

node.js - pm2 - 如果没有启动如何启动,如果启动如何杀死并启动

go - Windows API 错误 "The handle is invalid"

C++ 不在任务栏中显示使用 CreateWindow 创建的窗口

c - 在 Windows 10 上使用 DrawThemeBackground 绘制的部分不正确

c++ - 抛出异常时不调用 move 构造函数

c++ - 是否可以预测 srand(time(0)) 的随机数?

windows - 是否可以在 Windows 中创建全屏颜色叠加效果?

python-3.x - "PythonAccumulatorV2 does not exist"- 在 Jupyter Notebook 中运行 SparkContext() 时