while working with MFC there are dialog boxes for opening files, but no dialog box to browse a folder, the function below just does the same. code snippet below shows how one can open a folder browsing dialog box.
BOOL BrowseFolder (
HWND hWnd, // Handle to the calling window.
LPSTR pszDisplayName, // Title of the dialog box
BOOL bEdit, // whether edit box is allowed
LPSTR szPath ) // starting path
{
// TODO: Add your control notification handler code here
LPMALLOC pMalloc;
BOOL bResult = FALSE ;
/* Gets the Shell’s default allocator */
if (::SHGetMalloc(&pMalloc) == NOERROR)
{
BROWSEINFO bi;
LPITEMIDLIST pidl ;
//Get help on BROWSEINFO struct – it’s got all the bit settings.
bi.hwndOwner = hWnd ;
bi.pidlRoot = NULL ;
bi.pszDisplayName = szPath ;
bi.lpszTitle = pszDisplayName ;
//bi.ulFlags = BIF_RETURNFSANCESTORS | BIF_RETURNONLYFSDIRS |BIF_EDITBOX ;
bi.ulFlags = BIF_RETURNONLYFSDIRS |BIF_VALIDATE ;
// whether you want to display an edit box
if ( bEdit ) bi.ulFlags = bi.ulFlags | BIF_EDITBOX ;
bi.lpfn = NULL; // No call back function
bi.lParam = 0; // No Extra parameter
// This next call issues the dialog box.
if ((pidl = ::SHBrowseForFolder(&bi)) != NULL)
{
if (::SHGetPathFromIDList(pidl, szPath))
{
// At this point pszBuffer contains the selected path
bResult = TRUE ;
}
// Free the PIDL allocated by SHBrowseForFolder.
pMalloc->Free(pidl);
}
// Release the shell’s allocator.
pMalloc->Release();
}
return bResult ;
}
Calling the function.
TCHAR szPath [255] ;
_tcscpy ( szPath, "C:\\" ) ;
BOOL bSuccess = BrowseFolder (
this->m_hWnd, "Broswe Source Folder", FALSE, szPath ) ;
if ( bSuccess )
{
// szPath contains the selected folder.
}