This is a migrated thread and some comments may be shown as answers.

Displaying Icons from Shell32.dll in TreeView

2 Answers 835 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Civbaron
Top achievements
Rank 1
Civbaron asked on 31 Aug 2010, 01:01 AM
Hi,

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.

Thanks,
Jonathan

2 Answers, 1 is accepted

Sort by
0
Accepted
Kiril Stanoev
Telerik team
answered on 02 Sep 2010, 12:52 PM
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
0
Civbaron
Top achievements
Rank 1
answered on 03 Sep 2010, 10:06 PM
Hi Kiril ,

Yes this does help me a great deal.

thank you,
Jonathan
Tags
TreeView
Asked by
Civbaron
Top achievements
Rank 1
Answers by
Kiril Stanoev
Telerik team
Civbaron
Top achievements
Rank 1
Share this question
or