Jump to content
  • Advertisement
Sign in to follow this  

Opening Files Using a Dialog Box[WIN32]

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

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)
memset( &ofn, 0, sizeof(ofn) );

WCHAR currentDir[MAX_PATH];

static WCHAR filter[] =
L"X files (*.x)\0*.x\0"
L"All Files (*.*)\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";


if (GetOpenFileName( &ofn))
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!