Include header file: # include <shlobj. h>
1. The common method to open a file dialog box is to use the system cfiledialog. Here we will introduce another method: Use the structure of openfilename and the getopenfilename () function to implement Single-choice files or multiple-choice files. The Code is as follows:
Tchar szbuffer [max_path] = {0}; openfilename ofn = {0}; ofn. lstructsize = sizeof (ofn); ofn. hwndowner = m_hwnd; ofn. lpstrfilter = _ T ("EXE file (*. EXE) \ 0 *. exe \ 0 all files (*. *) \ 0 *. * \ 0 "); // specifies the file suffix ofn. lpstrinitialdir = _ T ("D: \ Program Files"); // default file path ofn. lpstrfile = szbuffer; // buffer for storing files ofn. nmaxfile = sizeof (szbuffer)/sizeof (* szbuffer); ofn. nfilterindex = 0; ofn. flags = ofn_pathmustexist | ofn_filemustexist | ofn_explorer; // Add ofn_allowmultiselectbool bsel = getopenfilename (& ofn) to multiple objects );
In this way, you can open the select file dialog box. You can select the desired file. Szbuffer is the path of the selected file to store.
2. How to open the folder dialog box:
Tchar szbuffer [max_path] = {0}; // select the full path of the folder browseinfo Bi; zeromemory (& BI, sizeof (browseinfo); bi. hwndowner = NULL; bi. pszdisplayname = szbuffer; bi. lpsztitle = _ T ("select the folder directory from below:"); bi. ulflags = bif_returnfsancestors; lpitemidlist IDL = shbrowseforfolder (& BI); If (null = IDL) {return;} shgetpathfromidlist (IDL, szbuffer );
The above code opens a select folder dialog box.
3. Call the select file or folder dialog box:
Tchar szbuffer [max_path] = {0}; browseinfo Bi; zeromemory (& BI, sizeof (browseinfo); bi. hwndowner = NULL; bi. pszdisplayname = szbuffer; bi. lpsztitle = _ T ("select a file or folder from the following:"); bi. ulflags = bif_browseincludefiles; lpitemidlist IDL = shbrowseforfolder (& BI); If (null = IDL) {return;} shgetpathfromidlist (IDL, szbuffer );
Address: http://3457302.blog.51cto.com/3447302/921631