📄 [c++] browseforfolder.txt
字号:
#include "shfolder.h"
CString BrowseForFolder(HWND hWnd, LPCSTR lpszTitle, UINT nFlags)
{
// We're going to use the shell to display a
// "Choose Directory" dialog box for the user.
CString strResult = "";
LPMALLOC lpMalloc;
if (::SHGetMalloc(&lpMalloc) != NOERROR)
{
// failed to get allocator
return strResult;
}
char szBuffer[_MAX_PATH];
char szDisplayName[_MAX_PATH];
BROWSEINFO browseInfo;
browseInfo.hwndOwner = hWnd;
// set root at Desktop
browseInfo.pidlRoot = NULL;
browseInfo.pszDisplayName = szDisplayName;
browseInfo.lpszTitle = lpszTitle;
browseInfo.ulFlags = nFlags;
browseInfo.lpfn = NULL;
browseInfo.lParam = 0;
LPITEMIDLIST lpItemIDList;
if ((lpItemIDList = ::SHBrowseForFolder(&browseInfo)) != NULL)
{
// Get the path of the selected folder from the item ID list.
if (::SHGetPathFromIDList(lpItemIDList, szBuffer))
{
// At this point, szBuffer contains the path the user chose.
if (szBuffer[0] == '\0')
{
// SHGetPathFromIDList failed, or SHBrowseForFolder failed.
AfxMessageBox("Failed to get directory", MB_ICONSTOP|MB_OK);
return strResult;
}
// We have a path in szBuffer!
strResult = szBuffer;
return strResult;
}
else
{
// The thing referred to by lpItemIDList
// might not have been a file system object.
// For whatever reason, SHGetPathFromIDList didn't work!
AfxMessageBox("Failed to get directory", MB_ICONSTOP|MB_OK);
return strResult; // strResult is empty
}
lpMalloc->Free(lpItemIDList);
lpMalloc->Release();
}
return strResult;
}
//然后这样使用:
CString strDir = BrowseForFolder(m_hWnd, "Select a directory:", BIF_RETURNONLYFSDIRS);
if (!strDir.IsEmpty())
{
GetDlgItem(IDC_HOMEDIRECTORY)->SetWindowText(strDir);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -