123456789101112131415161718192021222324252627282930313233 |
- #include "BrowseForFolder.h"
- #include <Windows.h>
- #include <ShlObj.h>
- #include <string>
- #include <Commdlg.h>
- using namespace std;
- int BrowseForFolder::SelectFile(string& path)
- {
- TCHAR szBuffer[MAX_PATH] = { 0 }; //存放选择文件的路径
- BROWSEINFO bi;
- ZeroMemory(&bi, sizeof(BROWSEINFO));
- bi.hwndOwner = NULL;
- bi.pszDisplayName = szBuffer;
- bi.lpszTitle = _T("从下面选择文件或文件夹:"); //_T()是在头文件tchar.h下的一个宏定义。
- bi.ulFlags = BIF_BROWSEINCLUDEFILES;
- LPITEMIDLIST idl = SHBrowseForFolder(&bi); //开始选择文件或文件夹
- if (NULL == idl)
- {
- return 0;
- }
- SHGetPathFromIDList(idl, szBuffer); //获取完整路径,否则szBuffer只会存储当前选择的文件或文件夹名称
- path = TCHAR2STRING(szBuffer);
- return 0;
- }
- std::string BrowseForFolder::TCHAR2STRING(TCHAR* STR)
- {
- int iLen = WideCharToMultiByte(CP_ACP, 0, STR, -1, NULL, 0, NULL, NULL);
- char* chRtn = new char[iLen * sizeof(char)];
- WideCharToMultiByte(CP_ACP, 0, STR, -1, chRtn, iLen, NULL, NULL);
- std::string str(chRtn);
- delete chRtn;
- return str;
- }
|