Help with Windows 'Open' and 'Save As' dialog boxes...

Started by
8 comments, last by Empirical 18 years, 8 months ago
Hey guys, here's an easy CPP Windows question! I don't know much about creating 'Save As' and 'Open' dialog boxes, so I slightly modified the code that came in the 'PlaySound' DirectX tutorial. First, I setup the 'OPENFILENAME' structure like this (pretty much exactly how 'PlaySound' does, except I inserted my own descriptions and extensions):
OPENFILENAME ofn = { sizeof(OPENFILENAME), hDlg, NULL,
			TEXT("Level WIP Files\0*.wip\0All Files\0*.*\0\0"), NULL,
			0, 1, strFileName, MAX_PATH, NULL, 0, strPath,
			TEXT("Open Level File"),
			OFN_FILEMUSTEXIST|OFN_HIDEREADONLY, 0, 0,
			TEXT(".wip"), 0, NULL, NULL };
Then I call 'GetOpenFileName(&ofn)' or 'GetSaveFileName(&ofn)', and it works. Except that the 'Open' and 'Save As' dialog boxes usually default to the directory "My Documents" or something oddball like that... How do I set the default directory in this structure to start in the 'levels' folder? Thanks in advance for the help, I really appreciate it!
"The crows seemed to be calling his name, thought Caw"
Advertisement
You set it with the lpstrInitialDir member of the OPENFILENAME structure.
Thanks for the reply, SiCrane. I tried doing this earlier...

ofn.lpstrInitialDir = "levels/";


but it didn't seem to make a difference. I put this code right after where I created 'ofn'. Is my syntax correct?

Thanks again!
"The crows seemed to be calling his name, thought Caw"
OK, I got it figured out. I was making it harder than it really was...

ofn.lpstrInitialDir = "levels";


Just plain 'levels'... heh! But now I have a different problem. When I exit my game, I write a 'settings.cfg' file (holds video/audio/control settings). After I've opened a level file, the following doesn't write the settings file into the root of the game installation folder anymore:

ofstream outbal("settings.cfg");


It writes 'settings.cfg' into the 'levels' folder! How do I get my settings file to write into the correct folder?

Thanks again... SOMEDAY I'm gonna get this thing figured out! [grin]
"The crows seemed to be calling his name, thought Caw"
Some day you will discover that most Windows API-related questions are best answered at msdn.microsoft.com
Note the OFN_NOCHANGEDIR flag ;)
_______________The essence of balance is detachment. To embrace a cause, to grow fond or spiteful, is to lose one''s balance after which, no action can be trusted. Our burden is not for the dependent of spirit. - Mayar, Third Keeper
As izhbq412 hinted, GetOpenFileName/GetSaveFileName changes the current working directory. When the dialog has closed, the working directory has been set to whatever folder the user saved his file in. This has exactly the effect that you discovered.

You can get around this by using the OFN_NOCHANGEDIR as suggested, but it seems this flag has no effect with GetOpenFileName under win NT4/2K/XP.

You can also get around it by saving the current working directory before opening the dialog, and the restoring it afterwards.
Quote:Original post by crudbreeder
As izhbq412 hinted, GetOpenFileName/GetSaveFileName changes the current working directory. When the dialog has closed, the working directory has been set to whatever folder the user saved his file in. This has exactly the effect that you discovered.

You can get around this by using the OFN_NOCHANGEDIR as suggested, but it seems this flag has no effect with GetOpenFileName under win NT4/2K/XP.

You can also get around it by saving the current working directory before opening the dialog, and the restoring it afterwards.


I just used that flag in a program on XP and it seemed to work fine...
Thanks for the replies, guys! I might have to give that 'OFN_NOCHANGEDIR' flag a try, because I'm now storing my working directory as part of a main game struct (wasted memory). Here's the function that made it possible:
getcwd(MyCharArray, sizeof(MyCharArray));

and the current working directory goes into the character array 'MyCharArray'. When I make a call to my 'settings.cfg' save/load routine, I concatinate the filename to the path stored in MyCharArray. If any of you guys want to use this method, make sure you use this header:
#include	<direct.h>

Thanks again guys, I think this problem is solved. [smile]
"The crows seemed to be calling his name, thought Caw"
Quote:Original post by trevaaar
I just used that flag in a program on XP and it seemed to work fine...


I havn't tested it myself, I was just quoting from the msdn page linked to in izhbq412's post.

"OFN_NOCHANGEDIR
Restores the current directory to its original value if the user changed the directory while searching for files.
Windows NT 4.0/2000/XP: This flag is ineffective for GetOpenFileName."
Quote:Original post by crudbreeder
Quote:Original post by trevaaar
I just used that flag in a program on XP and it seemed to work fine...


I havn't tested it myself, I was just quoting from the msdn page linked to in izhbq412's post.

"OFN_NOCHANGEDIR
Restores the current directory to its original value if the user changed the directory while searching for files.
Windows NT 4.0/2000/XP: This flag is ineffective for GetOpenFileName."


Perhaps GetOpenFileName no longer changes the current directory at all under xp?
So the flag is obsolete

This topic is closed to new replies.

Advertisement