Click here to Skip to main content
15,885,985 members
Articles / Programming Languages / C++

CBrowseFolderDialog Class

Rate me:
Please Sign up or sign in to vote.
4.00/5 (5 votes)
20 Apr 2009CPOL1 min read 29.1K   523   9   4
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:
C++
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:

C++
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Ukraine Ukraine
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionWhat is new about your code Pin
rajas16-Apr-09 15:50
rajas16-Apr-09 15:50 
AnswerRe: What is new about your code Pin
Deni\ys Ponomarenko16-Apr-09 22:04
Deni\ys Ponomarenko16-Apr-09 22:04 
GeneralNice. Pin
c-smile16-Apr-09 8:59
c-smile16-Apr-09 8:59 
AnswerRe: Nice. Pin
Deni\ys Ponomarenko16-Apr-09 22:10
Deni\ys Ponomarenko16-Apr-09 22:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.