diff --git a/CSharpFileExplorer/IconLoader.cs b/CSharpFileExplorer/IconLoader.cs index 50fdf74..f63ceb1 100644 --- a/CSharpFileExplorer/IconLoader.cs +++ b/CSharpFileExplorer/IconLoader.cs @@ -3,13 +3,13 @@ namespace CSharpFileExplorer { - class IconLoader + internal class IconLoader { public static Icon GetStockIcon(NativeMethods.SHSTOCKICONID siid) { - NativeMethods.SHSTOCKICONINFO stockIconInfo = new NativeMethods.SHSTOCKICONINFO(); - stockIconInfo.cbSize = (uint)Marshal.SizeOf(); - NativeMethods.SHGetStockIconInfo(siid, (uint)NativeMethods.SHGFI.Icon, ref stockIconInfo); + var stockIconInfo = new NativeMethods.SHSTOCKICONINFO(); + stockIconInfo.cbSize = (uint) Marshal.SizeOf(); + NativeMethods.SHGetStockIconInfo(siid, (uint) NativeMethods.SHGFI.Icon, ref stockIconInfo); var icon = Icon.FromHandle(stockIconInfo.hIcon).ToBitmap(); //convert to bitmap to make it transparent icon.MakeTransparent(); NativeMethods.DestroyIcon(stockIconInfo.hIcon); @@ -18,12 +18,14 @@ public static Icon GetStockIcon(NativeMethods.SHSTOCKICONID siid) public static Icon GetFileTypeIcon(string filetype) { - NativeMethods.SHFILEINFO iconInfo = new NativeMethods.SHFILEINFO(); - NativeMethods.SHGetFileInfo(filetype, 256, ref iconInfo, 0, (uint)(NativeMethods.SHGFI.Icon | NativeMethods.SHGFI.SmallIcon | NativeMethods.SHGFI.UseFileAttributes)); + var iconInfo = new NativeMethods.SHFILEINFO(); + NativeMethods.SHGetFileInfo(filetype, 256, ref iconInfo, 0, + (uint) (NativeMethods.SHGFI.Icon | NativeMethods.SHGFI.SmallIcon | + NativeMethods.SHGFI.UseFileAttributes)); var icon = Icon.FromHandle(iconInfo.hIcon).ToBitmap(); icon.MakeTransparent(); NativeMethods.DestroyIcon(iconInfo.hIcon); return Icon.FromHandle(icon.GetHicon()); } } -} +} \ No newline at end of file diff --git a/CSharpFileExplorer/NativeMethods.cs b/CSharpFileExplorer/NativeMethods.cs index 54448a3..0b2742f 100644 --- a/CSharpFileExplorer/NativeMethods.cs +++ b/CSharpFileExplorer/NativeMethods.cs @@ -3,43 +3,8 @@ namespace CSharpFileExplorer { - class NativeMethods + internal class NativeMethods { - [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] - public struct SHSTOCKICONINFO - { - public uint cbSize; - public IntPtr hIcon; - public int iSysIconIndex; - public int iIcon; - [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)] - public string szPath; - } - - [DllImport("shell32.dll")] - public static extern int SHGetStockIconInfo(SHSTOCKICONID siid, uint uFlags, ref SHSTOCKICONINFO psii); - - [DllImport("user32.dll")] - public static extern bool DestroyIcon(IntPtr handle); - - [DllImport("uxtheme.dll", ExactSpelling = true, CharSet = CharSet.Unicode)] - public static extern int SetWindowTheme(IntPtr hwnd, string pszSubAppName, string pszSubIdList); - - [DllImport("shell32.dll", CharSet = CharSet.Auto)] - public static extern IntPtr SHGetFileInfo(string pszPath, uint dwFileAttributes, ref SHFILEINFO psfi, uint cbFileInfo, uint uFlags); - - [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)] - public struct SHFILEINFO - { - public IntPtr hIcon; - public int iIcon; - public uint dwAttributes; - [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)] - public string szDisplayName; - [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)] - public string szTypeName; - } - [Flags] public enum SHGFI : uint { @@ -60,7 +25,7 @@ public enum SHGFI : uint PIDL = 0x000000008, UseFileAttributes = 0x000000010, AddOverlays = 0x000000020, - OverlayIndex = 0x000000040, + OverlayIndex = 0x000000040 } public enum SHSTOCKICONID : uint @@ -160,5 +125,44 @@ public enum SHSTOCKICONID : uint SIID_CLUSTEREDDRIVE = 140, SIID_MAX_ICONS = 175 } + + [DllImport("shell32.dll")] + public static extern int SHGetStockIconInfo(SHSTOCKICONID siid, uint uFlags, ref SHSTOCKICONINFO psii); + + [DllImport("user32.dll")] + public static extern bool DestroyIcon(IntPtr handle); + + [DllImport("uxtheme.dll", ExactSpelling = true, CharSet = CharSet.Unicode)] + public static extern int SetWindowTheme(IntPtr hwnd, string pszSubAppName, string pszSubIdList); + + [DllImport("shell32.dll", CharSet = CharSet.Auto)] + public static extern IntPtr SHGetFileInfo(string pszPath, uint dwFileAttributes, ref SHFILEINFO psfi, + uint cbFileInfo, uint uFlags); + + [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] + public struct SHSTOCKICONINFO + { + public uint cbSize; + public IntPtr hIcon; + public int iSysIconIndex; + public int iIcon; + + [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)] + public string szPath; + } + + [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)] + public struct SHFILEINFO + { + public IntPtr hIcon; + public int iIcon; + public uint dwAttributes; + + [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)] + public string szDisplayName; + + [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)] + public string szTypeName; + } } -} +} \ No newline at end of file