-
OpenFile Dialog Box
I am trying to write an app that will allow the user to open up a file and load it into my program. The file that will be loaded will probably be just a standard .txt file.
I would like to use the Windows standard OpenFile Dialog box, but I am having trouble getting the box to open.
I create a OPENFILENAME testfile, and then pass that to the function GetOpenFileName(&testfile); The window is never displayed, and I am out of ideas. If somebody could please help me that would be great. Thanks in advance.
- Brice
-
Have you initialised the members of the OPENFILENAME struct?
-
Here is one way to do it.
Code:
//Common control dialog
CString csFile;
CString name=("default") //Default text file
CString path=("C:\\windows\\desktop\\"+name); //Default directory
//The actual meat of the code
//Use TRUE to open FALSE to save
CFileDialog theFileDialog(TRUE,"TXT",path,OFN_HIDEREADONLY|OFN_FILEMUSTEXIST,"txt files(*.txt)|*.txt||");
if(theFileDialog.DoModal()==IDOK)
{
csFile= theFileDialog.GetPathName();
ifstream saveFile(csFile);
}
-
I have tried to initialize the struct, but I am not sure if I have everything in there that I need. Here is my struct
test.hwndOwner = NULL;
test.lpstrFilter = "All Files (*.*)\0*.*\0";
test.lpstrCustomFilter = NULL;
test.nMaxCustFilter = NULL;
test.nFilterIndex = 1;
test.nMaxFile = 256;
test.lpstrFileTitle = NULL;
test.nMaxFileTitle = NULL;
test.lpstrInitialDir = NULL;
test.lpstrTitle = NULL;
test.Flags = OFN_LONGNAMES;
after that I call GetOpenFileName(&test) and I never see my dialog box. Did I forget to initialize something??
- Brice
-
You have to initialise the lStructSize member with the size of the struct -
test.lStructSize=sizeof(OPENFILENAME);
-
Thanks zen!! I didnt realized that it needed that sizeof(struct) it works now. Would I do the same thing for a Save As dialog box? I am assuming so
- Brice