基于C++的图片文件路径设置及扫描规定格式文件

来源:互联网 发布:软件职业技能培训学校 编辑:程序博客网 时间:2024/04/29 15:25
ImageClass.h文件typedef struct FileMessage{ CString filename[100];    //文件信息 _In_ int filenum  ;    //文件数目}filemessage;class ImageObject : public CObject{public:ImageObject();~ImageObject();public:         //设置文件所在路径 void SetRetrievalPath(CString &strfile1);                 //扫描当前目录中的图像个数并获得文件名            FileMessage GetFileNameAndNumber(const CString &strfile, const CString &form);                                             };ImageClass.CPP文件#include "ImageClass.h"#include "Dib.h"#include "atlbase.h"#include "atlimage.h"  #include "string"#include "TypeConversion.h"//功能: 设置文件所在路径//strfile1:返回的文件路径//:status:finfishvoid ImageObject::SetRetrievalPath(CString &strfile1){ CString m_strPath;    //检索库路径 bool dir;//设置检索库标志位 BROWSEINFO browse; ZeroMemory(&browse, sizeof(browse));       //fills a block of memory with zeros. browse.hwndOwner = NULL; browse.pszDisplayName = m_strPath.GetBuffer(MAX_PATH); browse.lpszTitle = "请选择一个目录";//SHBrowseForFolder函数返回一个ITEMIDLIST结构的指针,包含了用户选择文件夹的信息 LPITEMIDLIST lpItem = SHBrowseForFolder(&browse); if (lpItem == NULL) return; m_strPath.ReleaseBuffer();//SHGetPathFromIDList把项目标志符列表转换为文档系统路径 if (SHGetPathFromIDList(lpItem, m_strPath.GetBuffer(MAX_PATH)) == false) return; m_strPath.ReleaseBuffer(); dir = true; //标志位设置为true,表示待检索文件已设置//得到目录下文件的路径 m_strPath.TrimRight(); m_strPath.TrimLeft(); //去除前后多余 strfile1 = m_strPath;}//扫描当前目录中的图像个数并获得文件名//strfile:文件夹路径//form:文件格式//FileMessage:filename[tempi]:文件名//filenum:文件个数//:status:finfishFileMessage ImageObject::GetFileNameAndNumber(const CString &strfile, const CString &form){ int tempi = 0;//临时的检索库图像计数器 FileMessage temp;//检索库中图像路径 temp.filenum = 0; BOOL yesno; CFileFind find; char tempFileFind[200]; sprintf_s(tempFileFind, "%s\\*." + form, strfile); yesno = find.FindFile(tempFileFind); //在当前目录下查找form文件 while (yesno) { yesno = find.FindNextFile(); char foundFileName[200];//临时存储查找到的图像名 strcpy_s(foundFileName, find.GetFileName().GetBuffer(200));//获取图像名  if (!find.IsDots()) //过滤缺省目录  {  char tempFileName[200];  sprintf_s(tempFileName, "%s\\%s", strfile, foundFileName);  CString strfilepath1;  strfilepath1.Format("%s", tempFileName);//获取图像完整路径  temp.filenum++;  temp.filename[tempi] = strfilepath1;//保存图像完整路径  tempi++;  } }  find.Close();  return temp;}

1 0
原创粉丝点击