Attaching a system imagelist to a list control


Desktop-as-a-Service Designed for Any Cloud ? Nutanix Frame

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;


        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!    


  • How to get path of file that display in list

    Posted by Legacy on 09/01/2003 07:00am

    Originally posted by: vx700

    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

  • Very Useful!

    Posted by Legacy on 08/26/2002 07:00am

    Originally posted by: Jerry

    It really very useful.


    Posted by Legacy on 10/04/2001 07:00am

    Originally posted by: James Hardacre "Newbie"

    Im trying to allocate images to a list view ... But I cant get it to work.

    Can Anyone help?

    This code is in my "OnInitDialog"

    //Jims SetupImage list
    CImageList il;

    //Jims Fill list view stuff


    My bitmap "IDB_BITMAP" is 90 pixles wide with numbers DRAWN on the relevent sections, just for reference purposes. So I presume it cuts them up correctly.

    It LOOKS like there are images as the date on the line has a space before it..... but there is nothing there!!!

    Please help .... AAAGGGHHH!!!!

    Mail mail for the code .....


  • Simple way to keep icons for disappearing

    Posted by Legacy on 06/13/2001 07:00am

    Originally 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!

  • System Image list

    Posted by Legacy on 04/12/2000 07:00am

    Originally posted by: Vishali Karnik

    I have an explorer like application in which I have to populate the list control. I am using system list for the list control. But the icons for the machines under Network Neighborhood takes a long long time. So I thought of hardcoding the image number which is 5 so that it does not have to look for the image number in the list. But this doesn't seem to work. It displays blank space instead of the icon.

    Can anyone throw some light on this?


  • I added icon from system icon list using SHGetFileInfo, but this is terrible slow!!

    Posted by Legacy on 03/07/2000 08:00am

    Originally 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 ).
    Previously 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 08:00am

    Originally posted by: Nathan Moinvaziri

    Try using this:

    SHFILEINFO shellinfo;
    CImageList m_imgList;
    CListCtrl m_ListCtrl;

    m_imgList.Attach((HIMAGELIST)SHGetFileInfo ("C:\\"),
    0,&shellinfo,sizeof(shellinfo), SHGFI_SYSICONINDEX | SHGFI_SMALLICON));

    m_ListCtrl.SetImageList(m_imgList, LVSIL_SMALL);

    The main reason why it probably didn't work before was that you were putting the SystemImageList in a HIMAGELIST, instead of doing that you can directly attach it and detach it to a CImageList control without going through the use of a HIMAGELIST. It's not like you can detach the HIMAGELIST from the system image list. Well,.. there probably is a way, but takes to much time. :)

    I think this is what I have in the alternative example posted on here.

    Hope this helps.

    Nathan Moinvaziri

  • The problem still exists after I tried all the ways in this section.

    Posted by Legacy on 02/20/2000 08:00am

    Originally posted by: Allen

    The problem all sys icons dissapear still exists after I tried all the ways in this section.
    Please help me on the problem.
    Thank you very much !

  • Prevent system image list from being destroyed

    Posted by Legacy on 02/04/2000 08:00am

    Originally 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.
    Doing 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 08:00am

    Originally 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);
    return rv;

    BOOL CSoftwareCatView::GetSysImgList()
    // get handle to the System's Image List

    HIMAGELIST hSysImageList;
    SHFILEINFO shellinfo;

    hSysImageList = (HIMAGELIST) SHGetFileInfo (_T("C:\\"),

    ASSERT (hSysImageList);
    m_imgList = CImageList::FromHandle(hSysImageList);
    ASSERT (m_imgList);

    GetListCtrl().SetImageList(m_imgList, LVSIL_SMALL);
    return TRUE;

    void CSoftwareCatView::OnDestroy()

  • Loading, Please Wait ...

  • You must have javascript enabled in order to post comments.

Leave a Comment
  • Your email address will not be published. All fields are required.

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date