Click here to Skip to main content
11,706,728 members (40,140 online)
Click here to Skip to main content

CBrowseFolderDialog Class

, 20 Apr 2009 CPOL 16.4K 408 9
Rate this:
Please Sign up or sign in to vote.
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Denis Ponomarenko
Software Developer
Ukraine Ukraine
No Biography provided

You may also be interested in...

Comments and Discussions

 
QuestionWhat is new about your code Pin
rajas16-Apr-09 15:50
memberrajas16-Apr-09 15:50 
AnswerRe: What is new about your code Pin
Denis Ponomarenko16-Apr-09 22:04
memberDenis Ponomarenko16-Apr-09 22:04 
GeneralNice. Pin
c-smile16-Apr-09 8:59
memberc-smile16-Apr-09 8:59 
AnswerRe: Nice. Pin
Denis Ponomarenko16-Apr-09 22:10
memberDenis Ponomarenko16-Apr-09 22:10 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150819.1 | Last Updated 20 Apr 2009
Article Copyright 2009 by Denis Ponomarenko
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid