Advertisement Jump to content
Sign in to follow this  

Opening Files Using a Dialog Box[WIN32]

This topic is 4898 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, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!