Shell icons
-
Dear all, If for example I am trying to make a custom folder explorer (to display PSP film names correctly) and I am using a TreeView to display the folder hierachy. Is it possible to access the system icons for example the currently used Folder icon from the Shell to use?? Many thanks Paul ps VS2005 and C#
-
Dear all, If for example I am trying to make a custom folder explorer (to display PSP film names correctly) and I am using a TreeView to display the folder hierachy. Is it possible to access the system icons for example the currently used Folder icon from the Shell to use?? Many thanks Paul ps VS2005 and C#
for getting file icons you need to use shell APIs. you can use the following snippet.
// define a struct for getting info from shell
[StructLayout(LayoutKind.Sequential)]
public struct SHFILEINFO
{
public IntPtr hIcon;
public IntPtr iIcon;
public uint dwAttributes;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
public string szDisplayName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]
public string szTypeName;
};// define this class for invoking shell api to get file information
class Win32
{
public const uint SHGFI_ICON = 0x100;
public const uint SHGFI_LARGEICON = 0x0; // 'Large icon
public const uint SHGFI_SMALLICON = 0x1; // 'Small icon\[DllImport("shell32.dll")\] public static extern IntPtr SHGetFileInfo(string pszPath, uint dwFileAttributes, ref SHFILEINFO psfi, uint cbSizeFileInfo, uint uFlags);
}
// write the following code where you want to get the selected file icon.
IntPtr hImgSmall; //the handle to the system image list
string fName = "c:\\getfileicon.cs"; // 'the file name to get icon from
SHFILEINFO shinfo = new SHFILEINFO();//Use this to get the small Icon
hImgSmall = Win32.SHGetFileInfo(fName, 0, ref shinfo,(uint)Marshal.SizeOf(shinfo),Win32.SHGFI_ICON |Win32.SHGFI_SMALLICON);/*
//Use this to get the small Icon
hImgLarge = Win32.SHGetFileInfo(fName, 0, ref shinfo,(uint)Marshal.SizeOf(shinfo),Win32.SHGFI_ICON |Win32.SHGFI_LARGEICON);
*///The icon is returned in the hIcon member of the shinfo struct
System.Drawing.Icon myIcon = System.Drawing.Icon.FromHandle(shinfo.hIcon);// do whatever with this icon. i.e I am adding it to an Image List.
imageList1.Images.Add(myIcon);I think this code would be helpful.
Saqib