Directory selection dialog box.

static CString strTmpPath;

static int CALLBACK BrowseCallbackProc(HWND hwnd, UINT uMsg, LPARAM lParam, LPARAM lpData);

static int CALLBACK BrowseCallbackProc(HWND hwnd, UINT uMsg, LPARAM lParam, LPARAM lpData)
{
TCHAR szDir[MAX_PATH];
switch(uMsg){
case BFFM_INITIALIZED:
if (lpData){
_tcscpy_s(szDir, strTmpPath.GetBuffer(strTmpPath.GetLength()));
SendMessage(hwnd,BFFM_SETSELECTION,TRUE,(LPARAM)szDir);
}
break;
case BFFM_SELCHANGED: {
if (SHGetPathFromIDList((LPITEMIDLIST) lParam ,szDir)){
SendMessage(hwnd,BFFM_SETSTATUSTEXT,0,(LPARAM)szDir);
}
break;
}
default:
break;
}

return 0;
}

BOOL GetFolderA(CString* strSelectedFolder, const CHAR* lpszTitle,  const HWND hwndOwner,   const CHAR* strRootFolder,const CHAR* strStartFolder)
{
CHAR pszDisplayName[MAX_PATH];
LPITEMIDLIST lpID;
BROWSEINFOA bi;

bi.hwndOwner = hwndOwner;
if (strRootFolder == NULL){
bi.pidlRoot = NULL;
}else{
LPITEMIDLIST  pIdl = NULL;
IShellFolder* pDesktopFolder;
CHAR         szPath[MAX_PATH];
OLECHAR       olePath[MAX_PATH];
ULONG         chEaten;
ULONG         dwAttributes;

strcpy_s(szPath, MAX_PATH, (LPCSTR)strRootFolder);
if (SUCCEEDED(SHGetDesktopFolder(&pDesktopFolder)))
{
pDesktopFolder->ParseDisplayName(NULL, NULL, olePath, &chEaten, &pIdl, &dwAttributes);
pDesktopFolder->Release();
}
bi.pidlRoot = pIdl;
}
bi.pszDisplayName = pszDisplayName;
bi.lpszTitle = lpszTitle;
bi.ulFlags = BIF_RETURNONLYFSDIRS | BIF_STATUSTEXT;
bi.lpfn = BrowseCallbackProc;
if (strStartFolder == NULL){
bi.lParam = FALSE;
}else{
strTmpPath.Format(_T(“%s”), strStartFolder);
bi.lParam = TRUE;
}
bi.iImage = NULL;
lpID = SHBrowseForFolderA(&bi);
if (lpID != NULL){
BOOL b = SHGetPathFromIDListA(lpID, pszDisplayName);
if (b == TRUE){
strSelectedFolder->Format(_T(“%s”),pszDisplayName);
return TRUE;
}
}else{
strSelectedFolder->Empty();
}
return FALSE;
}

BOOL GetFolderW(CString* strSelectedFolder, const WCHAR* lpszTitle,  const HWND hwndOwner, const WCHAR* strRootFolder,const WCHAR* strStartFolder)
{
WCHAR pszDisplayName[MAX_PATH];
LPITEMIDLIST lpID;
BROWSEINFOW bi;

bi.hwndOwner = hwndOwner;
if (strRootFolder == NULL){
bi.pidlRoot = NULL;
}else{
LPITEMIDLIST  pIdl = NULL;
IShellFolder* pDesktopFolder;
WCHAR         szPath[MAX_PATH];
OLECHAR       olePath[MAX_PATH];
ULONG         chEaten;
ULONG         dwAttributes;

wcscpy_s(szPath, MAX_PATH, (LPCWSTR)strRootFolder);
if (SUCCEEDED(SHGetDesktopFolder(&pDesktopFolder)))
{
pDesktopFolder->ParseDisplayName(NULL, NULL, olePath, &chEaten, &pIdl, &dwAttributes);
pDesktopFolder->Release();
}
bi.pidlRoot = pIdl;
}
bi.pszDisplayName = pszDisplayName;
bi.lpszTitle = lpszTitle;
bi.ulFlags = BIF_RETURNONLYFSDIRS | BIF_STATUSTEXT;
bi.lpfn = BrowseCallbackProc;
if (strStartFolder == NULL){
bi.lParam = FALSE;
}else{
strTmpPath.Format(_T(“%s”), strStartFolder);
bi.lParam = TRUE;
}
bi.iImage = NULL;
lpID = SHBrowseForFolderW(&bi);
if (lpID != NULL){
BOOL b = SHGetPathFromIDListW(lpID, pszDisplayName);
if (b == TRUE){
strSelectedFolder->Format(_T(“%s”),pszDisplayName);
return TRUE;
}
}else{
strSelectedFolder->Empty();
}
return FALSE;
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: