Skip to content

Commit

Permalink
Fixed formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
BreakShoot committed Jul 21, 2020
1 parent 12d6093 commit f77d4dc
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 45 deletions.
16 changes: 9 additions & 7 deletions CSharpFileExplorer/IconLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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.SHSTOCKICONINFO>();
NativeMethods.SHGetStockIconInfo(siid, (uint)NativeMethods.SHGFI.Icon, ref stockIconInfo);
var stockIconInfo = new NativeMethods.SHSTOCKICONINFO();
stockIconInfo.cbSize = (uint) Marshal.SizeOf<NativeMethods.SHSTOCKICONINFO>();
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);
Expand All @@ -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());
}
}
}
}
80 changes: 42 additions & 38 deletions CSharpFileExplorer/NativeMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -60,7 +25,7 @@ public enum SHGFI : uint
PIDL = 0x000000008,
UseFileAttributes = 0x000000010,
AddOverlays = 0x000000020,
OverlayIndex = 0x000000040,
OverlayIndex = 0x000000040
}

public enum SHSTOCKICONID : uint
Expand Down Expand Up @@ -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;
}
}
}
}

0 comments on commit f77d4dc

Please sign in to comment.