Reputation:
I just want to ask is there a simple way in C++ to make dialog boxes (openfiledialog, savefiledialog) to be written like this:
' VB6 Example
'Setting up the "open file" dialog box.
openDiag.DialogTitle = "Select a file to open"
openDiag.Filter = "Exe files | *.exe" 'Ensures only EXE files can be selected.
openDiag.ShowOpen 'Opens up the dialog
'Takes the filename that was selected in the dialog and stores it in your OpenPath var and
'also displays it in the textbox
openPath = openDiag.FileName
txtfilepath.Text = openPath
I'm assuming that I would need to use Classes, but I don't really know how to write this kind of code.
Any help will be much appreciated !
@@@FINAL EDIT@@@
It Seems that this code doesn't compile when pasted in WIN32 Project in VS 2010.
Gives me these errors:
error C2065: 'ofn' : undeclared identifier c:\documents and settings\User\my documents\visual studio 2010\projects\program\program\program.cpp
error C2065: 'ofn' : undeclared identifier c:\documents and settings\User\my documents\visual studio 2010\projects\program\program\program.cpp
error C2065: 'ofn' : undeclared identifier c:\documents and settings\User\my documents\visual studio 2010\projects\program\program\program.cpp
error C2065: 'ofn' : undeclared identifier c:\documents and settings\User\my documents\visual studio 2010\projects\program\program\program.cpp
error C2065: 'ofn' : undeclared identifier c:\documents and settings\User\my documents\visual studio 2010\projects\program\program\program.cpp
error C2065: 'ofn' : undeclared identifier c:\documents and settings\User\my documents\visual studio 2010\projects\program\program\program.cpp
error C2065: 'ofn' : undeclared identifier c:\documents and settings\User\my documents\visual studio 2010\projects\program\program\program.cpp
error C2065: 'ofn' : undeclared identifier c:\documents and settings\User\my documents\visual studio 2010\projects\program\program\program.cpp
error C2065: 'ofn' : undeclared identifier c:\documents and settings\User\my documents\visual studio 2010\projects\program\program\program.cpp
error C2065: 'ofn' : undeclared identifier c:\documents and settings\User\my documents\visual studio 2010\projects\program\program\program.cpp
error C2065: 'ofn' : undeclared identifier c:\documents and settings\User\my documents\visual studio 2010\projects\program\program\program.cpp
error C2065: 'ofn' : undeclared identifier c:\documents and settings\User\my documents\visual studio 2010\projects\program\program\program.cpp
error C2065: 'OFN_EXPLORER' : undeclared identifier c:\documents and settings\User\my documents\visual studio 2010\projects\program\program\program.cpp
error C2065: 'OFN_FILEMUSTEXIST' : undeclared identifier c:\documents and settings\User\my documents\visual studio 2010\projects\program\program\program.cpp
error C2065: 'OFN_HIDEREADONLY' : undeclared identifier c:\documents and settings\User\my documents\visual studio 2010\projects\program\program\program.cpp
error C2065: 'OPENFILENAME' : undeclared identifier c:\documents and settings\User\my documents\visual studio 2010\projects\program\program\program.cpp
error C2065: 'OPENFILENAME' : undeclared identifier c:\documents and settings\User\my documents\visual studio 2010\projects\program\program\program.cpp
error C2070: ''unknown-type'': illegal sizeof operand c:\documents and settings\User\my documents\visual studio 2010\projects\program\program\program.cpp
error C2070: ''unknown-type'': illegal sizeof operand c:\documents and settings\User\my documents\visual studio 2010\projects\program\program\program.cpp
error C2146: syntax error : missing ';' before identifier 'ofn' c:\documents and settings\User\my documents\visual studio 2010\projects\program\program\program.cpp
error C2228: left of '.Flags' must have class/struct/union c:\documents and settings\User\my documents\visual studio 2010\projects\program\program\program.cpp
error C2228: left of '.hwndOwner' must have class/struct/union c:\documents and settings\User\my documents\visual studio 2010\projects\program\program\program.cpp
error C2228: left of '.lpstrDefExt' must have class/struct/union c:\documents and settings\User\my documents\visual studio 2010\projects\program\program\program.cpp
error C2228: left of '.lpstrFile' must have class/struct/union c:\documents and settings\User\my documents\visual studio 2010\projects\program\program\program.cpp
error C2228: left of '.lpstrFilter' must have class/struct/union c:\documents and settings\User\my documents\visual studio 2010\projects\program\program\program.cpp
error C2228: left of '.lpstrTitle' must have class/struct/union c:\documents and settings\User\my documents\visual studio 2010\projects\program\program\program.cpp
error C2228: left of '.lStructSize' must have class/struct/union c:\documents and settings\User\my documents\visual studio 2010\projects\program\program\program.cpp
error C2228: left of '.nMaxFile' must have class/struct/union c:\documents and settings\User\my documents\visual studio 2010\projects\program\program\program.cpp
IntelliSense: identifier "OFN_EXPLORER" is undefined c:\documents and settings\User\my documents\visual studio 2010\projects\program\program\program.cpp
IntelliSense: identifier "OFN_FILEMUSTEXIST" is undefined c:\documents and settings\User\my documents\visual studio 2010\projects\program\program\program.cpp
IntelliSense: identifier "OFN_HIDEREADONLY" is undefined c:\documents and settings\User\my documents\visual studio 2010\projects\program\program\program.cpp
IntelliSense: identifier "OPENFILENAME" is undefined c:\documents and settings\User\my documents\visual studio 2010\projects\program\program\program.cpp
IntelliSense: identifier "OPENFILENAME" is undefined c:\documents and settings\User\my documents\visual studio 2010\projects\program\program\program.cpp
IntelliSense: too many arguments in function call c:\documents and settings\User\my documents\visual studio 2010\projects\program\program\program.cpp
Any suggestions on how to fix this error?
EDIT:
Found it myself...
Needed to include
#include <Commdlg.h>
Upvotes: 3
Views: 2644
Reputation: 244981
In C (or C++), you'll create an instance of the OPENFILENAME
structure, and set its members. This is very much the same as what you've shown in the above sample VB 6 code.
The linked documentation contains information on what all the various members of that structure mean and how you should fill them in, but most of them match up with what you find in VB 6.
For example:
OPENFILENAME ofn;
ofn.lpstrTitle = TEXT("Select a file to open");
ofn.lpstrFilter = TEXT("EXE files\0*.exe\0");
/* etc... */
The magic comes when you have set all the properties and are ready to show the dialog. Unlike in VB 6, where the data structure exposes a function that shows the dialog (ShowOpen
), in C/Win32, you need to call a different function to do this.
The function you're interested in is GetOpenFileName
, and it takes a single argument—a pointer to your OPENFILENAME
structure. The members of that structure are used to initialize the dialog box, and the function also fills in members of the structure with information about the file that the user selected. The function's return value indicates whether the user specified a file name and clicked the "OK" button (non-zero), or whether the user clicked "Cancel" or an error occurred (zero; FALSE
).
Complete sample code is here, but the simplified version is this:
TCHAR szFileName[MAX_PATH]; /* string buffer to hold name of the selected file */
OPENFILENAME ofn = {0};
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = hWnd; /* a handle to your window that will own the dialog */
ofn.lpstrFile = szFileName;
ofn.nMaxFile = MAX_PATH;
ofn.lpstrTitle = TEXT("Select a file to open");
ofn.lpstrFilter = TEXT("EXE files\0*.exe\0");
ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
/* etc... */
/* Show the dialog. */
if (!GetOpenFileName(&ofn))
{
/* Either the user clicked Cancel, or an error occurred.
* You need to handle it here... */
}
else
{
/* The user selected a file and clicked OK.
* The name and path to the file is contained in the szFileName buffer. */
MessageBox(NULL, szFileName, TEXT("You selected the file..."), MB_OK);
}
Yes, some things about this are different than VB 6. For example, C does not have a built-in string type, which makes things a bit more complicated to code. If you're confused by the above sample code, I very much advise that you learn the C language before trying to learn the Win32 API. Things will go much more smoothly and you won't be nearly as lost.
Upvotes: 2
Reputation: 22366
C++ does not contain any GUI libraries, so having a dialog box does not make any sense. Please refer to the docs of whatever GUI libraries you are using.
Upvotes: 0
Reputation: 994539
You need not use classes, in fact you don't even need to use C++. Win32 provides GetOpenFileName
which does all the heavy lifting for you.
Upvotes: 0