Repository navigation
Expand file tree
/
Copy pathWindowIcon.cs
More file actions
41 lines (36 loc) · 1.44 KB
/
Copy pathWindowIcon.cs
File metadata and controls
41 lines (36 loc) · 1.44 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
using System.IO;
using System.Windows;
using System.Windows.Interop;
namespace Parry;
internal static class WindowIcon
{
private static System.Drawing.Icon? _small;
private static System.Drawing.Icon? _big;
public static void Apply(Window window)
{
window.SourceInitialized += (_, _) => ApplyTo(new WindowInteropHelper(window).Handle);
}
public static void ApplyTo(IntPtr hwnd)
{
Ensure();
if (hwnd == IntPtr.Zero || _small is null || _big is null) return;
NativeMethods.SendMessage(hwnd, NativeMethods.WM_SETICON, NativeMethods.ICON_SMALL, _small.Handle);
NativeMethods.SendMessage(hwnd, NativeMethods.WM_SETICON, NativeMethods.ICON_BIG, _big.Handle);
}
private static void Ensure()
{
if (_big is not null) return;
var stream = System.Windows.Application.GetResourceStream(new Uri("pack://application:,,,/Assets/parry.ico"))?.Stream;
if (stream is null) return;
using (stream)
{
var ms = new MemoryStream();
stream.CopyTo(ms);
var bytes = ms.ToArray();
var sm = Math.Max(16, NativeMethods.GetSystemMetrics(NativeMethods.SM_CXSMICON));
var lg = Math.Max(32, NativeMethods.GetSystemMetrics(NativeMethods.SM_CXICON));
_small = new System.Drawing.Icon(new MemoryStream(bytes), sm, sm);
_big = new System.Drawing.Icon(new MemoryStream(bytes), lg, lg);
}
}
}