Jump to content
  • Advertisement
Sign in to follow this  
muimui1911

Opening Files Using a Dialog Box[WIN32]

This topic is 4772 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

Advertisement
You need to call the common dialog GetOpenFilename or GetSaveFilename. You fill in a structure (OPENFILENAME) and then call the function. The structure allows you to specify a filter etc. On return from the dialog the filename is filled into the structure.

Share this post


Link to post
Share on other sites
And here is an example:


// Use the common file dialog to allow the user to browse for a x file
bool GetFilename(WCHAR *filename,size_t filenameSize)
{
OPENFILENAME ofn;
memset( &ofn, 0, sizeof(ofn) );

WCHAR currentDir[MAX_PATH];
GetCurrentDirectory(MAX_PATH,currentDir);

static WCHAR filter[] =
L"X files (*.x)\0*.x\0"
L"All Files (*.*)\0*.*\0";

WCHAR file[MAX_PATH];
file[0]=0;

WCHAR fileTitle[MAX_PATH];

ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = DXUTGetHWND();
ofn.hInstance = NULL;;
ofn.lpstrFilter = filter;
ofn.lpstrCustomFilter = NULL;
ofn.nMaxCustFilter = 0L;
ofn.nFilterIndex = 1L;
ofn.lpstrFile = file;
ofn.nMaxFile = sizeof(file);
ofn.lpstrFileTitle = fileTitle;
ofn.nMaxFileTitle = sizeof(fileTitle);
ofn.lpstrInitialDir = currentDir;
ofn.nFileOffset = 0;
ofn.nFileExtension = 0;
ofn.lpstrDefExt = L"*.x";
ofn.lCustData = 0;
ofn.lpstrTitle = L"Select .x file";

ofn.Flags = OFN_PATHMUSTEXIST | OFN_HIDEREADONLY | OFN_NOCHANGEDIR | OFN_EXPLORER;

if (GetOpenFileName( &ofn))
{
StringCchCopy(filename,filenameSize,ofn.lpstrFile);
return true;
}

return false;
}




Perhaps the only confusion in this code is that I am using Unicode hence the L and use of WCHAR. Of course you can replace all that based on what character set you are compiling under.

Hope that helps.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!