2021-04-24 21:32:30 +12:00
|
|
|
|
// <copyright file="IconReader.cs" company="PlaceholderCompany">
|
|
|
|
|
// Copyright (c) PlaceholderCompany. All rights reserved.
|
|
|
|
|
// </copyright>
|
2021-10-16 06:42:02 +13:00
|
|
|
|
// see also: https://www.codeproject.com/Articles/2532/Obtaining-and-managing-file-and-folder-icons-using.
|
2021-04-24 21:32:30 +12:00
|
|
|
|
|
|
|
|
|
namespace SystemTrayMenu.Utilities
|
|
|
|
|
{
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Concurrent;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Drawing;
|
|
|
|
|
using System.Drawing.Imaging;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Runtime.InteropServices;
|
2021-09-24 08:53:46 +12:00
|
|
|
|
using System.Threading;
|
2022-06-07 08:46:13 +12:00
|
|
|
|
using SystemTrayMenu.DllImports;
|
2021-04-24 21:32:30 +12:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
2021-10-16 06:42:02 +13:00
|
|
|
|
/// Provides static methods to read system icons for folders and files.
|
2021-04-24 21:32:30 +12:00
|
|
|
|
/// </summary>
|
|
|
|
|
public static class IconReader
|
|
|
|
|
{
|
2021-11-17 12:13:46 +13:00
|
|
|
|
private static readonly ConcurrentDictionary<string, Icon> DictIconCacheMainMenu = new();
|
|
|
|
|
private static readonly ConcurrentDictionary<string, Icon> DictIconCacheSubMenus = new();
|
2021-04-24 21:32:30 +12:00
|
|
|
|
|
|
|
|
|
public enum IconSize
|
|
|
|
|
{
|
|
|
|
|
Large = 0, // 32x32 pixels
|
|
|
|
|
Small = 1, // 16x16 pixels
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public enum FolderType
|
|
|
|
|
{
|
|
|
|
|
Open = 0,
|
|
|
|
|
Closed = 1,
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-22 07:25:13 +13:00
|
|
|
|
// see https://github.com/Hofknecht/SystemTrayMenu/issues/209.
|
2021-11-13 20:18:33 +13:00
|
|
|
|
public static bool MainPreload { get; set; }
|
2021-10-16 06:42:02 +13:00
|
|
|
|
|
2021-11-14 01:55:01 +13:00
|
|
|
|
public static void Dispose(bool includingMainMenu = true)
|
2021-04-24 21:32:30 +12:00
|
|
|
|
{
|
2021-11-14 01:55:01 +13:00
|
|
|
|
if (includingMainMenu)
|
|
|
|
|
{
|
|
|
|
|
foreach (Icon icon in DictIconCacheMainMenu.Values)
|
|
|
|
|
{
|
|
|
|
|
icon?.Dispose();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach (Icon icon in DictIconCacheSubMenus.Values)
|
2021-04-24 21:32:30 +12:00
|
|
|
|
{
|
|
|
|
|
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;
|
2021-11-14 01:55:01 +13:00
|
|
|
|
if (DictIconCacheSubMenus.Count > Properties.Settings.Default.ClearCacheIfMoreThanThisNumberOfItems)
|
2021-10-07 10:22:58 +13:00
|
|
|
|
{
|
2021-11-14 01:55:01 +13:00
|
|
|
|
Dispose(false);
|
|
|
|
|
DictIconCacheSubMenus.Clear();
|
2021-10-07 10:22:58 +13:00
|
|
|
|
cleared = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return cleared;
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-18 00:38:09 +12:00
|
|
|
|
public static void RemoveIconFromCache(string path)
|
|
|
|
|
{
|
|
|
|
|
if (DictIconCacheMainMenu.Remove(path, out Icon iconToRemove))
|
|
|
|
|
{
|
|
|
|
|
iconToRemove?.Dispose();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-14 01:55:01 +13:00
|
|
|
|
public static Icon GetFileIconWithCache(
|
2022-06-10 05:55:13 +12:00
|
|
|
|
string path,
|
2022-06-18 00:38:09 +12:00
|
|
|
|
string resolvedPath,
|
2021-11-14 01:55:01 +13:00
|
|
|
|
bool linkOverlay,
|
|
|
|
|
bool updateIconInBackground,
|
|
|
|
|
bool isMainMenu,
|
|
|
|
|
out bool loading,
|
|
|
|
|
string keyPath = "")
|
2021-04-24 21:32:30 +12:00
|
|
|
|
{
|
2021-09-24 08:53:46 +12:00
|
|
|
|
loading = false;
|
2022-06-18 00:38:09 +12:00
|
|
|
|
string extension = Path.GetExtension(path);
|
2022-07-28 07:35:09 +12:00
|
|
|
|
IconSize size = IconSize.Small;
|
|
|
|
|
if (Scaling.Factor >= 1.25f ||
|
|
|
|
|
Scaling.FactorByDpi >= 1.25f ||
|
|
|
|
|
Properties.Settings.Default.IconSizeInPercent / 100f >= 1.25f)
|
|
|
|
|
{
|
|
|
|
|
size = IconSize.Large;
|
|
|
|
|
}
|
2021-04-24 21:32:30 +12:00
|
|
|
|
|
2022-06-18 00:38:09 +12:00
|
|
|
|
string key = path;
|
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-11-14 01:55:01 +13:00
|
|
|
|
if (!DictIconCache(isMainMenu).TryGetValue(key, out Icon icon) &&
|
|
|
|
|
!DictIconCache(!isMainMenu).TryGetValue(key, out icon))
|
2021-04-24 21:32:30 +12:00
|
|
|
|
{
|
2021-10-26 08:45:49 +13:00
|
|
|
|
icon = Resources.StaticResources.LoadingIcon;
|
2021-10-06 09:47:47 +13:00
|
|
|
|
loading = true;
|
|
|
|
|
if (updateIconInBackground)
|
|
|
|
|
{
|
|
|
|
|
new Thread(UpdateIconInBackground).Start();
|
|
|
|
|
void UpdateIconInBackground()
|
2021-09-24 08:53:46 +12:00
|
|
|
|
{
|
2022-06-18 00:38:09 +12:00
|
|
|
|
DictIconCache(isMainMenu).GetOrAdd(key, GetIconSTA(path, resolvedPath, linkOverlay, size, false));
|
2021-10-16 06:42:02 +13:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-24 21:32:30 +12:00
|
|
|
|
return icon;
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-14 01:55:01 +13:00
|
|
|
|
public static Icon GetFolderIconWithCache(
|
|
|
|
|
string path,
|
|
|
|
|
bool linkOverlay,
|
|
|
|
|
bool updateIconInBackground,
|
|
|
|
|
bool isMainMenu,
|
|
|
|
|
out bool loading)
|
2021-10-14 04:13:11 +13:00
|
|
|
|
{
|
|
|
|
|
loading = false;
|
2021-10-16 06:42:02 +13:00
|
|
|
|
|
2021-10-14 04:13:11 +13:00
|
|
|
|
IconSize size = IconSize.Small;
|
2022-07-28 07:35:09 +12:00
|
|
|
|
if (Scaling.Factor >= 1.25f ||
|
|
|
|
|
Scaling.FactorByDpi >= 1.25f ||
|
|
|
|
|
Properties.Settings.Default.IconSizeInPercent / 100f >= 1.25f)
|
2022-06-09 06:32:38 +12:00
|
|
|
|
{
|
|
|
|
|
// IconSize.Large returns another folder icon than windows explorer
|
|
|
|
|
size = IconSize.Large;
|
|
|
|
|
}
|
2021-10-14 04:13:11 +13:00
|
|
|
|
|
|
|
|
|
string key = path;
|
|
|
|
|
|
2021-11-14 01:55:01 +13:00
|
|
|
|
if (!DictIconCache(isMainMenu).TryGetValue(key, out Icon icon) &&
|
|
|
|
|
!DictIconCache(!isMainMenu).TryGetValue(key, out icon))
|
2021-10-14 04:13:11 +13:00
|
|
|
|
{
|
2021-10-26 08:45:49 +13:00
|
|
|
|
icon = Resources.StaticResources.LoadingIcon;
|
2021-10-14 04:13:11 +13:00
|
|
|
|
loading = true;
|
|
|
|
|
|
|
|
|
|
if (updateIconInBackground)
|
|
|
|
|
{
|
2021-11-13 20:18:33 +13:00
|
|
|
|
if (MainPreload)
|
2021-10-14 04:13:11 +13:00
|
|
|
|
{
|
2021-11-14 01:55:01 +13:00
|
|
|
|
DictIconCache(isMainMenu).GetOrAdd(key, GetFolder);
|
2021-10-14 04:13:11 +13:00
|
|
|
|
}
|
2021-10-16 06:42:02 +13:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
new Thread(UpdateIconInBackground).Start();
|
|
|
|
|
void UpdateIconInBackground()
|
|
|
|
|
{
|
2021-11-14 01:55:01 +13:00
|
|
|
|
DictIconCache(isMainMenu).GetOrAdd(key, GetFolder);
|
2021-10-16 06:42:02 +13:00
|
|
|
|
}
|
|
|
|
|
}
|
2021-10-14 04:13:11 +13:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Icon GetFolder(string keyExtension)
|
|
|
|
|
{
|
2022-06-18 00:38:09 +12:00
|
|
|
|
return GetIconSTA(path, path, linkOverlay, size, true);
|
2021-10-14 04:13:11 +13:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return icon;
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-18 00:38:09 +12:00
|
|
|
|
public static Icon GetIconSTA(string path, string resolvedPath, bool linkOverlay, IconSize size, bool isFolder)
|
2021-04-24 21:32:30 +12:00
|
|
|
|
{
|
|
|
|
|
Icon icon = null;
|
2021-10-14 04:13:11 +13:00
|
|
|
|
if (Thread.CurrentThread.GetApartmentState() == ApartmentState.STA)
|
|
|
|
|
{
|
2022-06-18 00:38:09 +12:00
|
|
|
|
icon = GetIcon(path, resolvedPath, linkOverlay, size, isFolder);
|
2021-10-14 04:13:11 +13:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2021-11-17 12:13:46 +13:00
|
|
|
|
Thread staThread = new(new ParameterizedThreadStart(StaThreadMethod));
|
2021-10-14 04:13:11 +13:00
|
|
|
|
void StaThreadMethod(object obj)
|
|
|
|
|
{
|
2022-06-18 00:38:09 +12:00
|
|
|
|
icon = GetIcon(path, resolvedPath, linkOverlay, size, isFolder);
|
2021-10-14 04:13:11 +13:00
|
|
|
|
}
|
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 AddIconOverlay(Icon originalIcon, Icon overlay)
|
|
|
|
|
{
|
|
|
|
|
Icon icon = null;
|
|
|
|
|
if (originalIcon != null)
|
|
|
|
|
{
|
2021-11-17 12:13:46 +13:00
|
|
|
|
using Bitmap target = new(originalIcon.Width, originalIcon.Height, PixelFormat.Format32bppArgb);
|
2021-10-14 04:13:11 +13:00
|
|
|
|
using Graphics graphics = Graphics.FromImage(target);
|
2021-04-24 21:32:30 +12:00
|
|
|
|
graphics.DrawIcon(originalIcon, 0, 0);
|
2022-01-08 00:40:55 +13:00
|
|
|
|
graphics.DrawIcon(overlay, new(0, 0, originalIcon.Width + 2, originalIcon.Height + 2));
|
2021-04-24 21:32:30 +12:00
|
|
|
|
target.MakeTransparent(target.GetPixel(1, 1));
|
2021-10-14 04:13:11 +13:00
|
|
|
|
IntPtr hIcon = target.GetHicon();
|
|
|
|
|
icon = (Icon)Icon.FromHandle(hIcon).Clone();
|
2022-06-07 08:46:13 +12:00
|
|
|
|
NativeMethods.User32DestroyIcon(hIcon);
|
2021-04-24 21:32:30 +12:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return icon;
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-14 01:55:01 +13:00
|
|
|
|
private static ConcurrentDictionary<string, Icon> DictIconCache(bool isMainMenu)
|
2021-10-22 07:25:13 +13:00
|
|
|
|
{
|
2021-11-14 01:55:01 +13:00
|
|
|
|
if (isMainMenu)
|
|
|
|
|
{
|
|
|
|
|
return DictIconCacheMainMenu;
|
|
|
|
|
}
|
|
|
|
|
else
|
2021-10-22 07:25:13 +13:00
|
|
|
|
{
|
2021-11-14 01:55:01 +13:00
|
|
|
|
return DictIconCacheSubMenus;
|
2021-10-22 07:25:13 +13:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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-11-17 12:13:46 +13:00
|
|
|
|
List<string> extensionsWithDiffIcons = new() { string.Empty, ".EXE", ".LNK", ".ICO", ".URL" };
|
2021-04-24 21:32:30 +12:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
2022-06-18 00:38:09 +12:00
|
|
|
|
private static Icon GetIcon(string path, string resolvedPath, bool linkOverlay, IconSize size, bool isFolder)
|
2021-04-24 21:32:30 +12:00
|
|
|
|
{
|
2022-06-10 05:55:13 +12:00
|
|
|
|
Icon icon;
|
|
|
|
|
if (Path.GetExtension(path).Equals(".ico", StringComparison.InvariantCultureIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
icon = Icon.ExtractAssociatedIcon(path);
|
|
|
|
|
}
|
2022-06-18 00:38:09 +12:00
|
|
|
|
else if (Path.GetExtension(resolvedPath).Equals(".ico", StringComparison.InvariantCultureIgnoreCase) &&
|
|
|
|
|
File.Exists(resolvedPath))
|
2022-06-10 05:55:13 +12:00
|
|
|
|
{
|
2022-06-18 00:38:09 +12:00
|
|
|
|
icon = Icon.ExtractAssociatedIcon(resolvedPath);
|
2022-06-10 05:55:13 +12:00
|
|
|
|
if (linkOverlay)
|
|
|
|
|
{
|
|
|
|
|
icon = AddIconOverlay(icon, Properties.Resources.LinkArrow);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
NativeMethods.SHFILEINFO shFileInfo = default;
|
2022-06-18 00:38:09 +12:00
|
|
|
|
uint flags = GetFlags(linkOverlay, size);
|
|
|
|
|
uint attribute = isFolder ? NativeMethods.FileAttributeDirectory : NativeMethods.FileAttributeNormal;
|
2022-06-10 05:55:13 +12:00
|
|
|
|
IntPtr imageList = NativeMethods.Shell32SHGetFileInfo(
|
|
|
|
|
path, attribute, ref shFileInfo, (uint)Marshal.SizeOf(shFileInfo), flags);
|
|
|
|
|
icon = GetIcon(path, linkOverlay, shFileInfo, imageList);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return icon;
|
2022-06-07 08:46:13 +12:00
|
|
|
|
}
|
2021-04-24 21:32:30 +12:00
|
|
|
|
|
2022-06-18 00:38:09 +12:00
|
|
|
|
private static uint GetFlags(bool linkOverlay, IconSize size)
|
2022-06-07 08:46:13 +12:00
|
|
|
|
{
|
|
|
|
|
uint flags = NativeMethods.ShgfiIcon | NativeMethods.ShgfiSYSICONINDEX;
|
2021-04-24 21:32:30 +12:00
|
|
|
|
if (linkOverlay)
|
|
|
|
|
{
|
2022-06-07 08:46:13 +12:00
|
|
|
|
flags += NativeMethods.ShgfiLINKOVERLAY;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-24 21:32:30 +12:00
|
|
|
|
if (size == IconSize.Small)
|
|
|
|
|
{
|
2022-06-07 08:46:13 +12:00
|
|
|
|
flags += NativeMethods.ShgfiSMALLICON;
|
2021-04-24 21:32:30 +12:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2022-06-07 08:46:13 +12:00
|
|
|
|
flags += NativeMethods.ShgfiLARGEICON;
|
2021-04-24 21:32:30 +12:00
|
|
|
|
}
|
|
|
|
|
|
2022-06-07 08:46:13 +12:00
|
|
|
|
return flags;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static Icon GetIcon(
|
|
|
|
|
string path, bool linkOverlay, NativeMethods.SHFILEINFO shFileInfo, IntPtr imageList)
|
|
|
|
|
{
|
|
|
|
|
Icon icon = null;
|
|
|
|
|
if (imageList != IntPtr.Zero)
|
2021-04-24 21:32:30 +12:00
|
|
|
|
{
|
|
|
|
|
IntPtr hIcon;
|
|
|
|
|
if (linkOverlay)
|
|
|
|
|
{
|
2022-06-07 08:46:13 +12:00
|
|
|
|
hIcon = shFileInfo.hIcon;
|
2021-04-24 21:32:30 +12:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2022-06-07 08:46:13 +12:00
|
|
|
|
hIcon = NativeMethods.ImageList_GetIcon(
|
|
|
|
|
imageList, shFileInfo.iIcon, NativeMethods.IldTransparent);
|
2021-04-24 21:32:30 +12:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
icon = (Icon)Icon.FromHandle(hIcon).Clone();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2022-06-07 08:46:13 +12:00
|
|
|
|
Log.Warn($"path:'{path}'", ex);
|
2021-04-24 21:32:30 +12:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!linkOverlay)
|
|
|
|
|
{
|
2022-06-07 08:46:13 +12:00
|
|
|
|
NativeMethods.User32DestroyIcon(hIcon);
|
2021-04-24 21:32:30 +12:00
|
|
|
|
}
|
|
|
|
|
|
2022-06-07 08:46:13 +12:00
|
|
|
|
NativeMethods.User32DestroyIcon(shFileInfo.hIcon);
|
2021-04-24 21:32:30 +12:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return icon;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|