Created
April 5, 2016 02:53
-
-
Save superlucky8848/9cef3673bf177df6a5171e8b4c3d6883 to your computer and use it in GitHub Desktop.
Sample for using folder browser in Windows C++ desktop app.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int CALLBACK BrowserCallbackProc(HWND hWnd, UINT uMsg, LPARAM lParam, LPARAM lpData) | |
{ | |
switch (uMsg) | |
{ | |
case BFFM_INITIALIZED: | |
::SendMessage(hWnd, BFFM_SETSELECTION, 1, lpData); | |
break; | |
default: | |
break; | |
} | |
return 0; | |
} | |
////////////////////////////////////////////////////////////////////////// | |
// [IN] HWND hWnd: | |
// [IN] LPCTSTR strTitle:窗口标题 | |
// [IN][OUT] CString& strDir:默认路径和选择得到的目录 | |
//________________________________________________________________________ | |
bool MyAppUtil::SelDirectory(HWND hWnd, LPCTSTR strTitle, CString& strDir) | |
{ | |
BROWSEINFO bi; | |
TCHAR szDisplayName[MAX_PATH] = { 0 }; | |
bi.hwndOwner = hWnd; | |
bi.pidlRoot = NULL; | |
bi.pszDisplayName = szDisplayName; | |
bi.lpszTitle = strTitle; | |
bi.ulFlags = 0; | |
bi.lpfn = BrowserCallbackProc; | |
bi.lParam = (LPARAM)(LPCTSTR)strDir; | |
bi.iImage = NULL; | |
//显示目录选择对话框,如果按了取消,那么piid为NULL | |
ITEMIDLIST* piid = ::SHBrowseForFolder(&bi); | |
if (piid == NULL) | |
{ | |
return false; | |
} | |
// 得到选择的目录 | |
BOOL bValidPath = ::SHGetPathFromIDList(piid, szDisplayName); | |
if (!bValidPath) | |
{ | |
return false; | |
} | |
LPMALLOC lpMalloc; | |
VERIFY(::SHGetMalloc(&lpMalloc) == NOERROR); | |
lpMalloc->Free(piid); | |
lpMalloc->Release(); | |
if (szDisplayName[0] == '\0') | |
{ | |
return false; //用户选择的可能的虚拟文件系统 | |
} | |
strDir = szDisplayName; | |
strDir.TrimRight(_T("\\")); | |
return true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment