Almost every time I develop an application, I need the «Browse For Folder» dialog functionality. But every time I use it, I have to allocate and initialize the
BROWSEINFO structure and call
Obviously, this code would have to be duplicated a lot of times. To avoid such duplications, I created a class that encapsulates all the above mentioned code inside.
Using the Code
What you have to do if you want to put it to work:
- Download the source code and add it to your project.
- Create a
- Call the
CBrowseFolderDialog::BrowseFolder() member function that creates a dialog and returns the path selected. Here is how the function is declared:
PCTSTR BrowseFolder(HWND hwndOwner = NULL, PCTSTR pTitle = NULL, int nCSIDL = 0, LPARAM lParam = NULL, UINT uFlags = BIF_NEWDIALOGSTYLE, BFFCALLBACK callbackProc = NULL
As you can see, all the parameters have default values. Here are several examples of using
CBrowseFolderDialog in the code:
CString strFolderPath = dlg.BrowseFolder();
strFolderPath = dlg.BrowseFolder(NULL, TEXT("SELECT FOLDER!"));
strFolderPath = dlg.BrowseFolder(CSIDL_DESKTOP);
strFolder = dlg.BrowseFolder(NULL, NULL, NULL, (LPARAM)_T("C:\\"));
// example 5: specifying your own browse
// callback procedure as 6-th parameter
static int CALLBACK BrowseCallbackProc(HWND hwnd, UINT uMsg,
LPARAM lParam, LPARAM lpData)
// some implementation
strFolder = dlg.BrowseFolder(NULL, NULL, NULL, NULL,
I have to explain a little about the fifth parameter. It is the
BROWSEINFO flags, and is explained in detail on MSDN. The default value is
BIF_NEWDIALOGSTYLE. If you want to get a different behaviour, specify your own flag combination here.
- 16th April, 2009: Initial post.
- 17th April, 2009: Article updated.