MFC使用脚本记录:
①遍历本地选择文件夹并展示在edit control中:
void CMFCtestDlg::OnBnClickedBtnSeldir()
{
// TODO: 在此添加控件通知处理程序代码
TCHAR szFolderPath[MAX_PATH] = { 0 };
CString strFolderPath;
BROWSEINFO sInfo;
ZeroMemory(&sInfo, sizeof(BROWSEINFO));
sInfo.pidlRoot = 0;
sInfo.lpszTitle = _T("请选择一个文件夹:");
sInfo.ulFlags = BIF_DONTGOBELOWDOMAIN | BIF_RETURNONLYFSDIRS | BIF_EDITBOX;
sInfo.lpfn = NULL;
// 显示文件夹选择对话框
LPITEMIDLIST lpidlBrowse = SHBrowseForFolder(&sInfo);
if (lpidlBrowse != NULL)
{
// 取得文件夹名
if (SHGetPathFromIDList(lpidlBrowse, szFolderPath))
{
strFolderPath = szFolderPath;
getpath = strFolderPath;
}
}
if (lpidlBrowse != NULL)
{
CoTaskMemFree(lpidlBrowse);
}
GetDlgItem(IDC_EDIT1)->SetWindowTextW(strFolderPath);
}
CMFCtestDlg替换为自己的dlg类;IDC_EDIT1替换为你要展示路径的edit control的ID
②获取指定文件夹内的特定格式文件的路径,并添加在容器中
void CMFCtestDlg::GetFileName(CString format = _T("bmp"))
{
CString filepath = getpath;
CString filename = _T("");
CFileFind find;
BOOL IsFind = find.FindFile(filepath + _T("/*.") + format);
mv_imgFile.clear();
bool filesFound = false;
while (IsFind)
{
IsFind = find.FindNextFile();
if (find.IsDots())
{
continue;
}
if (!find.IsDirectory())
{
filename = find.GetFileName();
mv_imgFile.emplace_back(filepath + _T("\\") + filename);
filesFound = true;
}
}
// 检查是否有文件被添加到列表中
if (!filesFound)
{
MessageBox(_T("没有找到符合条件的文件。"), _T("文件搜索"), MB_ICONINFORMATION | MB_OK);
}
}
③将容器中的每个路径展示在一个edit control编辑控件上
void CMFCtestDlg::OnBnClickedButton2()
{
strResult = _T("");
GetFileName(); // 调用默认参数或传递特定格式
// 假设IDC_EDIT2是一个能够显示多行文本的编辑控件
for (const auto& file : mv_imgFile)
{
strResult += file + _T("\r\n");
}
GetDlgItem(IDC_EDIT2)->SetWindowTextW(strResult);
}
#################mv_imgFile :储存容器的路径###############
#################IDC_EDIT2 :展示控件的ID#################
④创建文件夹
//创建文件夹
void CMFCtestDlg::creatDir(const std::string& path)
{
DWORD ftyp = GetFileAttributesA(path.c_str());
if (ftyp == INVALID_FILE_ATTRIBUTES)
{
_mkdir(path.c_str());
}
}
⑤遍历路径的容器读取里面的图片,并用某个函数处理
for (const auto& path : vecPaths)
{
std::string imagePath = CT2A(path.GetString());
cv::Point center = processImage(imagePath, std::string(CT2A(outputFolder)));
std::string filename = imagePath.substr(imagePath.find_last_of("\\/") + 1);
if (center.x != -1)
{
outputFile << filename << "," << center.x << "," << center.y << "\n";
m_cameraPoints1.push_back(center);
}
}
outputFile.close();
processImage换成自己需要处理图片的函数