SOMS/test/XuAilibTest/BrowseForFolder.cpp
2024-07-15 10:31:26 +08:00

33 lines
1020 B
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#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;
}