Attaching a system imagelist to a list control
Posted
by Matt Esterly
on August 6th, 1998
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
Thanks to Robert Edward Caldecott and Gil Rosin for pointing this out!

Comments
How to get path of file that display in list
Posted by Legacy on 09/01/2003 12:00amOriginally posted by: vx700
Hi
If in the reverse way,to get a filepath from list that show a file as icon, Is it have a way to do that.
Pls,give me some suggestion
thanks
Reply
Very Useful!
Posted by Legacy on 08/26/2002 12:00amOriginally posted by: Jerry
It really very useful.
ReplyIMAGE LIST NIGHTMARE!!
Posted by Legacy on 10/04/2001 12:00amOriginally posted by: James Hardacre "Newbie"
ReplySimple way to keep icons for disappearing
Posted by Legacy on 06/13/2001 12:00amOriginally posted by: Scott H. Pultz
If you don't want your icons to disappear, just set the LVS_SHAREIMAGELISTS style on the list control:
DWORD GetListViewStyle(HWND hListView)
{
return GetWindowLong(hListView, GWL_STYLE);
}
void SetListViewStyle(HWND hListView, DWORD dwStyle)
{
SetWindowLong(hListView, GWL_STYLE, dwStyle);
}
DWORD dwCurrentStyle=GetListViewStyle(hListCtrl);
SetListViewStyle(hListCtrl, dwCurrentStyle | LVS_SHAREIMAGELISTS);
That's it!
ReplySystem Image list
Posted by Legacy on 04/12/2000 12:00amOriginally posted by: Vishali Karnik
Reply
I added icon from system icon list using SHGetFileInfo, but this is terrible slow!!
Posted by Legacy on 03/07/2000 12:00amOriginally posted by: M�tiu Ioan Angelo
I tried to do a simple Explorer and I use a CListCtrl. Image list was taken from image system list using SHGetFileInfo function (of course passing name of file ).
ReplyPreviously the system list image was shared with list control.
But for a folder with up to 2000 files ( KitNT40 - precisely) getting image index and fill list is terrible slow ( 9-10 sec.). I wrote a little profiler using QueryPerformanceCounter() function and most time is spend in SHGetFileInfo() function for getting icon index.
I look at ACDSEE32 application and there the some thing is perfectfully O.K. - it takes 1 sec and interesting, I don't belive that use a separate thread.( Maybe I'm wrong ...)
What trick I can use?
Plese help me if you can. Thanks a lot for your time !
( And excuse my bad english ...)
Read this if your stupid Icons disappear!!
Posted by Legacy on 03/04/2000 12:00amOriginally posted by: Nathan Moinvaziri
ReplyThe problem still exists after I tried all the ways in this section.
Posted by Legacy on 02/20/2000 12:00amOriginally posted by: Allen
ReplyPrevent system image list from being destroyed
Posted by Legacy on 02/04/2000 12:00amOriginally posted by: Elad Lahav
The trick here appears to be quite simple. Just add the LVS_SHAREIMAGELISTS flag to the list view style, or mark the "Share Image List" check box in the resource editor.
ReplyDoing so tells the list control it does not own the image list, and therefore should not destroy it.
This method works with either the Attach()-Detach() or the FromHandle() approaches.
Icons dissapear always..
Posted by Legacy on 01/11/2000 12:00amOriginally posted by: Dusty Murray
I am using a application genrated met the appwizzard from vc6.0 and my assumptions are that the Inialisation should be in the create method (see code fragment) and the release should be in the ondestroy from the listview class..
but whatever i try to do after my application quits, all my system icons disapear, here in this codesample i am using the fromhandle, but whith attach the problem is exact the same,
I hope someone has an idea?
_code fragment--
BOOL CSoftwareCatView::Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext)
{
BOOL rv;
rv = CWnd::Create(lpszClassName, lpszWindowName, dwStyle, rect, pParentWnd, nID, pContext);
GetSysImgList();
return rv;
}
/////////////////////////////////////////////////
BOOL CSoftwareCatView::GetSysImgList()
/////////////////////////////////////////////////
{
// get handle to the System's Image List
HIMAGELIST hSysImageList;
SHFILEINFO shellinfo;
hSysImageList = (HIMAGELIST) SHGetFileInfo (_T("C:\\"),
0,&shellinfo,sizeof(shellinfo),
SHGFI_SYSICONINDEX | SHGFI_SMALLICON);
ASSERT (hSysImageList);
m_imgList = CImageList::FromHandle(hSysImageList);
ASSERT (m_imgList);
GetListCtrl().SetImageList(m_imgList, LVSIL_SMALL);
return TRUE;
}
void CSoftwareCatView::OnDestroy()
Reply{
m_imgList->Detach();
CListView::OnDestroy();
}
Loading, Please Wait ...