GetOpenFileName () interferes with SFML
I am creating 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");
But then when I try to import something via SMFL it says "Error: Can't open file" .:
sf::Texture _cursor;
if (!_cursor.loadFromFile("Resources/Metal_Norm.png"))
errorHandle("-Cursor Texture Couldn't Load");
Not sure why this error occurs, if anyone has possible answers, I would appreciate it.
source to share
GetOpenFileName
changes the current directory when navigated in a browser.
There is a flag that you can set OFN_NOCHANGEDIR
that should have prevented this, but I noticed that the MSDN docs were updated at some point to say that this is not. Don't work with GetOpenFileName
.
You can try this, but if it's true it won't work, the solution should be to save the current directory (use GetCurrentDirectory
) before calling GetOpenFileName
and then restore it with SetCurrentDirectory
.
source to share
What I did to fix this issue with the help of Jonathan Potter:
1.) Saved the current directory of the SFML application.
LPCWSTR mainDirectory = GetCurrentD();
2.) I did something with the GetOpenFileName () function.
Tilemap t(file.GetWindowWidth(), file.GetWindowHeight(), file.GetTileWidth(), file.GetTileHeight(), file.GetScale(), file.GetTop(), OpenFile());
3.) Then I restored the directory back to the original original directory that I saved.
if (!SetCurrentDirectory(mainDirectory))
errorHandle(L"Didn't Set Directory");
source to share