首先,在你的列表控件類里添加下面兩個數據成員:
CImageList m_smallImageList;
CImageList m_largeImageList;
接著,添加這個函數:
void CSystemListCtrl::InitializeSystemImageList()
{
//建立圖象列表
HIMAGELIST hSystemSmallImageList, hSystemLargeImageList;
SHFILEINFO ssfi, lsfi;
//獲得指向系統小圖標的句柄
hSystemSmallImageList = (HIMAGELIST)SHGetFileInfo( (LPCTSTR)_T("C:\\"), 0,
&ssfi, sizeof(SHFILEINFO), SHGFI_SYSICONINDEX | SHGFI_SMALLICON);
//把它分配給小圖象列表
//不要忘了在你的析構函數里執行m_smallImageList.Detach()操作
m_smallImageList.Attach(hSystemSmallImageList);
//對大圖標做同樣的工作
hSystemLargeImageList = (HIMAGELIST)SHGetFileInfo( (LPCTSTR)_T("C:\\"),
0, &lsfi, sizeof(SHFILEINFO), SHGFI_SYSICONINDEX | SHGFI_ICON);
m_largeImageList.Attach(hSystemLargeImageList);
//給列表控件設置圖象列表
SetImageList(&m_smallImageList, LVSIL_SMALL);
SetImageList(&m_largeImageList, LVSIL_NORMAL);
}
同時,你需要下面這個函數來取得你要顯示的每一項的圖標的ID。
int CSystemListCtrl::GetIconIndex(const CString& csFileName)
//完整的路徑以及文件名
{
SHFILEINFO sfi;
SHGetFileInfo((LPCTSTR)csFileName,0,&sfi,sizeof(SHFILEINFO),
SHGFI_SYSICONINDEX | SHGFI_SMALLICON );
return sfi.iIcon;
}
延伸閱讀
文章來源于領測軟件測試網 http://www.kjueaiud.com/