BROWSEINFO bi = { 0 };
bi.lpszTitle = L"请选择文件夹";
bi.ulFlags = BIF_NEWDIALOGSTYLE | BIF_RETURNONLYFSDIRS | BIF_STATUSTEXT;
bi.hwndOwner = ::GetForegroundWindow();
bi.lpfn = BrowseCallbackProc;
LPITEMIDLIST pidl = SHBrowseForFolder(&bi);
wchar_t path[MAX_PATH];
ZeroMemory(path, MAX_PATH);
SHGetPathFromIDList(pidl, path);
dir = QString::fromWCharArray(path);
CoTaskMemFree(pidl); //释放pIDList所指向内存空间;
static int CALLBACK BrowseCallbackProc(HWND hwnd, UINT uMsg, LPARAM lParam, LPARAM lpData)
{
switch (uMsg)
{
case BFFM_INITIALIZED: //初始化消息
{
wchar_t szDefaultDir[MAX_PATH] = “D:\\111\\”;
::SendMessage(hwnd, BFFM_SETSELECTION, TRUE, (LPARAM)szDefaultDir);
}
break;
case BFFM_SELCHANGED: //选择路径变化,
{
TCHAR curr[MAX_PATH];
SHGetPathFromIDList((LPCITEMIDLIST)lParam, curr);
QString dir = QString::fromWCharArray(curr);
QStringList listpath = dir.split("\\");
if(listpath.size() == 1 || (listpath.size() > 1 && listpath[1].isEmpty()))
::SendMessage(hwnd, BFFM_ENABLEOK, 0, 0);
else
::SendMessage(hwnd, BFFM_SETSTATUSTEXT, 0, (LPARAM)curr);
}
break;
default:
break;
}
return 0;
}
::SendMessage(hwnd, BFFM_ENABLEOK, 0, 0);的lparam设置为0则将确定按钮置灰
BFFM_ENABLEOK 启用或禁用“浏览文件夹”对话框的“确定”按钮。要启用OK按钮,请将此消息的lParam参数设置为非零值。要禁用OK按钮,请将此消息的lParam设置为零。
BFFM_SETSELECTION 选择指定的文件夹。要使用指向ITEMIDLIST结构(PIDL)的指针指定文件夹,请将此消息的lParam设置为PIDL,并将wParam设置为FALSE。要指定文件夹的路径,请将消息的lParam值设置为指向包含路径的以NULL结尾的字符串,并将wParam设置为TRUE。
BFFM_SETSTATUSTEXT 设置状态文本。将此消息的lParam值设置为指向包含所需文本的以NULL结尾的字符串。