Sign in to follow this  

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

This topic is 4513 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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!

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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."

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

This topic is 4513 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this