Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

CBrowseFolderDialog Class

0.00/5 (No votes)
20 Apr 2009 1  
A simple BrowseForFolder dialog functionality implementation.

sample1

Introduction

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 SHGetMalloc(), SHGetFolderLocation(), SHGetPathFromIDList(), etc.

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:

  1. Download the source code and add it to your project.
  2. Create a CBrowseFolderDialog instance.
  3. 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, // parent window handle
        PCTSTR pTitle = NULL, // text that will be shown in the top of dialog
        int nCSIDL = 0,   // here you can specify initial folder CSIDL
        LPARAM lParam = NULL, // here you can specify initial folder path
        UINT uFlags = BIF_NEWDIALOGSTYLE,  // dialog flags
        BFFCALLBACK callbackProc = NULL // callback procedure,
                // use NULL if you will not provide it);

As you can see, all the parameters have default values. Here are several examples of using CBrowseFolderDialog in the code:

CBrowseFolderDialog dlg;

// example 1:
CString strFolderPath = dlg.BrowseFolder();

// example 2:
strFolderPath = dlg.BrowseFolder(NULL, TEXT("SELECT FOLDER!"));

// example 3: specifying initial folder CSIDL
strFolderPath = dlg.BrowseFolder(CSIDL_DESKTOP);

// example 4: specifying initial folder Path
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
    return 0;
}

strFolder = dlg.BrowseFolder(NULL, NULL, NULL, NULL, 
                             NULL, BrowseCallbackProc);

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.

History

  • 16th April, 2009: Initial post.
  • 17th April, 2009: Article updated.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here