The open and save file dialog box and browse folder dialog box are implemented by MFC.

Source: Internet
Author: User

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 );
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.