Format description:
Explicit cfiledialog (
Bool bopenfiledialog, // true is enabled, false is saved
Lpctstr lpszdefext = NULL, // default file extension
Lpctstr lpszfilename = NULL, // initial file name in the file dialog box
DWORD dwflags = ofn_hidereadonly | ofn_overwriteprompt, // set the dialog box function
Lpctstr lpszfilter = NULL, // file filtering
Cwnd * pparentwnd = NULL,
DWORD dwsize = 0, // the size ofOpenfilenameStructure. If the value is 0, the system determines the correct size.
Bool bvistastyle = true
);
Description of the number of shards:
-
[In]
Bopenfiledialog
-
The parameter that specifies what type of dialog box to create. Set itTrueTo constructFile OpenDialog Box. Set itFalseTo constructFile SaveDialog box.
-
-
[In]
Lpszdefext
-
The default file name extension. If the user does not include an extension in the filename box, the extension specifiedLpszdefextIs automatically appended to the file name. If this parameter isNull, No extension is appended.
-
-
[In]
Lpszfilename
-
The initial file name that appears in the filename box. IfNull, No initial file name appears.
-
-
[In]
Dwflags
-
A combination of one or more flags that you can use to customize the dialog box. For a description of these flags, see theopenfilename structure in the Windows SDK. If you modifyM_ofn.flagsStructure Member, use a bitwise-or operator in your changes to keep the default behavior intact.
-
-
[In]
Lpszfilter
-
A series of string pairs that specify filters you can apply to the file. if you specify file filters, only files that match filter criteria will appear in the files list. see the remarks section for more information about how to work with file filters.
-
-
[In]
Pparentwnd
-
A pointer to the parent or owner window of the file dialog box.
-
-
[In]
Dwsize
-
The size ofOpenfilenameStructure. this value depends on the operating system version. MFC used this parameter to determine the appropriate kind of dialog box to create (for example, new Windows 2000 dialog boxes Instead of NT4 dialog boxes ). the default size of 0 means that the MFC code will determine the correct dialog box size to use based on the operating system version on which the program is run.
-
-
[In]
Bvistastyle
-
NoteThis parameter is applicable only if you are compiling in Windows Vista.
The parameter that specifies the style of the file Dieter. Set itTrueTo use the new vista Style File dialogs. Otherwise, the old style of dialog boxes will be used. See the remarks section for more information about compiling under Vista.
Remarks
EitherFile OpenOrFile SaveDialog box is constructed, depending on the valueBopenfiledialog.
To enable the user to select multiple files, setOfn_allowmultiselectFlag before you calldomodal.
You must supply your own file name buffer to store the returned list of multiple file names. Do this by replacing
M_ofn.lpstrfileWith a pointer to a buffer you have allocated, after you construct thecfiledialog, but before you call
Domodal. Additionally, you must setM_ofn.nmaxfileWith the number of characters in the buffer pointed to
M_ofn.lpstrfile. If you set the maximum number of files to be selected ton, the necessary buffer size isn *
(_ Max_path + 1) + 1.
Instance 1 open the file:
// Create dialog to open multiple files. cfiledialog DLG (true, _ T ("TXT"), _ T ("*. TXT "), ofn_allowmultiselect); // create buffer for file names. const DWORD buffers = 100; const DWORD filenamemaxlength = max_path + 1; const DWORD buffersize = (numberoffilenames * filenamemaxlength) + 1; tchar * filenamesbuffer = new tchar [buffersize]; // initialize beginning and end of buffer. filenamesbuffer [0] = N Ull; filenamesbuffer [bufferSize-1] = NULL; // attach buffer to openfilename member. DLG. m_ofn.lpstrfile = filenamesbuffer; DLG. m_ofn.nmaxfile = buffersize; // create array for file names. cstring filenamearray [numberoffilenames]; If (DLG. domodal () = idok) {// retrieve file name (s ). position filenamesposition = DLG. getstartposition (); int ICTR = 0; while (filenamesposition! = NULL) {filenamearray [ICTR] = DLG. getnextpathname (filenamesposition); ICTR ++ ;}// release file names buffer. Delete [] filenamesbuffer;
Instance 2 open the file:
Cfiledialog DLG (true ,"*","*. XYZ ", ofn_filemustexist | ofn_hidereadonly | ofn_allowmultiselect," All files (*. XYZ | *. XYZ | "); char szbuffer [1024]; szbuffer [0] = 0; DLG. m_ofn.lpstrfile = szbuffer; DLG. m_ofn.nmaxfile = 1024; If (idok = DLG. domodal () {positionpos = DLG. getstartposition (); cstringfilepath; while (Pos! = NULL) {filepath = DLG. getnextpathname (POS );}}
Instance 3 open the file:
Cstring filepath; char filename [256]; char filter [] = "Geo files (*. GEO) | *. GEO | all files (*. *) | *. * | "; updatedata (true); cfiledialog fdlg (true," BMP ", null, ofn_hidereadonly | ofn_overwriteprompt, szfilter); strcpy (filename, _ T ("file"); If (idok! = Cf. domodal () return; filepath = fdlg. getpathname (); // filepath is the path of the opened file updatedata (false );
Instance 4 open the file and set the dialog box title
Cfiledialog nfiledlg (true, l "XML", l "", ofn_hidereadonly | ofn_overwriteprompt, l "XML file (*. XML) | *. XML | "); nfiledlg. m_pofn-> lpstrtitle = l "Open blank task"; // file dialog box title if (nfiledlg. domodal () = idok) {m_picfolder = l "blank"; m_xmlfolder = l "blank"; cstring szxmlfilepath; cstring szxmlparentpath; cstring nxmlfilename; szxmlfilepath = nfiledlg. getpathname (); // absolute path file name nxmlfilename = nfiledlg. getfilename (); // name of the file without a path szxmlparentpath = szxmlfilepath. left (szxmlfilepath. getlength ()-nxmlfilename. getlength ()-1); // The parent folder where the file is located}
Instance 5 save the file:
Charfilename [256]; cstringtitle, fmtstring; cstringpathname; cstring path_and_filename; updatedata (true); pathname = _ T ("path. XML "); char based_code szfilter [] =" XML files (*. XML) | *. XML | all files (*. *) | *. * | "; cfiledialogfdlg (false," XML ", pathname, ofn_hidereadonly | ofn_overwriteprompt, szfilter); strcpy (filename, _ T (" file name "); If (idok! = Fdlg. domodal () return; path_and_filename = fdlg. getpathname (); // path_and_filename indicates the file storage path updatedata (false );
The preceding section uses cfiledialog to open a file. The following section uses the shbrowseforfolder to open the file:
Onbnclickedbutton1 () {browseinfo Bi; zeromemory (& BI, sizeof (browseinfo); lpmalloc pmalloc; lpitemidlist pidl = shbrowseforfolder (& BI); If (pidl = NULL) return; if (pidl! = NULL) {tchar * Path = new tchar [max_path]; shgetpathfromidlist (pidl, PATH); // MessageBox (null, path, text ("choose"), mb_ OK ); if (succeeded (shgetmalloc (& pmalloc) // when pidl points to an object, it should be released. Previously, {pmalloc-> free (pidl) is ignored ); pmalloc-> release ();} m_path = path; updatedata (false); Delete [] path ;}}