The 'Browse For Folder' dialog allows the user to select a folder from all available local drives and network resources.
The following code snippet demonstrate how to display this dialog-box. The BrowseFolders function accept 3 parameters:
hwnd - The handle of the parent window
lpszFolder - Address of a buffer to receive the selected folder. Before calling this function, you should fill this buffer with the folder that you want to be selected when the dialog-box is loaded at first.
lpszTitle - The text that should be displayed as a title of the 'Browse Folders' dialog-box
Return back to C/C++ code index
#include <windows.h> #include <shlobj.h> int CALLBACK BrowseForFolderCallback(HWND hwnd,UINT uMsg,LPARAM lp, LPARAM pData) { char szPath[MAX_PATH]; switch(uMsg) { case BFFM_INITIALIZED: SendMessage(hwnd, BFFM_SETSELECTION, TRUE, pData); break; case BFFM_SELCHANGED: if (SHGetPathFromIDList((LPITEMIDLIST) lp ,szPath)) { SendMessage(hwnd, BFFM_SETSTATUSTEXT,0,(LPARAM)szPath); } break; } return 0; } BOOL BrowseFolders(HWND hwnd, LPSTR lpszFolder, LPSTR lpszTitle) { BROWSEINFO bi; char szPath[MAX_PATH + 1]; LPITEMIDLIST pidl; BOOL bResult = FALSE; LPMALLOC pMalloc; if (SUCCEEDED(SHGetMalloc(&pMalloc))) { bi.hwndOwner = hwnd; bi.pidlRoot = NULL; bi.pszDisplayName = NULL; bi.lpszTitle = lpszTitle; bi.ulFlags = BIF_STATUSTEXT; //BIF_EDITBOX bi.lpfn = BrowseForFolderCallback; bi.lParam = (LPARAM)lpszFolder; pidl = SHBrowseForFolder(&bi); if (pidl) { if (SHGetPathFromIDList(pidl,szPath)) { bResult = TRUE; strcpy(lpszFolder, szPath); } pMalloc->Free(pidl); pMalloc->Release(); } } return bResult; } |