Code of the open file dialog box and Open folder dialog box

Source: Internet
Author: User

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

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.