Attaching a system imagelist to a list control

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

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:00am

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

    Originally posted by: Jerry

    It really very useful.

    Reply
  • IMAGE LIST NIGHTMARE!!

    Posted by Legacy on 10/04/2001 12: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;
    il.Create(IDB_BITMAP,18,1,CLR_NONE);
    m_List_View.SetImageList(&il,LVSIL_SMALL);

    //Jims Fill list view stuff
    m_List_View.InsertColumn(0,(CString)"1",LVCFMT_CENTER,100);

    m_List_View.InsertItem(0,(CString)"1",0);
    m_List_View.InsertItem(1,(CString)"2",1);
    m_List_View.InsertItem(3,(CString)"3",2);
    m_List_View.InsertItem(2,(CString)"4",0);
    m_List_View.InsertItem(4,(CString)"5",0);


    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 .....

    james@jameshardacre.com

    Reply
  • Simple way to keep icons for disappearing

    Posted by Legacy on 06/13/2001 12: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!

    Reply
  • System Image list

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

    Originally posted by: Vishali Karnik

    Hi,
    
    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?

    Thanks
    Vishali.

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

    Posted by Legacy on 03/07/2000 12: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 ...)

    Reply
  • Read this if your stupid Icons disappear!!

    Posted by Legacy on 03/04/2000 12: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);
    m_imgList.Detach();

    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
    nouser@home.com

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

    Posted by Legacy on 02/20/2000 12: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 !

    Reply
  • Prevent system image list from being destroyed

    Posted by Legacy on 02/04/2000 12: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.

    Reply
  • Icons dissapear always..

    Posted by Legacy on 01/11/2000 12: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);
    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()
    {
    m_imgList->Detach();
    CListView::OnDestroy();
    }

    Reply
  • 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.

Top White Papers and Webcasts

  • Live Event Date: August 20, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT When you look at natural user interfaces as a developer, it isn't just fun and games. There are some very serious, real-world usage models of how things can help make the world a better place – things like Intel® RealSense™ technology. Check out this upcoming eSeminar and join the panel of experts, both from inside and outside of Intel, as they discuss how natural user interfaces will likely be getting adopted in a wide variety …

  • Savvy enterprises are discovering that the cloud holds the power to transform IT processes and support business objectives. IT departments can use the cloud to redefine the continuum of development and operations—a process that is becoming known as DevOps. Download the Executive Brief DevOps: Why IT Operations Managers Should Care About the Cloud—prepared by Frost & Sullivan and sponsored by IBM—to learn how IBM SmartCloud Application services provide a robust platform that streamlines …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds