I disagree. Start with GUI programs.
Here's how to use the common "open file" dialog without having to play with COM interfaces (just straight-up Windows API):
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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
|
#include <iostream>
#ifndef NOMINMAX
#define NOMINMAX
#endif
#include <windows.h>
int main()
{
char filename[ MAX_PATH ];
OPENFILENAME ofn;
ZeroMemory( &filename, sizeof( filename ) );
ZeroMemory( &ofn, sizeof( ofn ) );
ofn.lStructSize = sizeof( ofn );
ofn.hwndOwner = NULL; // If you have a window to center over, put its HANDLE here
ofn.lpstrFilter = "Text Files\0*.txt\0Any File\0*.*\0";
ofn.lpstrFile = filename;
ofn.nMaxFile = MAX_PATH;
ofn.lpstrTitle = "Select a File, yo!";
ofn.Flags = OFN_DONTADDTORECENT | OFN_FILEMUSTEXIST;
if (GetOpenFileNameA( &ofn ))
{
std::cout << "You chose the file \"" << filename << "\"\n";
}
else
{
// All this stuff below is to tell you exactly how you messed up above.
// Once you've got that fixed, you can often (not always!) reduce it to a 'user cancelled' assumption.
switch (CommDlgExtendedError())
{
case CDERR_DIALOGFAILURE : std::cout << "CDERR_DIALOGFAILURE\n"; break;
case CDERR_FINDRESFAILURE : std::cout << "CDERR_FINDRESFAILURE\n"; break;
case CDERR_INITIALIZATION : std::cout << "CDERR_INITIALIZATION\n"; break;
case CDERR_LOADRESFAILURE : std::cout << "CDERR_LOADRESFAILURE\n"; break;
case CDERR_LOADSTRFAILURE : std::cout << "CDERR_LOADSTRFAILURE\n"; break;
case CDERR_LOCKRESFAILURE : std::cout << "CDERR_LOCKRESFAILURE\n"; break;
case CDERR_MEMALLOCFAILURE : std::cout << "CDERR_MEMALLOCFAILURE\n"; break;
case CDERR_MEMLOCKFAILURE : std::cout << "CDERR_MEMLOCKFAILURE\n"; break;
case CDERR_NOHINSTANCE : std::cout << "CDERR_NOHINSTANCE\n"; break;
case CDERR_NOHOOK : std::cout << "CDERR_NOHOOK\n"; break;
case CDERR_NOTEMPLATE : std::cout << "CDERR_NOTEMPLATE\n"; break;
case CDERR_STRUCTSIZE : std::cout << "CDERR_STRUCTSIZE\n"; break;
case FNERR_BUFFERTOOSMALL : std::cout << "FNERR_BUFFERTOOSMALL\n"; break;
case FNERR_INVALIDFILENAME : std::cout << "FNERR_INVALIDFILENAME\n"; break;
case FNERR_SUBCLASSFAILURE : std::cout << "FNERR_SUBCLASSFAILURE\n"; break;
default : std::cout << "You cancelled.\n";
}
}
}
|
Here's the documentation for the function itself:
http://www.google.com/search?btnI=1&q=msdn+GetOpenFileName+function
There are all kinds of options you can play with:
http://www.google.com/search?btnI=1&q=msdn+OPENFILENAME+structure
Make sure to read the docs carefully -- some things require some special care, such as OFN_ALLOWMULTISELECT.
Also, notice that my example uses
char
as the character type, but it is structured such that you can easily change it to use
wchar_t
. (Change the element type of
filename and change the 'A' on the end of the function to a 'W' and fix the string literals to be
L"wide literals"
.)
Oh, almost forgot -- you need to link with comdlg32.lib. (You can read what to include and what library to link with at the bottom of the GetOpenFileName() page.)
Hope this helps.