using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Runtime.InteropServices;
namespace AjGenesisStudio.Utils
{
class IconExtraction
{
public static int GetIcon(ImageList images, string extension)
{
return GetIcon(images, extension, false);
}
public static int GetIcon(ImageList images, string extension, bool largeIcon)
{
for (int i = 0; i < images.Images.Count; i++)
if (images.Images.Keys[i] == extension)
return i;
images.Images.Add(extension, ExtractIconForExtension(extension, largeIcon));
return images.Images.Count - 1;
}
public static Icon ExtractIconForExtension(string extension, bool large)
{
if (extension != null)
{
//let's just make up a file name with that extension
string fictitiousFile = "0" + extension;
//now get the icon for that file
return GetAssociatedIcon(fictitiousFile, large);
}
else
{
throw new ArgumentException("Invalid file or extension.", "fileOrExtension");
}
}
public static Icon GetAssociatedIcon(string stubPath, bool large)
{
SHFILEINFO info = new SHFILEINFO(true);
int cbFileInfo = Marshal.SizeOf(info);
SHGFI flags;
if (large)
flags = SHGFI.Icon | SHGFI.LargeIcon | SHGFI.UseFileAttributes;
else
flags = SHGFI.Icon | SHGFI.SmallIcon | SHGFI.UseFileAttributes;
SHGetFileInfo(stubPath, 256, out info, (uint)cbFileInfo, flags);
return (Icon)Icon.FromHandle(info.hIcon);
}
#region Win32 API imports
[DllImport("shell32.dll", CharSet = CharSet.Auto)]
private static extern int SHGetFileInfo(
string pszPath,
int dwFileAttributes,
out SHFILEINFO psfi,
uint cbfileInfo,
SHGFI uFlags);
private const int MAX_PATH = 260;
private const int MAX_TYPE = 80;
private struct SHFILEINFO
{
public SHFILEINFO(bool b)
{
hIcon = IntPtr.Zero;
iIcon = 0;
dwAttributes = 0;
szDisplayName = "";
szTypeName = "";
}
public IntPtr hIcon;
public int iIcon;
public uint dwAttributes;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = MAX_PATH)]
public string szDisplayName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = MAX_TYPE)]
public string szTypeName;
};
[Flags]
enum SHGFI : int
{
/// get icon
Icon = 0x000000100,
/// get display name
DisplayName = 0x000000200,
/// get type name
TypeName = 0x000000400,
/// get attributes
Attributes = 0x000000800,
/// get icon location
IconLocation = 0x000001000,
/// return exe type
ExeType = 0x000002000,
/// get system icon index
SysIconIndex = 0x000004000,
/// put a link overlay on icon
LinkOverlay = 0x000008000,
/// show icon in selected state
Selected = 0x000010000,
/// get only specified attributes
Attr_Specified = 0x000020000,
/// get large icon
LargeIcon = 0x000000000,
/// get small icon
SmallIcon = 0x000000001,
/// get open icon
OpenIcon = 0x000000002,
/// get shell size icon
ShellIconize = 0x000000004,
/// pszPath is a pidl
PIDL = 0x000000008,
/// use passed dwFileAttribute
UseFileAttributes = 0x000000010,
/// apply the appropriate overlays
AddOverlays = 0x000000020,
/// Get the index of the overlay in the upper 8 bits of the iIcon
OverlayIndex = 0x000000040,
}
#endregion
}
}