-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIconBitmapResolver.cs
More file actions
65 lines (56 loc) · 2.23 KB
/
Copy pathIconBitmapResolver.cs
File metadata and controls
65 lines (56 loc) · 2.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
using System.Runtime.InteropServices;
using static DesktopIconDropper.NativeMethods;
namespace DesktopIconDropper;
// Masaüstündeki bir simgenin GÖRÜNEN ADINDAN yola çıkarak (örn. "Chrome"), gerçek
// dosya/kısayol yolunu bulup Windows'tan o dosyanın gerçek ikon resmini (Bitmap) çeker.
// Bunu, "takla atma" animasyonu sırasında gerçek simgeyi kendi çizdiğimiz bir resimle
// (döndürerek) göstermek için kullanıyoruz.
internal static class IconBitmapResolver
{
private static readonly string[] DesktopDirs =
{
Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory),
Environment.GetFolderPath(Environment.SpecialFolder.CommonDesktopDirectory)
};
public static Bitmap? GetIconBitmap(string itemName)
{
string? path = ResolvePath(itemName);
if (path == null) return null;
SHFILEINFO shfi = new();
nint result = SHGetFileInfo(path, 0, ref shfi, (uint)Marshal.SizeOf<SHFILEINFO>(),
SHGFI_ICON | SHGFI_LARGEICON);
if (result == 0 || shfi.hIcon == 0) return null;
try
{
using Icon icon = Icon.FromHandle(shfi.hIcon);
return (Bitmap)icon.ToBitmap().Clone();
}
finally
{
DestroyIcon(shfi.hIcon);
}
}
private static string? ResolvePath(string itemName)
{
foreach (var dir in DesktopDirs)
{
if (string.IsNullOrEmpty(dir) || !Directory.Exists(dir)) continue;
string direct = Path.Combine(dir, itemName);
if (File.Exists(direct) || Directory.Exists(direct))
return direct;
// Windows kısayollarda uzantıyı (.lnk vs.) gizlediği için, isim tam
// eşleşmezse dosya adını uzantısız karşılaştırarak arıyoruz.
try
{
var match = Directory.EnumerateFileSystemEntries(dir).FirstOrDefault(f =>
string.Equals(Path.GetFileNameWithoutExtension(f), itemName, StringComparison.OrdinalIgnoreCase));
if (match != null) return match;
}
catch
{
// erişim engellenmiş bir klasör olabilir, yoksay
}
}
return null;
}
}