June 12, 2016

[ MFC ] - Selecting a folder using Folder Browser Dialog

1. Introduction

In MFC Application, many times we end up browsing the folder structure of windows operating system and picking a folder from it. In this article, we will create a sample application that picks a folder from a tree structure.

2. About the example

The sample application is shown below:

Folder Browser Dialog Example
Fig 1. Folder Browser Dialog Example

When we click on the “Browse Folder” button, a dialog is displayed with a tree structure. This tree structure actually lists the windows folders. Once a folder is picked from the dialog, the picked folder name is displayed in the first read only text box. The second textbox displays fully qualified path to the folder.

3. BROWSEINFO struct & SHBrowseForFolder

A Folder browser dialog can be created by making a call to the global function called "SHBrowseForFolder". This function expects a structure called BROWSEINFO and the same structure is used to get the folder selected in the folder browser dialog. The BROWSEINFO structure is given below:

 typedef struct _browseinfoA {
    HWND        hwndOwner;
    LPSTR        pszDisplayName;        // Return display name of item selected.
    LPCSTR       lpszTitle;                     // text to go in the banner over the tree.
    UINT         ulFlags;                       // Flags that control the return stuff
    BFFCALLBACK  lpfn;
    LPARAM       lParam;                        // extra info that's passed back in callbacks
    int          iImage;                        // output var: where to return the Image index.

4 SHBrowseForFolder code - explained

1) First handle the Button click message for “Browse Folder” button. The message handler function is shown below:

void CFolderBrowseDlg::OnBnClickedButtonBrowse()

2) In the function body, two char buffers called foldername and folderpath are created by specifying the maximum capacity through the MAX_PATH constant.  Below is the code:

//Sample 01: Buffer to get the selected folder
char foldername[MAX_PATH];
char folderpath[MAX_PATH];

3) Next the BROSWEINFO structure is created and populated. Here, the member hwndOwner expects the owner dialog from which the folder browser dialog will be invoked. The member lpszTitle specifies the description that will get displayed above the folder tree structure. The ulFlags member is used to set the folder browser dialog options. In our example, BIF_RETURNONLYFSDIRS is used tell the browser dialog that only the folders that belongs to the file system needs to be selected. The member pszDisplayName returns selected folder name. Below is the structure member filled with required data before making call to the Folder browser dialog:

//Sample 02: Prepare BROWSEINFO structure
binfo.hwndOwner = this->m_hWnd;
binfo.iImage = NULL;
binfo.lParam = NULL;
binfo.lpfn = NULL;
binfo.lpszTitle = "Browse and select a specific folder";
binfo.pidlRoot = NULL;
binfo.pszDisplayName = foldername;

4) The folder browser dialog is displayed by calling the function SHBrowseForFolder. This function takes the BROSEINFO structure as parameter and it uses that for displaying the dialog. This dialog returns ID list of folder that corresponds to the selected node (Folder) of tree structure. If the ID list returned by the function call is empty, then user clicked the cancel button in the dialog. Below is the code:

//Sample 03: Now make a call to SHBrowseForFolder
LPITEMIDLIST item_list = SHBrowseForFolder(& binfo);
if (item_list == NULL )

5)  The LPITEMIDLIST is passed to the function SHGetPathFromIDList to get the full path to the folder. Below is the function call which returns full path in the second parameter:

//Sample 04: Get Full Folder Name from ID List

6) At the end of the message handler, foldername and fully qualified path to the folder name is displayed in the corresponding text boxes. Below is code:

//Sample 05: Show the Selected Folder and folder path

The Sample application is shown in the below video.

Source Code: Download

No comments:

Post a Comment

Leave your comment(s) here.

Like this site? Tell it to your Friend :)