2021-04-24 21:32:30 +12:00
|
|
|
|
// <copyright file="IconReader.cs" company="PlaceholderCompany">
|
|
|
|
|
// Copyright (c) PlaceholderCompany. All rights reserved.
|
|
|
|
|
// </copyright>
|
|
|
|
|
|
|
|
|
|
namespace SystemTrayMenu.Utilities
|
|
|
|
|
{
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Concurrent;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Drawing;
|
|
|
|
|
using System.Drawing.Imaging;
|
|
|
|
|
using System.IO;
|
2021-10-14 04:13:11 +13:00
|
|
|
|
using System.Linq;
|
2021-04-24 21:32:30 +12:00
|
|
|
|
using System.Runtime.InteropServices;
|
2021-10-14 04:13:11 +13:00
|
|
|
|
using System.Text;
|
2021-09-24 08:53:46 +12:00
|
|
|
|
using System.Threading;
|
2021-10-14 04:13:11 +13:00
|
|
|
|
using TAFactory.IconPack;
|
2021-04-24 21:32:30 +12:00
|
|
|
|
|
|
|
|
|
// from https://www.codeproject.com/Articles/2532/Obtaining-and-managing-file-and-folder-icons-using
|
|
|
|
|
// added ImageList_GetIcon, IconCache, AddIconOverlay
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Provides static methods to read system icons for both folders and files.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <example>
|
|
|
|
|
/// <code>IconReader.GetFileIcon("c:\\general.xls");</code>
|
|
|
|
|
/// </example>
|
|
|
|
|
public static class IconReader
|
|
|
|
|
{
|
|
|
|
|
private static readonly ConcurrentDictionary<string, Icon> DictIconCache = new ConcurrentDictionary<string, Icon>();
|
2021-09-25 01:37:58 +12:00
|
|
|
|
private static readonly Icon LoadingIcon = Properties.Resources.Loading;
|
2021-04-24 21:32:30 +12:00
|
|
|
|
|
|
|
|
|
// private static readonly object ReadIcon = new object();
|
|
|
|
|
public enum IconSize
|
|
|
|
|
{
|
|
|
|
|
Large = 0, // 32x32 pixels
|
|
|
|
|
Small = 1, // 16x16 pixels
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public enum FolderType
|
|
|
|
|
{
|
|
|
|
|
Open = 0,
|
|
|
|
|
Closed = 1,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void Dispose()
|
|
|
|
|
{
|
|
|
|
|
foreach (Icon icon in DictIconCache.Values)
|
|
|
|
|
{
|
|
|
|
|
icon?.Dispose();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-11 03:44:06 +13:00
|
|
|
|
public static bool ClearIfCacheTooBig()
|
2021-10-07 10:22:58 +13:00
|
|
|
|
{
|
|
|
|
|
bool cleared = false;
|
|
|
|
|
if (DictIconCache.Count > 200)
|
|
|
|
|
{
|
|
|
|
|
Dispose();
|
|
|
|
|
DictIconCache.Clear();
|
|
|
|
|
cleared = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return cleared;
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-14 04:13:11 +13:00
|
|
|
|
public static Icon GetExtractAllIconsLastWithCache(string filePath, bool updateIconInBackground, out bool loading)
|
|
|
|
|
{
|
|
|
|
|
bool linkOverlay = false;
|
|
|
|
|
loading = false;
|
|
|
|
|
string key = filePath;
|
|
|
|
|
|
|
|
|
|
string extension = Path.GetExtension(filePath);
|
|
|
|
|
|
|
|
|
|
if (IsExtensionWithSameIcon(extension))
|
|
|
|
|
{
|
|
|
|
|
key = extension + linkOverlay;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ClearBadIcons();
|
|
|
|
|
|
|
|
|
|
if (!DictIconCache.TryGetValue(key, out Icon icon))
|
|
|
|
|
{
|
|
|
|
|
icon = LoadingIcon;
|
|
|
|
|
loading = true;
|
|
|
|
|
|
|
|
|
|
if (updateIconInBackground)
|
|
|
|
|
{
|
|
|
|
|
new Thread(UpdateIconInBackground).Start();
|
|
|
|
|
void UpdateIconInBackground()
|
|
|
|
|
{
|
|
|
|
|
DictIconCache.GetOrAdd(key, GetExtractAllIconsLast);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Icon GetExtractAllIconsLast(string keyExtension)
|
|
|
|
|
{
|
|
|
|
|
return GetExtractAllIconsLastSTA(filePath);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return icon;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Icon GetExtractAllIconsLastSTA(string filePath)
|
|
|
|
|
{
|
|
|
|
|
Icon icon = null;
|
|
|
|
|
|
|
|
|
|
if (Thread.CurrentThread.GetApartmentState() == ApartmentState.STA)
|
|
|
|
|
{
|
|
|
|
|
icon = GetExtractAllIconsLast(filePath);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Thread staThread = new Thread(new ParameterizedThreadStart(StaThreadMethod));
|
|
|
|
|
void StaThreadMethod(object obj)
|
|
|
|
|
{
|
|
|
|
|
icon = GetExtractAllIconsLast(filePath);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
staThread.SetApartmentState(ApartmentState.STA);
|
|
|
|
|
staThread.Start(icon);
|
|
|
|
|
staThread.Join();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static Icon GetExtractAllIconsLast(string filePath)
|
|
|
|
|
{
|
|
|
|
|
StringBuilder executable = new StringBuilder(1024);
|
|
|
|
|
DllImports.NativeMethods.Shell32FindExecutable(filePath, string.Empty, executable);
|
|
|
|
|
|
|
|
|
|
// icon = IconReader.GetFileIcon(executable, false);
|
|
|
|
|
// e.g. VS 2019 icon, need another icom in imagelist
|
|
|
|
|
List<Icon> extractedIcons = IconHelper.ExtractAllIcons(
|
|
|
|
|
executable.ToString());
|
|
|
|
|
return extractedIcons.Last();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return icon;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Icon GetFileIconWithCache(string filePath, bool linkOverlay, bool updateIconInBackground, out bool loading, string keyPath = "")
|
2021-04-24 21:32:30 +12:00
|
|
|
|
{
|
2021-09-24 08:53:46 +12:00
|
|
|
|
loading = false;
|
2021-04-24 21:32:30 +12:00
|
|
|
|
string extension = Path.GetExtension(filePath);
|
2021-05-02 20:05:33 +12:00
|
|
|
|
IconSize size = IconSize.Small;
|
2021-06-27 01:23:39 +12:00
|
|
|
|
if (Scaling.Factor > 1)
|
|
|
|
|
{
|
|
|
|
|
size = IconSize.Large;
|
|
|
|
|
}
|
2021-04-24 21:32:30 +12:00
|
|
|
|
|
2021-10-06 09:47:47 +13:00
|
|
|
|
string key = filePath;
|
2021-10-14 04:13:11 +13:00
|
|
|
|
if (!string.IsNullOrEmpty(keyPath))
|
|
|
|
|
{
|
|
|
|
|
key = keyPath;
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-06 09:47:47 +13:00
|
|
|
|
if (IsExtensionWithSameIcon(extension))
|
2021-04-24 21:32:30 +12:00
|
|
|
|
{
|
2021-10-06 09:47:47 +13:00
|
|
|
|
key = extension + linkOverlay;
|
2021-04-24 21:32:30 +12:00
|
|
|
|
}
|
2021-10-06 09:47:47 +13:00
|
|
|
|
|
2021-10-14 04:13:11 +13:00
|
|
|
|
ClearBadIcons();
|
|
|
|
|
|
2021-10-11 03:44:06 +13:00
|
|
|
|
if (!DictIconCache.TryGetValue(key, out Icon icon))
|
2021-04-24 21:32:30 +12:00
|
|
|
|
{
|
2021-10-06 09:47:47 +13:00
|
|
|
|
icon = LoadingIcon;
|
|
|
|
|
loading = true;
|
2021-09-24 08:53:46 +12:00
|
|
|
|
|
2021-10-06 09:47:47 +13:00
|
|
|
|
if (updateIconInBackground)
|
|
|
|
|
{
|
|
|
|
|
new Thread(UpdateIconInBackground).Start();
|
|
|
|
|
void UpdateIconInBackground()
|
2021-09-24 08:53:46 +12:00
|
|
|
|
{
|
2021-10-06 09:47:47 +13:00
|
|
|
|
DictIconCache.GetOrAdd(key, GetIcon);
|
2021-09-24 08:53:46 +12:00
|
|
|
|
}
|
|
|
|
|
}
|
2021-05-30 03:14:36 +12:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Icon GetIcon(string keyExtension)
|
|
|
|
|
{
|
|
|
|
|
return GetFileIconSTA(filePath, linkOverlay, size);
|
2021-04-24 21:32:30 +12:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return icon;
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-14 04:13:11 +13:00
|
|
|
|
public static Icon GetFolderIconWithCache(string path, FolderType folderType, bool linkOverlay, bool updateIconInBackground, out bool loading)
|
|
|
|
|
{
|
|
|
|
|
loading = false;
|
|
|
|
|
IconSize size = IconSize.Small;
|
|
|
|
|
|
|
|
|
|
// with this we get another folder icon than windows explorer
|
|
|
|
|
// if (Scaling.Factor > 1)
|
|
|
|
|
// {
|
|
|
|
|
// size = IconSize.Large;
|
|
|
|
|
// }
|
|
|
|
|
string key = path;
|
|
|
|
|
|
|
|
|
|
// Maybe we can reduce object when checking if a standard folder
|
|
|
|
|
// if (IsStandardFolderIcon(key))
|
|
|
|
|
// {
|
|
|
|
|
// key = "folder" + linkOverlay;
|
|
|
|
|
// }
|
|
|
|
|
ClearBadIcons();
|
|
|
|
|
|
|
|
|
|
if (!DictIconCache.TryGetValue(key, out Icon icon))
|
|
|
|
|
{
|
|
|
|
|
icon = LoadingIcon;
|
|
|
|
|
loading = true;
|
|
|
|
|
|
|
|
|
|
if (updateIconInBackground)
|
|
|
|
|
{
|
|
|
|
|
new Thread(UpdateIconInBackground).Start();
|
|
|
|
|
void UpdateIconInBackground()
|
|
|
|
|
{
|
|
|
|
|
DictIconCache.GetOrAdd(key, GetFolder);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Icon GetFolder(string keyExtension)
|
|
|
|
|
{
|
|
|
|
|
return GetFolderIconSTA(path, folderType, linkOverlay, size);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return icon;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-24 21:32:30 +12:00
|
|
|
|
public static Icon GetFolderIconSTA(
|
|
|
|
|
string directoryPath,
|
|
|
|
|
FolderType folderType,
|
|
|
|
|
bool linkOverlay,
|
|
|
|
|
IconSize size = IconSize.Small)
|
|
|
|
|
{
|
|
|
|
|
Icon icon = null;
|
2021-10-14 04:13:11 +13:00
|
|
|
|
if (Thread.CurrentThread.GetApartmentState() == ApartmentState.STA)
|
|
|
|
|
{
|
|
|
|
|
icon = GetFolderIcon(
|
|
|
|
|
directoryPath,
|
|
|
|
|
folderType,
|
|
|
|
|
linkOverlay,
|
|
|
|
|
size);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Thread staThread = new Thread(new ParameterizedThreadStart(StaThreadMethod));
|
|
|
|
|
void StaThreadMethod(object obj)
|
|
|
|
|
{
|
|
|
|
|
icon = GetFolderIcon(
|
|
|
|
|
directoryPath,
|
|
|
|
|
folderType,
|
|
|
|
|
linkOverlay,
|
|
|
|
|
size);
|
|
|
|
|
}
|
2021-04-24 21:32:30 +12:00
|
|
|
|
|
2021-10-14 04:13:11 +13:00
|
|
|
|
staThread.SetApartmentState(ApartmentState.STA);
|
|
|
|
|
staThread.Start(icon);
|
|
|
|
|
staThread.Join();
|
|
|
|
|
}
|
2021-04-24 21:32:30 +12:00
|
|
|
|
|
|
|
|
|
return icon;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Icon GetFolderIcon(
|
|
|
|
|
string directoryPath,
|
|
|
|
|
FolderType folderType,
|
|
|
|
|
bool linkOverlay,
|
|
|
|
|
IconSize size = IconSize.Small)
|
|
|
|
|
{
|
|
|
|
|
Icon icon = null;
|
|
|
|
|
|
|
|
|
|
// Need to add size check, although errors generated at present!
|
|
|
|
|
// uint flags = Shell32.SHGFI_ICON | Shell32.SHGFI_USEFILEATTRIBUTES;
|
|
|
|
|
|
|
|
|
|
// MH: Removed SHGFI_USEFILEATTRIBUTES, otherwise was wrong folder icon
|
|
|
|
|
uint flags = DllImports.NativeMethods.ShgfiIcon; // | Shell32.SHGFI_USEFILEATTRIBUTES;
|
|
|
|
|
|
|
|
|
|
if (linkOverlay)
|
|
|
|
|
{
|
|
|
|
|
flags += DllImports.NativeMethods.ShgfiLINKOVERLAY;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (folderType == FolderType.Open)
|
|
|
|
|
{
|
|
|
|
|
flags += DllImports.NativeMethods.ShgfiOPENICON;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (size == IconSize.Small)
|
|
|
|
|
{
|
|
|
|
|
flags += DllImports.NativeMethods.ShgfiSMALLICON;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
flags += DllImports.NativeMethods.ShgfiLARGEICON;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get the folder icon
|
|
|
|
|
DllImports.NativeMethods.SHFILEINFO shfi = default;
|
|
|
|
|
IntPtr success = DllImports.NativeMethods.Shell32SHGetFileInfo(
|
|
|
|
|
directoryPath,
|
|
|
|
|
DllImports.NativeMethods.FileAttributeDirectory,
|
|
|
|
|
ref shfi,
|
|
|
|
|
(uint)Marshal.SizeOf(shfi),
|
|
|
|
|
flags);
|
|
|
|
|
if (success != IntPtr.Zero &&
|
|
|
|
|
shfi.hIcon != IntPtr.Zero)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
icon = (Icon)Icon.FromHandle(shfi.hIcon).Clone();
|
|
|
|
|
DllImports.NativeMethods.User32DestroyIcon(shfi.hIcon);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Log.Error($"directoryPath:'{directoryPath}'", ex);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return icon;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Icon AddIconOverlay(Icon originalIcon, Icon overlay)
|
|
|
|
|
{
|
|
|
|
|
Icon icon = null;
|
|
|
|
|
if (originalIcon != null)
|
|
|
|
|
{
|
2021-10-14 04:13:11 +13:00
|
|
|
|
using Bitmap target = new Bitmap(originalIcon.Width, originalIcon.Height, PixelFormat.Format32bppArgb);
|
|
|
|
|
using Graphics graphics = Graphics.FromImage(target);
|
2021-04-24 21:32:30 +12:00
|
|
|
|
graphics.DrawIcon(originalIcon, 0, 0);
|
|
|
|
|
graphics.DrawIcon(overlay, 0, 0);
|
|
|
|
|
target.MakeTransparent(target.GetPixel(1, 1));
|
2021-10-14 04:13:11 +13:00
|
|
|
|
IntPtr hIcon = target.GetHicon();
|
|
|
|
|
icon = (Icon)Icon.FromHandle(hIcon).Clone();
|
|
|
|
|
DllImports.NativeMethods.User32DestroyIcon(hIcon);
|
2021-04-24 21:32:30 +12:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return icon;
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-14 04:13:11 +13:00
|
|
|
|
private static void ClearBadIcons()
|
|
|
|
|
{
|
|
|
|
|
IEnumerable<string> badKeysList = DictIconCache.Where(x => x.Value == null).Select(x => x.Key);
|
|
|
|
|
foreach (string badKey in badKeysList)
|
|
|
|
|
{
|
|
|
|
|
DictIconCache.Remove(badKey, out _);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-06 09:47:47 +13:00
|
|
|
|
private static bool IsExtensionWithSameIcon(string fileExtension)
|
2021-04-24 21:32:30 +12:00
|
|
|
|
{
|
2021-10-06 09:47:47 +13:00
|
|
|
|
bool isExtensionWithSameIcon = true;
|
2021-04-24 21:32:30 +12:00
|
|
|
|
List<string> extensionsWithDiffIcons = new List<string>
|
|
|
|
|
{ string.Empty, ".EXE", ".LNK", ".ICO", ".URL" };
|
|
|
|
|
if (extensionsWithDiffIcons.Contains(fileExtension.ToUpperInvariant()))
|
|
|
|
|
{
|
2021-10-06 09:47:47 +13:00
|
|
|
|
isExtensionWithSameIcon = false;
|
2021-04-24 21:32:30 +12:00
|
|
|
|
}
|
|
|
|
|
|
2021-10-06 09:47:47 +13:00
|
|
|
|
return isExtensionWithSameIcon;
|
2021-04-24 21:32:30 +12:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static Icon GetFileIconSTA(string filePath, bool linkOverlay, IconSize size = IconSize.Small)
|
|
|
|
|
{
|
|
|
|
|
Icon icon = null;
|
|
|
|
|
|
2021-10-14 04:13:11 +13:00
|
|
|
|
if (Thread.CurrentThread.GetApartmentState() == ApartmentState.STA)
|
|
|
|
|
{
|
|
|
|
|
icon = GetFileIcon(filePath, linkOverlay, size);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Thread staThread = new Thread(new ParameterizedThreadStart(StaThreadMethod));
|
|
|
|
|
void StaThreadMethod(object obj)
|
|
|
|
|
{
|
|
|
|
|
icon = GetFileIcon(filePath, linkOverlay, size);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
staThread.SetApartmentState(ApartmentState.STA);
|
|
|
|
|
staThread.Start(icon);
|
|
|
|
|
staThread.Join();
|
|
|
|
|
}
|
2021-04-24 21:32:30 +12:00
|
|
|
|
|
|
|
|
|
return icon;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static Icon GetFileIcon(string filePath, bool linkOverlay, IconSize size = IconSize.Small)
|
|
|
|
|
{
|
|
|
|
|
Icon icon = null;
|
|
|
|
|
DllImports.NativeMethods.SHFILEINFO shfi = default;
|
|
|
|
|
uint flags = DllImports.NativeMethods.ShgfiIcon | DllImports.NativeMethods.ShgfiSYSICONINDEX;
|
|
|
|
|
|
|
|
|
|
if (linkOverlay)
|
|
|
|
|
{
|
|
|
|
|
flags += DllImports.NativeMethods.ShgfiLINKOVERLAY;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Check the size specified for return. */
|
|
|
|
|
if (size == IconSize.Small)
|
|
|
|
|
{
|
|
|
|
|
flags += DllImports.NativeMethods.ShgfiSMALLICON;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
flags += DllImports.NativeMethods.ShgfiLARGEICON;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
IntPtr hImageList = DllImports.NativeMethods.Shell32SHGetFileInfo(
|
|
|
|
|
filePath,
|
|
|
|
|
DllImports.NativeMethods.FileAttributeNormal,
|
|
|
|
|
ref shfi,
|
|
|
|
|
(uint)Marshal.SizeOf(shfi),
|
|
|
|
|
flags);
|
|
|
|
|
if (hImageList != IntPtr.Zero)
|
|
|
|
|
{
|
|
|
|
|
IntPtr hIcon;
|
|
|
|
|
if (linkOverlay)
|
|
|
|
|
{
|
|
|
|
|
hIcon = shfi.hIcon; // Get icon directly
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// Get icon from .ink without overlay
|
|
|
|
|
hIcon = DllImports.NativeMethods.ImageList_GetIcon(hImageList, shfi.iIcon, DllImports.NativeMethods.IldTransparent);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
// Copy (clone) the returned icon to a new object, thus allowing us to clean-up properly
|
|
|
|
|
icon = (Icon)Icon.FromHandle(hIcon).Clone();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Log.Error($"filePath:'{filePath}'", ex);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Cleanup
|
|
|
|
|
if (!linkOverlay)
|
|
|
|
|
{
|
|
|
|
|
DllImports.NativeMethods.User32DestroyIcon(hIcon);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DllImports.NativeMethods.User32DestroyIcon(shfi.hIcon);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return icon;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|