Home

The Browse For Folder Dialog Box

     

Introduction

Microsoft Windows provides many dialog boxes useful for tasks that involve files, such as letting the user browse the hard drive to select a folder. This can be done using the Browse For Folder dialog box:

Browse For Folder

Creating a Browse for Folder Dialog Box

To help you create a Browse For Folder dialog box, Microsoft Windows provides a function named SHBrowseForFolder. Its syntax is:

PIDLIST_ABSOLUTE SHBrowseForFolder(LPBROWSEINFO lpbi);

This function takes one argument that is an object of type BROWSEINFO. This object is defined as:

typedef struct _browseinfo {
    HWND hwndOwner;
    PCIDLIST_ABSOLUTE pidlRoot;
    LPTSTR pszDisplayName;
    LPCTSTR lpszTitle;
    UINT ulFlags;
    BFFCALLBACK lpfn;
    LPARAM lParam;
    int iImage;
} BROWSEINFO, *PBROWSEINFO, *LPBROWSEINFO;

Here is an example of calling the SHBrowseForFolder() function:

//---------------------------------------------------------------------------

#include <vcl.h>
#include <Shlobj.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
	: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::mnuFileOpenClick(TObject *Sender)
{
	BROWSEINFO bi;

	bi.hwndOwner = Handle;
	bi.lpszTitle = "Select a Directory";

	SHBrowseForFolder(&bi);
}
//---------------------------------------------------------------------------

After using the Browse For Folder dialog box, if the user clicks Cancel or presses Esc, whatever change was made would be dismissed and the function would return false. If the user clicks OK or presses Enter, the function returns a PIDLIST_ABSOLUTE object. That object holds all the necessary information about the selection the user made.

 
 
     
 

Home Copyright © 2010-2016, FunctionX