mfc CFileFind查找类

 查找文件
 CFileFind类
 提取文件图标
 显示大图标
 显示小图标
 

 一、查找文件
  1、 CFileFind类
 //c:mydirmyfile.txt
GetFileName
获取文件名 myfile.txt 
GetFilePath
获取文件路径c:mydirmyfile.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.


 二、提取文件图标
WINSHELLAPI DWORD WINAPI SHGetFileInfo(
    LPCTSTR pszPath, //文件路径
    DWORD dwFileAttributes, //文件属性 忽略
    SHFILEINFO FAR *psfi,//返回文件信息
    UINT cbFileInfo, //SHFILEINFO 结构大小
    UINT uFlags //文件标志 SHGFI_ICON 
);
三、代码测试
CListCtrl* m_list1=(CListCtrl*)GetDlgItem(IDC_LIST1);          
         
        int indeximage;
         imagelistb.Create(32,32,ILC_COLOR32|ILC_MASK,0,0);//创建图标列表        
            //imagelistb.Add(AfxGetApp()->LoadIcon(IDR_MAINFRAME));        
         
            CFileFind   cFindFiles;
            WCHAR szFileName[256];
            WCHAR found[200];
             swprintf(szFileName,L"%s//*.* ",L"C:\");
             BOOL bFound=cFindFiles.FindFile(L"C:\//*.*");
            // 
            while(bFound)//这个循环是枚举文件的
            {
                bFound=(BOOL)cFindFiles.FindNextFile();
                 if(!cFindFiles.IsDirectory())
                 {
                    wcscpy(found,cFindFiles.GetFilePath().GetBuffer(200));
                    SHFILEINFO shfi;
                    memset(&shfi,0,sizeof(shfi));
                    SHGetFileInfo(found, 
                        0,
                        &shfi, sizeof(shfi),
                        SHGFI_ICON  );  //获取文件信息,含图标的
                    indeximage=imagelistb.Add( shfi.hIcon);//向ImageList里面添加图标shfi.hIcon
                     
                    int nCount=m_list1->GetItemCount();
                    m_list1->InsertItem(nCount,found,nCount);
                     
                 }
            }  
            
              m_list1->SetImageList(&imagelistb,LVSIL_NORMAL); //设置图标
              m_list1->SetImageList(&imagelistb,LVSIL_SMALL);
        

int CDialog_ListCtrl_Test::OnInitDialog(void)
{
    //添加项目
    CListCtrl* plst_ctl=(CListCtrl*)GetDlgItem(IDC_LIST1);
    //添加图标
    m_imagelist_b.Create(32,32,ILC_COLOR32|ILC_MASK ,0,0);
    m_imagelist_s.Create(16,16,ILC_COLOR32|ILC_MASK ,0,0);
    plst_ctl->SetImageList(&m_imagelist_b,LVSIL_NORMAL   );//大图标
    plst_ctl->SetImageList(&m_imagelist_s,LVSIL_SMALL   );
    
    /*plst_ctl->InsertItem(0,L"0000",0);
    plst_ctl->InsertItem(1,L"1111",0);
    plst_ctl->InsertItem(2,L"2222");
    plst_ctl->InsertItem(3,L"aaaaa");
    plst_ctl->SetItemText(3,0,L"33333");*/
    CFileFind findfile;
    int nfound=findfile.FindFile(L"C:\Windows\//*.*");
    int i=0;
     while(nfound)
     {
         nfound=findfile.FindNextFile();
         if (findfile.IsDirectory())
         {
             continue;
         }
         SHFILEINFO finfo;
         //获取文件信息,主要图标icon
         SHGetFileInfo(findfile.GetFilePath(),0,&finfo,sizeof(finfo),SHGFI_ICON );
         //添加文件项目和图标
         m_imagelist_s.Add(finfo.hIcon);//向ImageList里添加图标资源
         plst_ctl->InsertItem(i++,findfile.GetFileName(), m_imagelist_b.Add(finfo.hIcon));
     }
    findfile.Close();//释放资源
    return 0;
}
原文地址:https://www.cnblogs.com/whzym111/p/6222306.html