C++ 打开选择文件夹对话框选择目录的操作
时间:2021-08-30 10:03:24|栏目:C代码|点击: 次
需要添加头文件:
#ifdef WIN32 #include <shlobj.h> #endif
调用文件对话框选择目录:
// 选择目录 void CustomInstalWndViewCtrl::OnSigBtnSelectInstallDir() { BROWSEINFO bi; bi.hwndOwner = NULL; bi.pidlRoot = CSIDL_DESKTOP;//文件夹的根目录,此处为桌面 bi.pszDisplayName = NULL; bi.lpszTitle = NULL;//显示位于对话框左上部的提示信息 bi.ulFlags = BIF_DONTGOBELOWDOMAIN | BIF_RETURNONLYFSDIRS | BIF_NEWDIALOGSTYLE;//有新建文件夹按钮 bi.lpfn = NULL; bi.iImage = 0; LPITEMIDLIST pidl = SHBrowseForFolder(&bi);//调用选择对话框 if (pidl == NULL) { std::cout << "没有选择目录" << std::endl; return; } TCHAR strFolder[MAX_PATH]; SHGetPathFromIDList(pidl, strFolder); std::string sFolder = StringUtil::WStringToString(strFolder); }
补充:c++ MFC选择文件夹
void openDir(){ //选择文件夹 string _Path,root; TCHAR szDir[MAX_PATH]; BROWSEINFO bi; ITEMIDLIST *pidl; bi.hwndOwner = this->m_hWnd; bi.pidlRoot = NULL; bi.pszDisplayName = szDir;//这个是输出缓冲区 bi.lpszTitle = _T("文件夹:"); //标题 bi.ulFlags = BIF_NEWDIALOGSTYLE;//使用新的界面,在win7中效果较好//BIF_RETURNONLYFSDIRS; bi.lpfn = NULL; bi.lParam = 0; bi.iImage = 0; pidl = SHBrowseForFolder(&bi);//弹出对话框 if (pidl == NULL)//点了取消,或者选择了无效的文件夹则返回NULL return; if (SHGetPathFromIDList(pidl, szDir)) { int length = WideCharToMultiByte(CP_ACP, 0, szDir, -1, NULL, 0, NULL, NULL); char ch[] = ""; WideCharToMultiByte(CP_ACP, 0, szDir, -1, ch, length, NULL, NULL); root = string(ch); } }