Displaying Icons from Shell32.dll in TreeView

3 posts, 1 answers
  1. Civbaron
    Civbaron avatar
    20 posts
    Member since:
    Jan 2010

    Posted 30 Aug 2010 Link to this post


    I am creating a windows explorer type application using WPF, and want to use the icons that are within the Shell32.dll so that it matches what is in windows explorer. I have tried several different methods assigning the icon to the radtreeviewitem and none have worked.

    Here is an excerpt from my code I am first checking to see if an image is contained in the imagelist before adding a new image to the list:

                        if (images.Images["Folders"] == null)
                            images.Images.Add("Folders", (System.Drawing.Icon)GetIcon(drive.Name, 0));
                        item.DefaultImageSrc = "Folders";

    Here is the getIcon method:
            private object GetIcon(string path, int openclose)
                IntPtr hImgSmall;
                SHFILEINFO shinfo = new SHFILEINFO();

                hImgSmall = Win32.SHGetFileInfo(path, 0, ref shinfo, (uint)Marshal.SizeOf(shinfo), Win32.SHGFI_ICON | Win32.SHGFI_SMALLICON);

                return System.Drawing.Icon.FromHandle(shinfo.hIcon);

    I am not sure if this is even possible using the radtreeview of WPF. I moved this code into a program that was built using winforms and it works perfectly.

  2. Answer
    Kiril Stanoev
    Kiril Stanoev avatar
    1512 posts

    Posted 02 Sep 2010 Link to this post

    Hi Jonathan,

    I did a little research and it appears that extracting the icons is not as trivial as it might seem. I've prepared a small sample that populates RadTreeView with nodes and each node has an icon of a folder that comes nativly from Windows. I read several articles to understand how to retrieve for example the icon for a folder or a hard drive. Then, having the icon (System.Drawing.Icon object) I had to convert it to a BitmapImage so that the Image control in WPF understands it. I am attaching my sample project and I hope it will help you accomplish your scenario. Additionally, please have a look at these articles for further information:

    WPF - Assigning Icon to Image control

    Icon.ExtractAssociatedIcon Method

    shgetfileinfo (shell32)

    Need Working Example of Shell32's ExtractAssociatedIcon Function in .NET

    Retrieving shell icons

    How do I fetch the folder icon on Windows 7 using Shell32.SHGetFileInfo

    Sincerely yours,
    Kiril Stanoev
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  3. Civbaron
    Civbaron avatar
    20 posts
    Member since:
    Jan 2010

    Posted 03 Sep 2010 Link to this post

    Hi Kiril ,

    Yes this does help me a great deal.

    thank you,
Back to Top