3/13/2014

C, C++, To select folder only and get path using SHBrowseForFolder function, example source code

//Get only folder name
 ITEMIDLIST *pidlBrowse;
 WCHAR pszPathname[MAX_PATH];
 
 BROWSEINFO BrInfo;
 BrInfo.hwndOwner = GetSafeHwnd();
 BrInfo.pidlRoot = NULL;
 
 CString str =_T("c:\\"); //initial path
 LPCTSTR lpszDefaultFolder = str;
 memset(&BrInfo, 0, sizeof(BrInfo));
 BrInfo.pszDisplayName=(LPSTR)pszPathname;
 BrInfo.lpszTitle=_T("Select directory");
 BrInfo.ulFlags=BIF_RETURNONLYFSDIRS; //select folder only
 //BrInfo.ulFlags=BIF_BROWSEINCLUDEFILES; //select folder and file
 BrInfo.lParam=(LPARAM)lpszDefaultFolder;
 //BrInfo.lpfn=BrowseCallback;
 
 pidlBrowse = ::SHBrowseForFolder(&BrInfo);

 
 if (pidlBrowse != NULL)
 {
  //Get Path
  SHGetPathFromIDList(pidlBrowse, (LPSTR)pszPathname);
  AfxMessageBox((LPCSTR)pszPathname);
 }


...



No comments:

Post a Comment