Trying to fix an instability with the icon retrieval, unfortunately this problem on occurs when Greenshot is running outside Visual Studio.

This commit is contained in:
RKrom 2014-10-29 16:36:01 +01:00
commit ec302c6003

View file

@ -61,7 +61,7 @@ namespace GreenshotPlugin.UnmanagedHelpers {
public int lParam; public int lParam;
public IntPtr iImage; public IntPtr iImage;
} }
[StructLayout(LayoutKind.Sequential)] [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
private struct SHFILEINFO { private struct SHFILEINFO {
public IntPtr hIcon; public IntPtr hIcon;
public int iIcon; public int iIcon;
@ -143,35 +143,40 @@ namespace GreenshotPlugin.UnmanagedHelpers {
} }
/// <summary> /// <summary>
/// Returns an icon for a given file - indicated by the name parameter. /// Returns an icon for a given file extension - indicated by the name parameter.
/// </summary> /// </summary>
/// <param name="name">Pathname for file.</param> /// <param name="name">Filename</param>
/// <param name="size">Large or small</param> /// <param name="size">Large or small</param>
/// <param name="linkOverlay">Whether to include the link icon</param> /// <param name="linkOverlay">Whether to include the link icon</param>
/// <returns>System.Drawing.Icon</returns> /// <returns>System.Drawing.Icon</returns>
public static Icon GetFileIcon(string name, IconSize size, bool linkOverlay) { public static Icon GetFileIcon(string filename, IconSize size, bool linkOverlay) {
SHFILEINFO shfi = new SHFILEINFO(); SHFILEINFO shfi = new SHFILEINFO();
// SHGFI_USEFILEATTRIBUTES makes it simulate, just gets the icon for the extension
uint flags = Shell32.SHGFI_ICON | Shell32.SHGFI_USEFILEATTRIBUTES; uint flags = Shell32.SHGFI_ICON | Shell32.SHGFI_USEFILEATTRIBUTES;
if (true == linkOverlay) { if (true == linkOverlay) {
flags += Shell32.SHGFI_LINKOVERLAY; flags += Shell32.SHGFI_LINKOVERLAY;
} }
/* Check the size specified for return. */ // Check the size specified for return.
if (IconSize.Small == size) { if (IconSize.Small == size) {
flags += Shell32.SHGFI_SMALLICON; flags += Shell32.SHGFI_SMALLICON;
} else { } else {
flags += Shell32.SHGFI_LARGEICON; flags += Shell32.SHGFI_LARGEICON;
} }
SHGetFileInfo(name, Shell32.FILE_ATTRIBUTE_NORMAL, ref shfi, (uint)Marshal.SizeOf(shfi), flags); SHGetFileInfo(Path.GetFileName(filename), Shell32.FILE_ATTRIBUTE_NORMAL, ref shfi, (uint)Marshal.SizeOf(shfi), flags);
// Only return an icon if we really got one
if (shfi.hIcon != IntPtr.Zero) {
// Copy (clone) the returned icon to a new object, thus allowing us to clean-up properly // Copy (clone) the returned icon to a new object, thus allowing us to clean-up properly
Icon icon = (Icon)Icon.FromHandle(shfi.hIcon).Clone(); Icon icon = (Icon)Icon.FromHandle(shfi.hIcon).Clone();
// Cleanup // Cleanup
User32.DestroyIcon(shfi.hIcon); User32.DestroyIcon(shfi.hIcon);
return icon; return icon;
} }
return null;
}
/// <summary> /// <summary>
/// Used to access system folder icons. /// Used to access system folder icons.