MFC Wrapper for SHBrowseForFolder | CodeGuru

MFC Wrapper for SHBrowseForFolder

Copyright ) 1998-1999, Kenneth M. Reed, All rights reserved. Description CFolderDialog is an MFC based class that encapsulates the Windows Shell function for browsing folders, ::ShBrowseForFolder. To use it, you create an instance of CFolderDialog and call the DoModal method. Upon return, the method GetPathName can be used to retrieve the path to the user […]

Written By
CodeGuru Staff
CodeGuru Staff
Feb 3, 1999
2 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

Copyright ) 1998-1999, Kenneth M. Reed, All rights
reserved.

Description

CFolderDialog is an MFC based class that
encapsulates the Windows Shell function for browsing folders, ::ShBrowseForFolder.

To use it, you create an instance of CFolderDialog
and call the DoModal method. Upon return, the method
GetPathName can be used to retrieve the path to the
user selected folder.

The constructor allows you to specify an optional initial
folder, flags specific to the shell function (see the description
of the ulFlags field of the BROWSEINFO structure
in the help for Visual Studio or the Windows Shell API) and a
pointer to the parent window’s CWnd. All of
these arguments are optional.

You may override the two virtual methods OnInitDialog
and OnSelChanged in a derived class to customize
their behavior. The default behavior of OnInitDialog
is to call SetSelection and SetStatusText
with the folder name provided in the constructor. The default
version of OnSelChanged places the new path into the
status text area of the dialog.

The class provides utility functions for 1) enabling or
disabling the OK button (EnableOK), 2) setting the
status text that appears above the folder list box in the browser
folder dialog box (SetStatusText) and 3) setting the
current selection (SetSelection). The file MyFolderDialog.h,
included in the zip file, shows an example of a derived class
which uses SetStatusText.

For advanced users, the BROWSEINFO structure is
available via the m_bi member variable. This
structure is initialized by the constructor and any changes
should be made before the call to DoModal. See the help for
Visual Studio or the Windows Shell API for more information on
the BROWSEINFO structure.

The zip file also contains the source for CMyFolderDialog, a
class derived from CFolderDialog to show how to take advantage of
some of the advanced features.

Revisions

December 29, 1998
Added the CMyFolderDialog source code the version
available on the codeguru site.
 
June 23, 1998
Fixed a problem in EnableOK. The documenation for the
SHBrowseForFolder call back incorrectly states that it is
the wParam on the BFFM_ENABLEOK message which controls
the enabling/disabling of the OK button. However, It is
actually the lParam.
Thanks to Langis Pitre for finding this one.
 
April 5, 1998
Changed the way status text is handled:
  • The constructor now forces the BIF_STATUSTEXT
    flag to be set
  • Changed the base OnInitDialog method
    to display the initial folder in the status text.
  • Changed the base OnSelChanged method
    to update the contents of m_strFinalFolderName
    and display the new folder in the status text
    area.
  • Removed the MyFolderDialog.h file
    from the zip.
Advertisement

Bugs and Suggestions

Please send suggestions and/or bugs to reedk@daneel.mv.com.

Ken Reed

Download source code – 6 KB

Date Last Updated: February 3, 1999

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.