一、查找文件
CFileFind类:
例://c:\mydir\myfile.txt
GetFileName | 获取文件名 myfile.txt | ||
GetFilePath | 获取文件路径c:\mydir\myfile.txt | ||
GetFileTitle | 获取文件标题 myfile | ||
Close | 关掉搜索请求,释放掉占用资源 |
| |
FindFile | 搜索目录,成功返回非零值,否则返回0 |
| |
FindNextFile | 继FindFile后查找下一个文件,最后一个文件时返回 |
|
IsReadOnly | Determines if the found file is read-only. |
IsDirectory | Determines if the found file is a directory. |
IsCompressed | Determines if the found file is compressed. |
IsSystem | Determines if the found file is a system file. |
IsHidden | Determines if the found file is hidden. |
IsTemporary | Determines if the found file is temporary. |
二、提取文件图标
WINSHELLAPIDWORD WINAPI SHGetFileInfo(
LPCTSTR pszPath, //文件路径
DWORD dwFileAttributes, //文件属性 忽略
SHFILEINFO FAR *psfi,//返回文件信息
UINT cbFileInfo, //SHFILEINFO 结构大小
UINT uFlags //文件标志 SHGFI_ICON
);
三、示例代码
CListCtrl* plst_ctl = (CListCtrl*)GetDlgItem(IDC_LIST2);
//添加图标
m_imagelist_b.Create(32, 32, ILC_COLOR32 | ILC_MASK, 0, 0);
CFileFind findfile;
int nfound = findfile.FindFile(L"C:\\Windows\\//*.*");//C:\\Windows\\为Windows目录下的
int i = 0;
while (nfound)
{
nfound = findfile.FindNextFileW();
if (findfile.IsDirectory())
{
continue;
}
SHFILEINFO finfo;
//获取文件信息,主要是图标icon
SHGetFileInfo(findfile.GetFilePath(), 0, &finfo, sizeof(finfo), SHGFI_ICON);
plst_ctl->InsertItem(i++, findfile.GetFileName(), m_imagelist_b.Add(finfo.hIcon));
}
plst_ctl->SetImageList(&m_imagelist_b, LVSIL_NORMAL);//大图标,若要小图标,则在定义一个CImageList变量,create、add
findfile.Close();//释放资源
此处和
CImageList类、List Control控件结合起来使用。