Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Windows open file dialog 1

Status
Not open for further replies.

Weber1

Programmer
Sep 9, 2000
20
US
I am looking for an easy to way to bring up the standard Windows open file dialog. I've looked around for an obvious way to do it, but so far no luck; at this point I'd settle just with knowing which files/libs to include and to look at, but this seems like something that should be pretty common.

I don't really want to mess around much with the attributes of the dialog or anything fancy (maybe I'll try that later if I can get it working first), I just want to bring it up to a path I set and to use it like normal; hopefully if a file is opened, I'd get a file pointer or something useful like that.

Any help would be greatly appreciated.
 
OPENFILENAME opf;
opf.hwndOwner = 0;
opf.lpstrFilter = "All files\0\*.*\0\0";
opf.lpstrCustomFilter = 0;
opf.nMaxCustFilter = 0L;
opf.nFilterIndex = 1L;
opf.lpstrFile = 0;
opf.nMaxFile = 255;
opf.lpstrFileTitle = 0;
opf.nMaxFileTitle=50;
opf.lpstrInitialDir = 0;
opf.lpstrTitle = "save";
opf.nFileOffset = 0;
opf.nFileExtension = 2;
opf.lpstrDefExt = "*.*";
opf.lpfnHook = NULL;
opf.lCustData = 0;
opf.Flags = OFN_PATHMUSTEXIST | OFN_OVERWRITEPROMPT;
opf.lStructSize = sizeof(OPENFILENAMEW);
GetOpenFileNameW(&opf);

you can wrap this code in a class and reuse it :)

Ion Filipski
1c.bmp
 
Hey, thanks!

It wouldn't quite compile for me, but I did some digging and changed OPENFILENAMEW to OPENFILENAMEA and GetOpenFileNameW to GetOpenFileNameA (write versus append?) I couldn't figure out from the COMMDLG.h file why it wouldn't work with the "W", but I'll keep playing with it. This is plenty to get me started.

Thanks again!

 
put GetOpenFileName instead of GetOpenFileNameW
and OPENFILENAME instead of OPENFILENAMEW

Ion Filipski
1c.bmp
 
Ok, another question:
I have modified the code a bit, wrapped it into a class operation with char* inputs Filter and Path and it returns returnstring (char*). It appears, however, that even though I can open the open dialog successfully and pick a file, lpstrFile is not being set (it remains NULL):

------------------------------------------
char* returnstring = NULL;

OPENFILENAME opf;
opf.hwndOwner = 0;
opf.lpstrFilter = Filter;
opf.lpstrCustomFilter = 0;
opf.nMaxCustFilter = 0L;
opf.nFilterIndex = 1L;
opf.lpstrFile = 0;
opf.nMaxFile = 256;
opf.lpstrFileTitle = 0;
opf.nMaxFileTitle=50;
opf.lpstrInitialDir = Path;
opf.lpstrTitle = "Open File";
opf.nFileOffset = 0;
opf.nFileExtension = 0;
opf.lpstrDefExt = "*.*";
opf.lpfnHook = NULL;
opf.lCustData = 0;
opf.Flags = (OFN_PATHMUSTEXIST | OFN_OVERWRITEPROMPT) & ~OFN_ALLOWMULTISELECT;
opf.lStructSize = sizeof(OPENFILENAME);

if(GetOpenFileName(&opf))
{
returnstring = opf.lpstrFile;
}

return returnstring;

----------------------------------
I do hit the line where returnstring is set, but both opf.lpstrFile and returnstring remain null. Any ideas what I'm doing wrong?



 
Ok, I think I answered my own question. I thought that the GetOpenFile operation would create the buffer I needed to hold the filename string, but I need to do that myself. Here's the finished (for now) operation, for anyone interested:

C_mystring* C_file_handler::LoadFileDialog(char* Path, char* Filter)
{
char Filestring[256];
C_mystring* returnstring = NULL;

OPENFILENAME opf;
opf.hwndOwner = 0;
opf.lpstrFilter = Filter;
opf.lpstrCustomFilter = 0;
opf.nMaxCustFilter = 0L;
opf.nFilterIndex = 1L;
opf.lpstrFile = Filestring;
opf.lpstrFile[0] = '\0';
opf.nMaxFile = 256;
opf.lpstrFileTitle = 0;
opf.nMaxFileTitle=50;
opf.lpstrInitialDir = Path;
opf.lpstrTitle = "Open File";
opf.nFileOffset = 0;
opf.nFileExtension = 0;
opf.lpstrDefExt = "*.*";
opf.lpfnHook = NULL;
opf.lCustData = 0;
opf.Flags = (OFN_PATHMUSTEXIST | OFN_OVERWRITEPROMPT) & ~OFN_ALLOWMULTISELECT;
opf.lStructSize = sizeof(OPENFILENAME);

if(GetOpenFileName(&opf))
{
returnstring = new C_mystring(opf.lpstrFile);
}

return returnstring;
}


C_mystring is my own string class that dynamically allocates memory (using my memory manager) for the string; I believe substituting a call to "new char[stringsize]" and return the pointer to it would work, as well (so long as the user makes sure that the memory allocated gets handled.)

These were helpful links, as well:

Thanks for the help, IonFilipski!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top