mfc在listControl 中显示图标

本文介绍了如何在MFC的ListControl中显示系统图标,包括获取系统的小型和大型图标列表,附加到CImageList,然后添加到List Control。别忘了在完成后detach它们,否则系统图标会消失。文章提供了初始化图像列表的函数和获取图标ID的函数,并提到了Windows 95/98与NT4.0在获取图标上的差异,以及在析构函数中detach图例列表的重要性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 A   system   image   list   contains   each   file,   folder,   shortcut,   etc.   's   icon.     This   can   come       in   very   handy   for   creating   a   custom   explore   type   application.     The   first   step   consists     of   getting   the   small   and   large   (if   you   want   it)   image   lists   from   the   system   then   attaching     them   to   a   CImageList   and   then   adding   them     to   your   list   control.     Don't   forget   to   Detach()   them   after   you       are   done   or   else   all   the   icons   on   the   system   will   disappear   and   a   reboot   seems   to   be   the   only   fix.           Create   the   following   member   variables   in   you   list   control   class:       CImageList   m_smallImageList;       CImageList   m_largeImageList;           And   add   this   function:       void   CSystemListCtrl::InitializeSystemImageList()       {         //image   list   setup         HIMAGELIST     hSystemSmallImageList,   hSystemLargeImageList;         SHFILEINFO         ssfi,   lsfi;                 //get   a   handle   to   the   system   small   icon   list         hSystemSmallImageList   =           (HIMAGELIST)SHGetFileInfo(           (LPCTSTR)_T("C://"),           0,           &ssfi,           sizeof(SHFILEINFO),           SHGFI_SYSICONINDEX   |   SHGFI_SMALLICON);       //attach   it   to   the   small   image   list       //--DON'T   FORGET   TO   PUT   m_smallImageList.Detach();     in   your   destructor             m_smallImageList.Attach(hSystemSmallImageList);               //do   the   same   for   the   large         hSystemLargeImageList   =           (HIMAGELIST)SHGetFileInfo(           (LPCTSTR)_T("C://"),           0,           &lsfi,           sizeof(SHFILEINFO),           SHGFI_SYSICONINDEX   |   SHGFI_ICON);         m_largeImageList.Attach(hSystemLargeImageList);               //Set   the   list   control   image   list         SetImageList(&m_smallImageList,   LVSIL_SMALL);         SetImageList(&m_largeImageList,   LVSIL_NORMAL);           }     You   will   also   need   a   function   to   get   the   image   ID   for   each   item   you   would   like   to   dispaly     int   CSystemListCtrl::GetIconIndex(const   CString&   csFileName)     //full   path   and   file   name     {                                                   SHFILEINFO         sfi;                         SHGetFileInfo(                           (LPCTSTR)csFileName,                             0,                           &sfi,                             sizeof(SHFILEINFO),                             SHGFI_SYSICONINDEX   |   SHGFI_SMALLICON   );                         return   sfi.iIcon;     }     Feed   this   result   back   into   you   ListCtrl   LV_ITEM   struct   and   you   are   on   your   way.     Note:     Windows   95/98   returns   a   full   image   list   upfront   where   as   NT4.0   only   will   retreive   the   icons   as   you   ask   for   them   in   the   future.         "If   you   do   an   imagecount   on   the   imagelist   right   after   you   get   the   handle,   you'll   see   the   smaller   imagelist.     Only   until   you   iterate   throught   the   various   documents   are   they   added   to   the   NT   imagelist."   -   Gil   Rosin    

第二次显示图标时无法显示是因为缺少以下代码 CImportDlg::~CImportDlg(){  m_imglist.Detach();}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值