选择文件对话框
CFileDialog dlg(TRUE, NULL, mImagePath, OFN_HIDEREADONLY | OFN_FILEMUSTEXIST,
_T("JPG Files (*.jpg)|*.jpg|BMP Files (*.bmp)|*.bmp|REF Files (*.ref)|*.ref|"), NULL);
dlg.m_ofn.lpstrTitle = mImagePath;
INT_PTR result = dlg.DoModal();
if (result == IDOK)
{
CString pathName;
pathName = dlg.GetPathName();//获取文件名-包含路径
pathName = pathName.Left(pathName.ReverseFind('\\'));//去掉文件名
}
选择文件对话框
BROWSEINFO bi;
ZeroMemory(&bi, sizeof(bi));
bi.lpfn = BrowseCallbackProc;
bi.lpszTitle = _T(""); // 可以不指定
// bi.ulFlags = BIF_EDITBOX | BIF_RETURNONLYFSDIRS;
// 打开对话框, 有点像DoModal
LPITEMIDLIST targetLocation = SHBrowseForFolder(&bi);
if (targetLocation != NULL) {
m_ListBoxScan.ResetContent();
TCHAR targetPath[MAX_PATH];
SHGetPathFromIDList(targetLocation, targetPath);
sprintf_s(mImagePath, "%s", targetPath);
}
int CALLBACK BrowseCallbackProc(HWND hwnd, UINT uMsg, LPARAM /*lParam*/, LPARAM lpData)
{
if (uMsg == BFFM_INITIALIZED)
{
//(LPARAM)"D:\\remote"--指定初始目录
SendMessage(hwnd, BFFM_SETSELECTION, (WPARAM)TRUE,(LPARAM)"D:\\remote");
}
return 0;
}
遍历文件 文件夹
vector<string> mVtImagePath;
char TempPath[MAX_PATH];
char path[MAX_PATH];
sprintf_s(TempPath, "%s\\", mImagePath);//mImagePath文件夹路径
sprintf_s(path, "%s\\*.*", mImagePath);
WIN32_FIND_DATA FindFileData;
HANDLE hFind = ::FindFirstFile(path, &FindFileData);
if (INVALID_HANDLE_VALUE == hFind) return false;
while (true)
{
if (FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
if (FindFileData.cFileName[0] != '.')
{
//添加文件夹路径
str = TempPath;
str.append(FindFileData.cFileName);
mVtImagePath.push_back(str);
}
}
else
{
str = FindFileData.cFileName;
//添加文件 jpg bmp
if (str.find("jpg") != str.npos || str.find("bmp") != str.npos)
{
//只添加文件名中包含E的文件
if (str.find("E") != str.npos)
{
str = TempPath;
str.append(FindFileData.cFileName);
mVtImagePath.push_back(str);
}
}
}
if (!FindNextFile(hFind, &FindFileData)) break;
}
FindClose(hFind);