Click here to Skip to main content
Click here to Skip to main content

CXSBrowseFolder - A Simple Class to Encapsulate SHBrowseForFolder

, 19 May 2002 CPOL
Rate this:
Please Sign up or sign in to vote.
This class makes it easier to use the shell function SHBrowseForFolder

Sample Image


As most of you probably know the shell function SHBrowseForFolder is easy to use, but there are a few things that have to be done every time that hardly ever change. One example is having to free the ITEMIDLIST pointer returned by the shell after the folder/file path is queried with SHGetPathFromIDList. My class takes care of that for you, and it automatically sets up default values for other parameters. 

Here's an example of using CXSBrowseFolder taken from the demo application:

// Create class 
CXSBrowseFolder foo; 

// Use the new style dialog
foo.ModifyStyle(BIF_NEWDIALOGSTYLE, 0);

// Set the dialog's title text
foo.SetTitle("This is the title text. Use CXSBrowseFolder::SetTitle() to set it:");

// Buffer for the returned path
char path[MAX_PATH];

// Display the dalog and check the return code
switch (foo.Show(GetSafeHwnd(), path)) {

    // Success
    case CXSBrowseFolder::RET_OK:
    MessageBox(path, "You Selected", MB_ICONINFORMATION | MB_TOPMOST);

    // User clicked cancel
    case CXSBrowseFolder::RET_CANCEL:
    MessageBox("Operation cancelled.", "Info", MB_ICONINFORMATION | MB_TOPMOST);

    // The shell did not return a path for the selection
    case CXSBrowseFolder::RET_NOPATH:
    MessageBox("The shell did not return a path for the selected item!", 
               "Uh Oh", MB_ICONSTOP | MB_TOPMOST);


This class is very simple, but here is an overview of the methods:


DWORD CXSBrowseFolder::GetStyle()

Returns the current style of the dialog


The current style of the dialog (refer to the SHBrowseForFolder docs for style information)


DWORD CXSBrowseFolder::ModifyStyle(DWORD add, DWORD remove = 0)
Adds and/or removes styles from the dialog. (See shell docs for available styles)

style(s) to add
remove (optional)
style(s) to remove

Current style (after modifications)


void CXSBrowseFolder::SetTitle(LPSTR title)

Sets the text displayed above the tree view in the dialog.

The text to be displayed



CXSBrowseFolder::retCode CXSBrowseFolder::Show(HWND parent, LPSTR pathBuffer)

Shows the folder/file browse dialog with the current settings.

HWND of a parent for the dialog
Buffer that will be filled with the path information of the selected file/folder

User clicked the dialog's cancel button
The shell did not return a valid path for the selection
The OK button was pressed and pathBuffer should contain a valid path


This class doesn't support customizing the dialog, but Microsoft doesn't recommend that with the new dialog style anyway because it's resizeable.

Well, that's about it for this article. I hope you find this simple class as useful and time saving as I have.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Dana Holt
Software Developer (Senior)
United States United States
I started learning C and 68K assembly language on an Atari ST around 1985 or so using the Mark Williams compiler. I was instantly hooked.
My favorite languages are: C/C++, C#, Ruby, and Python.
My first PC clone was a 386SX-25MHz with 4MB of RAM. OMG | :OMG:

Comments and Discussions

GeneralThank you, very useful PinmemberLeifUK1-Jan-11 12:42 
QuestionHow to add starting folder.... Pinmemberpige3-May-07 23:34 
AnswerRe: How to add starting folder.... Pinmemberlyuabe13-Jun-14 12:52 
GeneralRegarding CXSBrowseFolder Pinmemberjk403205-Apr-07 2:42 
GeneralWinCe PinmemberJoe ce00113-Dec-05 21:07 
GeneralWindows 2000 Pinmemberluniv040418-Oct-05 5:44 
GeneralRe: Windows 2000 PinmemberThe_Mega_ZZTer24-Oct-05 13:07 
GeneralNew Dialog Style PinmemberCasper7022-Jan-03 8:43 
GeneralRe: New Dialog Style PinmemberDana Holt10-May-03 14:50 
GeneralWorked...good PinsussAnonymous26-Aug-02 9:20 

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
Web03 | 2.8.150327.1 | Last Updated 20 May 2002
Article Copyright 2002 by Dana Holt
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid