Selecting files

How do you make the window that let you select files to open up in C++.

I'm not sure what the prober name for it is, but it loads your files and let you select them, hopefully someone will know what I mean
Assuming you're using WinAPI (which I'm going to assume because this is the Windows forum), you probably are asking about OPENFILENAME / GetOpenFileName:

Note that like all versions of WinAPI structs/functions (which involves strings), they come in 3 flavors: char, wchar_t, and TCHAR:

http://msdn.microsoft.com/en-us/library/windows/desktop/ms646839%28v=vs.85%29.aspx
http://msdn.microsoft.com/en-us/library/windows/desktop/ms646927%28v=vs.85%29.aspx


Example using the 'char' version:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
char buffer[MAX_PATH] = "";
OPENFILENAMEA ofn = {0};  // note:  OPENFILENAMEA, not OPENFILENAME
  // the 'A' at the end specifies we want the 'char' version and not the 'TCHAR' version
  // if you want the 'wchar_t' version, you want to use OPENFILENAMEW instead

ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = your_hwnd; // not entirely necessary if you don't have a window
ofn.lpstrFilter = "TextFiles\0*.txt\0All Files\0*.*\0";
ofn.nFilterIndex = 1; // for some reason this is 1-based not zero-based.  grrrrr

ofn.Flags = OFN_FILEMUSTEXIST;  // only allow the user to open files that actually exist

// the most important bits:
ofn.lpstrFile = buffer;
ofn.nMaxFile = MAX_PATH;  // size of our 'buffer' buffer


// Now that we've prepped the struct, actually open the dialog:
//  the below function call actually opens the "File Open" dialog box, and returns
//  once the user exits out of it
if( !GetOpenFileNameA( &ofn ) ) // <- again note the 'A' at the end, signifying the 'char' version
{
  // code reaches here if the user hit 'Cancel'
}
else
{
  // code reaches here if the user hit 'OK'.  The full path of the file
  //  they selected is now stored in 'buffer'
}
Last edited on
hi Disch.... well explained.... :)
When i started out I had lots of issues because I forgot to include the Comdlg32.lib library (if you are using mingw then it will be libcomdlg32.a)

Make sure you link this to your program. :)
It is supposed to first read MSDN documentation for every windows functions, it says which library must be used.
Topic archived. No new replies allowed.