Can anyone please show me how to use GetOpenFileName() ?
Thanks.
Printable View
Can anyone please show me how to use GetOpenFileName() ?
Thanks.
STFWQuote:
Originally posted by Devil Panther
Can anyone please show me how to use GetOpenFileName() ?
Thanks.
EDIT: As Eibro says. Code removed as it's copyrighted.
the example does not work, it doesn't even show the dialog box.
I have a feeling it's your code, and not the examples code. Are you checking the return values of all your calls? If you want another example, check www.sunlightd.com (It's the Windows tutorial, bitmap loading section)
I've placed the code so it will be used when a button is pressed, and changed the hwnd to my handle and everything, but it doesn't show the dialog...
Please wait while I attempt to use my telepathic powers to read the code off of your PC...
Or, if you don't want to wait as long, post some code.
Code:OPENFILENAME ofn;
char filename[256]={0};
ZeroMemory(&ofn, sizeof(OPENFILENAME));
ofn.lStructSize = sizeof(OPENFILENAME);
ofn.hwndOwner = hDlg;
ofn.lpstrFilter = "Bitmap Files (*.bmp)\0*.bmp\0All Files (*.*\0*.*\0\0";
ofn.lpstrFile = filename;
ofn.nMaxFile = 256;
ofn.lpstrTitle = "Browse";
ofn.Flags = OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
GetOpenFileName(&ofn);
From The Forgers Win32 API Tutorial
http://www.winprog.org/tutorial/
- SeanCode:OPENFILENAME ofn;
char szFileName[MAX_PATH] = "";
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(ofn); // SEE NOTE BELOW
ofn.hwndOwner = hwnd;
ofn.lpstrFilter = "Text Files (*.txt)\0*.txt\0All Files (*.*)\0*.*\0";
ofn.lpstrFile = szFileName;
ofn.nMaxFile = MAX_PATH;
ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
ofn.lpstrDefExt = "txt";
if(GetOpenFileName(&ofn))
{
// Do something usefull with the filename stored in szFileName
}
*shrug* works fine for me. The only potential problem I can see is hDlg. Ensure that it's a valid window handle.
A stab in the dark, have you called InitCommonControls() somewhere?
here is the exact code:
hDlg is the handle of that dialog, I'm calling the GetOpenFileName() from...Code:// in the WM_COMMAND
case IDD_SETLOG_BROWSE:
{
OPENFILENAME ofn;
char szFileName[100] = "";
InitCommonControls();
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(ofn); // SEE NOTE BELOW
ofn.hwndOwner = hDlg;
ofn.lpstrFilter = "Text Files (*.txt)\0*.txt\0All Files (*.*)\0*.*\0";
ofn.lpstrFile = szFileName;
ofn.nMaxFile = 100;
ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
ofn.lpstrDefExt = "txt";
if(GetOpenFileName(&ofn))
{
MessageBox (NULL, "Message" , " ", 0);
}
}
break;
And I am calling InitCommonControls().
Just... one more thing, I'm using borland C++ 5.5 compiler. And C, not C++, I don't know, I'm just shooting in the dark as well
http://www.winprog.org/tutorial/
I've checked out their example code for GetOpenFileName()
I downloaded it (app_two) and tried to use FILE->OPEN, but nothing happened... maybe it's the windows?
With the last code you posted, did it put up the messagge box, i.e. is the call to GetOpenFileName() returning TRUE?
it returns false... and as I said, it doesn't even show the dialog...
>>> it returns false
Ah, so call CommDlgExtendedError() and see why.
Well I did some error checking and found out that CommDlgExtendedError() returns CDERR_STRUCTSIZE, this means that:
Quote:
The lStructSize member of the initialization structure for the corresponding common dialog box is invalid.
ofn.lStructSize = sizeof(ofn);
What's wrong with that?
Try changing...
... to...Code:ofn.lStructSize = sizeof(ofn);
... and see if that works. If it doesn't work, tell me what OS version you are using.Code:ofn.lStructSize = sizeof(OPENFILENAME);
>>I'm using borland C++ 5.5 compiler<<
If you are using bcc5.5 cmd line tools with win9x/nt and have not set up your configuration files correctly then the kind of problem you are experiencing will typically occur.
Read my post in this faq for further information.
I never had any problems before that, all controls and dialogs worked...Quote:
If you are using bcc5.5 cmd line tools with win9x/nt and have not set up your configuration files correctly then the kind of problem you are experiencing will typically occur.
And, the CDERR_STRUCTSIZE is still there, after I changed the size... So, I'm running win98 second edition
Try changing...
... to...Code:ofn.lStructSize = sizeof(OPENFILENAME);
... and see if that works.Code:ofn.lStructSize = sizeof(OPENFILENAME_SIZE_VERSION_400);
Ken is thinking along the same lines as me, a number of structures have been changed by MS, particulaly after 0x0400. This was one of them.
Nothing... still the same error.
adrianxw, you know I think I'm up to something... can you download http://www.winprog.org/tutorial/files/source.zip and run app_two.exe (it's located inside the zip), then try to FILE->OPEN inside app_two.exe
You see, because app_two uses GetOpenFileName(), but the FILE->OPEN doesn't open me anything, no dialog not nothing.
Check it out, and let me know if that works for you.
Thanks
If you use bcc5.5 and you compile and run your program which contains common dialogs on win98 (generally win9x/nt) and you have not defined:Quote:
Originally posted by Devil Panther
I never had any problems before that, all controls and dialogs worked...
in your bcc32.cfg file then your common dialog will not appear.Quote:
-DWINVER=0x0400
-D_WIN32_WINNT=0x0400
Do you have those macros defined in your bcc32.cfg file? If not, put them in and recompile - your common dialog (file) will show, provided there are no other errors in your code.
If I try to run the .exe on my Win95 machine, it behaves as you suggest, however, when I compile it, (after fixing a couple of things that MSVC grouched about), it worked as expected. On my WinXP system the .exe runs.
I am quite sure your problem is with the changed definition of the OPENFILENAME structure. Try what Ken is saying, you somehow need to make sure that your compiler knows which version of the structure you need.
I often find myself putting a #define for _WIN32_WINNT in programs.
IT WORKS IT REALLY WORKS... Ken you were right.
gee guys... I don't know what say...
Thanks for all of your help, it's good to know that when you have a problem, there is a place like this.
Thanks a million.