1. Open and save the dialog box
The file dialog box is a common dialog box (In addition, there are color, search, search replacement, Font, print, and other dialog boxes ).
With the help of the MFC ready-made class cfiledialog, you can easily operate the file dialog box.
Cfiledialog DLG (true, _ T ("TXT"), _ T ("B .txt"), ofn_filemustexist | ofn_hidereadonly, _ T ("text | *. TXT | all files | * |"
));
If (DLG. domodal () = idok ){
// DLG. getpathname ();
}
// Open the file dialog box. In the displayed dialog box, select the path to save the file [4/13/2011 shulei].
Cstring Foldername;
Cfiledialog
DLG (false, _ T ("xls"), _ T (""), ofn_overwriteprompt | ofn_hidereadonly, _ T ("Excel file | *. XLS | all files | * | "));
DLG. m_ofn.lpstrtitle = _ T ("save data as an Excel file ");
If (DLG. domodal () = idok ){
Foldername = DLG. getpathname ();
}
The first parameter is true: open file dialog box, false: save file dialog box
The second parameter indicates the default extension of the file to be opened (it is important to save the file)
The third parameter indicates the target file name to be opened. If no extension is provided, the name specified by the second parameter is used.
The fourth parameter ofn_filemustexist indicates that the target must exist. ofn_hidereadonly does not display the read-only file, and some other parameters can be searched by ofn_xxx.
The fifth parameter indicates to filter the string table. In this format, "title | filter table |"
2. Browse folder dialog box
There is no ready-made MFC class available in the Open folder dialog box. You need to use an API function called shell. Three steps: In the configuration dialog box, open the dialog box and obtain the returned value (Folder path ).
// Obtain the lpitemidlist of a specific folder, which can be understood as handle.
// You can use csidl_xxx to search for a specific folder.
Lpitemidlist rootloation;
Shgetspecialfolderlocation (null, csidl_desktop, & rootloation );
If (rootloation = NULL ){
// Unkown Error
// Return
}
// Configuration dialog box
Browseinfo Bi;
Zeromemory (& BI, sizeof (BI ));
Bi. pidlroot = rootloation; // the root directory of the folder dialog box. If not specified, it is my computer.
Bi. lpsztitle = _ T ("dialog box Header"); // you can leave it unspecified.
// Bi. ulflags = bif_editbox | bif_returnonlyfsdirs;
// Open the dialog box, a bit like domodal
Lpitemidlist targetlocation = shbrowseforfolder (& BI );
If (targetlocation! = NULL ){
Tchar targetpath [max_path];
Shgetpathfromidlist (targetlocation, targetpath );
// MessageBox (targetpath );
}