Click here to Skip to main content
15,867,568 members
Articles / Web Development / HTML

CXSBrowseFolder - A Simple Class to Encapsulate SHBrowseForFolder

Rate me:
Please Sign up or sign in to vote.
4.96/5 (17 votes)
19 May 2002CPOL1 min read 122.9K   2.5K   38   15
This class makes it easier to use the shell function SHBrowseForFolder

Sample Image

Introduction

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:

C++
// 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);
    break;

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

    // 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);
    break;
}

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

C++
DWORD CXSBrowseFolder::GetStyle()

Description

  • Returns the current style of the dialog

Parameters

  • None

Return

  • The current style of the dialog (refer to the SHBrowseForFolder docs for style information)
C++
DWORD CXSBrowseFolder::ModifyStyle(DWORD add, DWORD remove = 0)

Description

  • Adds and/or removes styles from the dialog (See shell docs for available styles)

Parameters

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

Return

  • Current style (after modifications)
C++
void CXSBrowseFolder::SetTitle(LPSTR title)

Description

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

Parameters

  • title: The text to be displayed

Return

  • None
C++
CXSBrowseFolder::retCode CXSBrowseFolder::Show(HWND parent, LPSTR pathBuffer)

Description

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

Parameters

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

Return

  • CXSBrowseFolder::RET_CANCEL: User clicked the dialog's cancel button
  • CXSBrowseFolder::RET_NOPATH: The shell did not return a valid path for the selection
  • CXSBrowseFolder::RET_OK: 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.

License

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


Written By
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 Pin
Leif Simon Goodwin1-Jan-11 11:42
Leif Simon Goodwin1-Jan-11 11:42 
QuestionHow to add starting folder.... Pin
pige3-May-07 22:34
pige3-May-07 22:34 
AnswerRe: How to add starting folder.... Pin
lyuabe13-Jun-14 11:52
lyuabe13-Jun-14 11:52 
GeneralRegarding CXSBrowseFolder Pin
jk403205-Apr-07 1:42
jk403205-Apr-07 1:42 
GeneralWinCe Pin
Joe ce00113-Dec-05 20:07
Joe ce00113-Dec-05 20:07 
GeneralWindows 2000 Pin
luniv040418-Oct-05 4:44
luniv040418-Oct-05 4:44 
GeneralRe: Windows 2000 Pin
The_Mega_ZZTer24-Oct-05 12:07
The_Mega_ZZTer24-Oct-05 12:07 
GeneralNew Dialog Style Pin
Casper7022-Jan-03 7:43
Casper7022-Jan-03 7:43 
GeneralRe: New Dialog Style Pin
Dana Holt10-May-03 13:50
Dana Holt10-May-03 13:50 
GeneralWorked...good Pin
Anonymous26-Aug-02 8:20
Anonymous26-Aug-02 8:20 
GeneralRe: Worked...good Pin
Dana Holt28-Aug-02 15:11
Dana Holt28-Aug-02 15:11 
GeneralAlternatives Pin
20-May-02 22:13
suss20-May-02 22:13 
GeneralGood start Pin
Shog920-May-02 18:23
sitebuilderShog920-May-02 18:23 
GeneralRe: Good start Pin
Dana Holt21-May-02 3:05
Dana Holt21-May-02 3:05 
GeneralRe: Good start Pin
real name10-Mar-03 21:48
sussreal name10-Mar-03 21:48 

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.