Elan
Elan

Reputation: 6376

How do I determine the icon index for Desktop and Network for use in SHGetImageList?

I am able to successfully extract the icons for file system drives, folders and files using the APIs I included below. Additional info on the DLL imports etc. that helped me get this far can be found here. By calling the method GetExtraLargeIconForFolder I get a 48x48 sized image in the icon.

public enum ImageListIconSize : int
{
    Large = 0x0,
    Small = 0x1,
    ExtraLarge = 0x2,
    Jumbo = 0x4
}

private static IImageList GetSystemImageListHandle(ImageListIconSize size)
{
    IImageList iImageList;
    Guid imageListGuid = new Guid("46EB5926-582E-4017-9FDF-E8998DAA0950");
    int ret = SHGetImageList(
        (int)size,
        ref imageListGuid,
        out iImageList
        );
    return iImageList;
}

public static Icon GetExtraLargeIconForFolder(string path)
{
    SHFILEINFO shinfo = new SHFILEINFO();
    IntPtr retVal = SHGetFileInfo(
        path, 0, ref shinfo, (uint)Marshal.SizeOf(shinfo),
        (int)(SHGetFileInfoConstants.SHGFI_SYSICONINDEX |
              SHGetFileInfoConstants.SHGFI_ICON));

    int iconIndex = shinfo.iIcon;
    IImageList iImageList =
        (IImageList)GetSystemImageListHandle(ImageListIconSize.ExtraLarge);
    IntPtr hIcon = IntPtr.Zero;
    if (iImageList != null)
    {
        iImageList.GetIcon(iconIndex,
            (int)ImageListDrawItemConstants.ILD_TRANSPARENT, ref hIcon);
    }

    Icon icon = null;
    if (hIcon != IntPtr.Zero)
    {
        icon = Icon.FromHandle(hIcon).Clone() as Icon;
        DestroyIcon(shinfo.hIcon);
    }
    return icon;
}

In Windows Explorer one sees, icons for the Desktop, Network and Computer. How does one go about getting the correct icon index for these file system nodes?

Upvotes: 1

Views: 2318

Answers (1)

David Heffernan
David Heffernan

Reputation: 613392

You are nearly there. You still use SHGetFileInfo but instead you will need to pass SHGFI_PIDL in the flags parameter.

Then you need to specify the shell object of interest by passing a PIDL rather than a path. Obtain the PIDL by calling SHGetSpecialFolderLocation. Pass a CSIDL value to this routine, e.g. CSIDL_DESKTOP, CSIDL_DRIVES, CSIDL_NETWORK etc.

Upvotes: 3

Related Questions