Click here to Skip to main content
13,352,879 members (80,953 online)
Click here to Skip to main content
Add your own
alternative version


149 bookmarked
Posted 8 Jan 2004

CBrowseCtrl - Adding File/Folder-Browse Features to Your Projects Easily

, 25 May 2004
Rate this:
Please Sign up or sign in to vote.
A file/folder browsing control that integrates an editbox and a button with built-in images.

Sample Image - CBrowseCtrl.gif


Usually, when we need to add file/folder-browse features to our projects, we have to do quite some trivia: first we must create a CEdit which will display the path name, then we need to create a CButton and handle its ON_BN_CLICKED event to launch the CFileDialog and copy the path name to the CEdit. Furthermore, if we want to make it look nice, we will have to include some icon or bitmap resources to decorate the CButton. It could cause some headache if we have multiple dialogs which all need the file/folder-browse feature on them.

I have been thinking that it would be nice if there was such a control in MFC, which integrates a CEdit and a CButton on it, handles the control events, pops up the CFileDialog and updates CEdit text automatically. And it'd be even nicer if that control has some built-in images, that is, it has its own drawing capability without requiring us to provide any image resource to it. And of course, some tooltips wouldn't hurt... Unfortunately, there's no such control in MFC, but fortunately we can always make our own.

CBrowseCtrl is made for meeting the exact needs I described in the above paragraphs. It derives from CButton, has an editbox, a browse button and a tooltip on it. It can draw some built-in images on the browse button as well, so you don't need to include any icon or bitmap resources for this control. It handles button events by itself so whenever the user clicks on the browse button, a file/folder dialog pops up.

How to Use

You need to add source files BrowseCtrl.h and BrowseCtrl.cpp to your workspace first, and include BrowseCtrl.h wherever needed. To create the control, you can either use CBrowseCtrl::Create to create one at runtime, or draw a CButton on the dialog template and bind it with a CBrowseCtrl variable.

Code Samples

Specifying Control Styles

You may access the control styles anytime to change its appearance and behavior. Multiple style flags may be combined using the "|" operator. The valid style flags are:



BC_CTL_ALLOWEDITAllows user to type in the editbox
BC_CTL_FOLDERSONLYBrowse for folders instead of files
BC_BTN_LEFTDisplay the browse button on the left-side of the editbox
BC_BTN_FLATUse flat button style
BC_BTN_ICONUse icon, no text will be displayed
BC_ICO_ARROWFOLDERThe arrow-folder icon, must be combined with BC_BTN_ICON
BC_ICO_FOLDERThe folder icon, must be combined with BC_BTN_ICON
BC_ICO_EXPLORERThe Windows Explorer icon, must be combined with BC_BTN_ICON

To set the "Windows Explorer" icon on the browse button, make the button flat, and finally, make the control "Browse for Folders" only:

DWORD dwStyle = m_wndBrowseCtrl.GetButtonStyle();
dwStyle |= BC_BTN_ICON; // Make sure to display icon instead of text
dwStyle |= BC_BTN_FLAT; // Flat button
dwStyle &= ~BC_ICO_ARROWFOLDER; // Remove other icons
dwStyle &= ~BC_ICO_FOLDER;
dwStyle |= BC_ICO_EXPLORER; // Set the explorer icon
dwStyle |= BC_CTL_FOLDERSONLY; // Browse for folders only
m_wndBrowseCtrl.SetButtonStyle(dwStyle); // Apply

I've got tired of images and want the browse button to display text, say, "Click Me!", also make it display tooltip "Click to browse your files.":

m_wndBrowseCtrl.SetButtonStyle(m_wndBrowseCtrl.GetButtonStyle() & ~BC_BTN_ICON);
m_wndBrowseCtrl.SetButtonText(_T("Click Me!"));
m_wndBrowseCtrl.SetTooltipText(_T("Click to browse your files."));

Browsing for Files and Folders

CBrowseCtrl handles the button events by itself so whenever the user clicks on the browse button, a file or folder dialog is popped up automatically. You can, of course, programmatically handle it in your code also, usually you may want to initialize the file dialog using whatever you would pass into the CFileDialog constructor. CBrowseCtrl provides a set of functions to allow doing the same initialization, you basically can just treat the control as a dialog:

m_wndBrowseCtrl.SetOpenSave(TRUE); // TRUE-"open", FALSE-"save as"
m_wndBrowseCtrl.SetDefExt(NULL); // Do not use any default extension
m_wndBrowseCtrl.SetFilter(_T("Text Files (*.txt)|*.txt|All Files (*.*)|*.*||"));
m_wndBrowseCtrl.SetPathName(_T("c:\\MyApp\\readme.txt")); // default path

if (m_wndBrowseCtrl.DoModal() == IDOK)

To browse for folders:

                                            | BC_CTL_FOLDERSONLY);
// Display an editbox on the dialog
m_wndBrowseCtrl.SetFolderDialogTitle(_T("Please select a folder"));

if (m_wndBrowseCtrl.DoModal() == IDOK)

And if the user has selected multiple files from the file dialog, you can iterate through all path names by the following code:

if (m_wndBrowseCtrl.GetSelectedCount() > 1)// Multiple files selected
    POSITION pos = m_wndBrowseCtrl.GetStartPosition();
    while (pos != NULL)
        CString sPathName = m_wndBrowseCtrl.GetNextPathName(pos);
        // Process the path name...

Notifying the Parent Window

After the file/folder dialog is closed by the user, a notifying message is sent to the parent window. The wParam is either IDOK or IDCANCEL, the lParam is a pointer to this CBrowseCtrl object.

To make the control notify the parent window, you need to provide CBrowseCtrl a custom window message which is greater than 0. If the message you provide is 0, the message will not be sent. If you do not provide any message, no message will be sent either.

// Now ready to receive the notification!


So that's about it. It is a nice little control to have around, I hope it could help you in some cases. Any suggestions are welcome, thanks for your time.


Jan 08, 2004

  • Initial release.

Jan 09, 2004

  • Improved drawing functions to properly handle the case in which the control client area is too small to be drawn. In debug mode, it will provide a message box to inform the developer about that issue.
  • Improved the edit box so it acts better upon receiving and losing the input focus.
  • Updated the source code and demo project.

Jan 10, 2004

  • Fixed a problem where the edit box is not properly redrawn upon certain events. Thanks to JOHN11.
  • Added method CBrowseCtrl::GetSelectedCount which returns the number of items the user has selected in the most recent file/folder dialog that was terminated by IDOK.
  • Improved the mouse/focus events monitoring functions.
  • Fixed a drawing glitch which could occur when the user clicks on the edges of the edit box.
  • Changed the drawing-area calculating methods for performance improvement.
  • Updated the source code and demo project.

Jan 14, 2004

  • Updated SetPathName and GetPathName member functions.
  • Altered the message sending method so the lParam is now a pointer to this CBrowseCtrl object.
  • Updated the source code and demo project.

Jan 22, 2004

  • Added methods to monitor whether the user has manually changed the contents of the edit box when BC_CTL_ALLOWEDIT is set. The return value of GetPathName will also be properly affected.
  • The window titles of file/folder dialogs can now be accessed by calling SetDialogTitle and GetDialogTitle.
  • The banner text of folder dialogs can now be accessed by calling SetDialogBanner and GetDialogBanner.
  • Added method ModifyButtonStyle to allow convenient style changing.

Feb 07, 2004

  • Improved drawing functions so that images/text on the browse button are partially drawn if there is not enough space.

May 22, 2004

  • Updated drawing functions.
  • Added functions: GetDriveLetter, GetDirectory, GetFileName, GetFileTitle, GetFileExt.


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


About the Author

China China
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralLicense Pin
Lars Wetzel15-May-11 23:27
memberLars Wetzel15-May-11 23:27 
GeneralUsed in dotNetInstaller Pin
dB.18-Oct-09 16:41
memberdB.18-Oct-09 16:41 
GeneralAlt key causes to draw the control like a button Pin
jmaldarondo18-Aug-08 5:04
memberjmaldarondo18-Aug-08 5:04 
GeneralHow Pin
Kryptech20-Jun-07 9:51
memberKryptech20-Jun-07 9:51 
Generalbind to CBrowseCtrl Pin
jmhill2317-Apr-07 21:24
memberjmhill2317-Apr-07 21:24 
GeneralRe: bind to CBrowseCtrl Pin
davy77710-May-07 14:27
memberdavy77710-May-07 14:27 
GeneralRe: bind to CBrowseCtrl Pin
jmhill2315-May-07 9:30
memberjmhill2315-May-07 9:30 
GeneralRe: bind to CBrowseCtrl Pin
jmhill2315-May-07 21:20
memberjmhill2315-May-07 21:20 
GeneralRe: bind to CBrowseCtrl Pin
Duely24-Oct-09 10:00
memberDuely24-Oct-09 10:00 
GeneralCBrowseCtrl with own icon Pin
dressman19818-Mar-07 3:23
memberdressman19818-Mar-07 3:23 
Generalmultiple browse buttons Pin
Anxnahlia12-Jan-07 2:47
memberAnxnahlia12-Jan-07 2:47 
GeneralRe: multiple browse buttons Pin
=[ Abin ]=14-Jan-07 18:23
member=[ Abin ]=14-Jan-07 18:23 
GeneralBrilliant Pin
obriengearoid23-Jun-05 1:31
memberobriengearoid23-Jun-05 1:31 
General[Message Deleted] Pin
Anonymous15-May-05 9:59
sussAnonymous15-May-05 9:59 
GeneralRe: Any simpler code?! Pin
bassam.mehanni1-Aug-06 4:57
memberbassam.mehanni1-Aug-06 4:57 
GeneralRelease Mode Crashes Pin
j.jonez23-Jan-05 13:59
memberj.jonez23-Jan-05 13:59 
GeneralSetting the text in the File Name edit control Pin
Girays2-Jan-05 1:10
memberGirays2-Jan-05 1:10 
GeneralRe: Setting the text in the File Name edit control Pin
HTKim15-Jan-05 8:31
memberHTKim15-Jan-05 8:31 
QuestionFont bug? Pin
bob_elapanna30-Dec-04 8:03
memberbob_elapanna30-Dec-04 8:03 
AnswerRe: Font bug? Pin
=[ Abin ]=25-Jul-05 20:33
member=[ Abin ]=25-Jul-05 20:33 
GeneralSmall bug in GetNextPathName() Pin
eTurtle10-Sep-04 1:43
membereTurtle10-Sep-04 1:43 
Generalif it allow to create folder, that will be better. Pin
jancook27-May-04 8:38
memberjancook27-May-04 8:38 
AnswerRe: if it allow to create folder, that will be better. Pin
davedrums6-Nov-08 10:37
memberdavedrums6-Nov-08 10:37 
GeneralNice, but... Pin
pgiustinoni3-Apr-04 0:39
memberpgiustinoni3-Apr-04 0:39 
GeneralRe: Nice, but... Pin
=[ Abin ]=4-Apr-04 2:29
member=[ Abin ]=4-Apr-04 2:29 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.180111.1 | Last Updated 26 May 2004
Article Copyright 2004 by Abin
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid