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

被折叠的 条评论
为什么被折叠?



