Tuesday, June 12, 2007

Retrieving the Icon Image within the System Image List in .NETCF 2.0

Here's a nice trick for retrieving the icon image of a file or folder from the system image list. All we actually need is to P/Invoke SHGetFileInfo and use Icon.FromHandle() to get the Icon.

First, we need to declare our P/Invokes.

[StructLayout(LayoutKind.Sequential)]
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;
}

const uint SHGFI_ICON = 0x000000100;
const uint SHGFI_LARGEICON = 0x000000000;
const uint SHGFI_SMALLICON = 0x000000001;
const uint SHGFI_SELECTICON = 0x000040000;

[DllImport("coredll.dll")]
static extern IntPtr SHGetFileInfo(string pszPath, uint dwFileAttributes,
  ref SHFILEINFO psfi, uint cbSizeFileInfo, uint uFlags);


To get an instance of System.Drawing.Icon for the small icon of a file

Icon GetSystemIconSmall(string file)
{
  SHFILEINFO shinfo = new SHFILEINFO();
  IntPtr i = SHGetFileInfo(file, 0, ref shinfo,
    (uint)Marshal.SizeOf(shinfo), SHGFI_ICON | SHGFI_SMALLICON);

  return Icon.FromHandle(shinfo.hIcon);
}


For the large icon of a file

Icon GetSystemIconLarge(string file)
{
  SHFILEINFO shinfo = new SHFILEINFO();
  IntPtr i = SHGetFileInfo(file, 0, ref shinfo,
    (uint)Marshal.SizeOf(shinfo), SHGFI_ICON | SHGFI_LARGEICON);

  return Icon.FromHandle(shinfo.hIcon);
}


For the small icon of a file when it is selected

Icon GetSystemIconSmallSelected(string file)
{
  SHFILEINFO shinfo = new SHFILEINFO();
  IntPtr i = SHGetFileInfo(file, 0, ref shinfo,
    (uint)Marshal.SizeOf(shinfo), SHGFI_ICON | SHGFI_SMALLICON | SHGFI_SELECTICON);

  return Icon.FromHandle(shinfo.hIcon);
}


And last for the large icon of a file when it is selected

Icon GetSystemIconLargeSelected(string file)
{
  SHFILEINFO shinfo = new SHFILEINFO();
  IntPtr i = SHGetFileInfo(file, 0, ref shinfo,
    (uint)Marshal.SizeOf(shinfo), SHGFI_ICON | SHGFI_LARGEICON | SHGFI_SELECTICON);

  return Icon.FromHandle(shinfo.hIcon);
}

Ok, now how is this helpful? Well if you want to implement a File Explorer-ish control, then wouldn't have to include Icons and other images in your application. You can just use the icons in the system image list

1 comment:

  1. You could wrap the RAPI calls yourself or use the free, open source managed code product we've been developing for years:

    http://www.opennetcf.com/sharedsource/communication.ocf

    ReplyDelete