Click here to Skip to main content
15,896,153 members
Articles / Desktop Programming / MFC

CFolderDialog - Selecting Folders

Rate me:
Please Sign up or sign in to vote.
4.95/5 (89 votes)
17 Feb 2005CPOL5 min read 553.8K   24.9K   166  
The CFolderDialog class allows you to add a folder-selection dialog box to your applications.
/////////////////////////////////////////////////////////////////////////////
/* 
DESCRIPTION:
	CFolderDialog  - Folder Selection Dialog Class	
	http://www.codeproject.com/dialog/cfolderdialog.asp

NOTES:
	Copyright(C) Armen Hakobyan, 2002
	mailto:armenh@web.am
	
VERSION HISTORY:
	24 Mar 2002 - First release
	30 Mar 2003 - Some minor changes
				- Added missing in old Platform SDK new flag definitions  
				- Added support for both MFC 6.0 and 7.0
				- Added OnIUnknown handler for Windows XP folder filtration
				- Added SetExpanded and SetOKText and GetSelectedFolder functions
	24 May 2003 - Added OnSelChanged implementation
*/
/////////////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "FolderDlg.h"

/////////////////////////////////////////////////////////////////////////////

#ifndef BFFM_VALIDATEFAILED
	#ifndef UNICODE
		#define BFFM_VALIDATEFAILED		3
	#else
		#define BFFM_VALIDATEFAILED		4	
	#endif
#endif

#ifndef BFFM_IUNKNOWN
	#define BFFM_IUNKNOWN				5
#endif

/////////////////////////////////////////////////////////////////////////////
// CFolderDialog

IMPLEMENT_DYNAMIC( CFolderDialog, CDialog )

CFolderDialog::CFolderDialog( IN LPCTSTR lpszTitle		/*NULL*/,
							  IN LPCTSTR lpszSelPath	/*NULL*/,
							  IN CWnd*	 pParentWnd		/*NULL*/,
							  IN UINT	 uFlags			/*BIF_RETURNONLYFSDIRS*/ )
			 : CCommonDialog( pParentWnd )
			 , m_hWnd( NULL )
{
	::ZeroMemory( m_szFolPath, MAX_PATH );
	::ZeroMemory( m_szSelPath, MAX_PATH );
	::ZeroMemory( &m_bi, sizeof( BROWSEINFO ) );

	if( lpszSelPath != NULL )
		SetSelectedFolder( lpszSelPath );
	
	// Fill data	
	m_bi.hwndOwner	= pParentWnd->GetSafeHwnd();
	m_bi.pidlRoot	= NULL;	
	m_bi.lpszTitle	= lpszTitle;
	m_bi.ulFlags	= uFlags;
	m_bi.lpfn		= (BFFCALLBACK)BrowseCallbackProc;
	m_bi.lParam		= (LPARAM)this;

	// The size of this buffer is assumed to be MAX_PATH bytes
	m_bi.pszDisplayName = new TCHAR[ MAX_PATH ];
	::ZeroMemory( m_bi.pszDisplayName, MAX_PATH * sizeof( TCHAR ) );
}

CFolderDialog::~CFolderDialog( void )
{
	_delete2( m_bi.pszDisplayName );	
	::ZeroMemory( &m_bi, sizeof( BROWSEINFO ) );	
}

BEGIN_MESSAGE_MAP( CFolderDialog, CCommonDialog )
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CFolderDialog message handlers

#if ( _MFC_VER < 0x0700 )
	INT		CFolderDialog::DoModal( void )
#else
	INT_PTR CFolderDialog::DoModal( void )
#endif
{
	ASSERT_VALID( this );	
	ASSERT( m_bi.lpfn != NULL );
		
	m_bi.hwndOwner = PreModal();	
	INT_PTR nRet   = -1;
	LPITEMIDLIST lpItemIDList = ::SHBrowseForFolder( &m_bi );

	if( lpItemIDList != NULL )
	{
		if( ::SHGetPathFromIDList( lpItemIDList, m_szFolPath ) )
		{
			IMalloc* lpMalloc = NULL;
			if( SUCCEEDED( ::SHGetMalloc( &lpMalloc ) ) )
			{
				lpMalloc->Free( lpItemIDList );
				lpMalloc->Release();
				lpMalloc = NULL;
			}
			nRet = IDOK;
		}
		else
			nRet = IDCANCEL;

		lpItemIDList = NULL;
	}

	PostModal();
	return nRet;	
}

/////////////////////////////////////////////////////////////////////////////
// Overridables:

void CFolderDialog::OnInitialized( void )
{
	if( ::lstrlen( m_szSelPath ) > 0 )
		SetSelection( m_szSelPath );
}

void CFolderDialog::OnSelChanged( IN LPITEMIDLIST lpItemIDList )
{
	if( m_bi.ulFlags & BIF_STATUSTEXT )
	{
		TCHAR szSelFol[ MAX_PATH ] = { 0 };
		if( ::SHGetPathFromIDList( lpItemIDList, szSelFol ) )	
			SetStatusText( szSelFol );
	}
}

INT CFolderDialog::OnValidateFailed( IN LPCTSTR /*lpszFolderPath*/ )
{	
	::MessageBeep( MB_ICONHAND );
	return 1; // Return 1 to leave dialog open, 0 - to end one
}

void CFolderDialog::OnIUnknown( IN IUnknown* /*lpIUnknown*/ )
{
}

/////////////////////////////////////////////////////////////////////////////
// Callback function used with the SHBrowseForFolder function. 

INT CALLBACK CFolderDialog::BrowseCallbackProc( HWND hWnd, 
					UINT uMsg, LPARAM lParam, LPARAM lpData )
{
	CFolderDialog* pThis = (CFolderDialog*)lpData;
	pThis->m_hWnd = hWnd;
	INT nRet = 0;

	switch( uMsg )
	{
	case BFFM_INITIALIZED:
		pThis->OnInitialized();
		break;
	case BFFM_SELCHANGED:
		pThis->OnSelChanged( (LPITEMIDLIST)lParam );
		break;
	case BFFM_VALIDATEFAILED:
		nRet = pThis->OnValidateFailed( (LPCTSTR)lParam );
		break;
	case BFFM_IUNKNOWN:
		pThis->OnIUnknown( (IUnknown*)lParam );
		break;
	default:
		ASSERT( FALSE );
		break;
	}

	pThis->m_hWnd = NULL;
	return nRet;	
}

/////////////////////////////////////////////////////////////////////////////

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

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) SafeNet Inc
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions