9bb4f85802
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
42 lines
1.5 KiB
C#
42 lines
1.5 KiB
C#
using System.Runtime.InteropServices;
|
|
using Avalonia.Controls;
|
|
|
|
namespace CVRManager.Views;
|
|
|
|
public static class WindowIcons
|
|
{
|
|
const uint WM_SETICON = 0x0080;
|
|
const int ICON_SMALL = 0;
|
|
const int ICON_BIG = 1;
|
|
const int SM_CXICON = 11;
|
|
const int SM_CXSMICON = 49;
|
|
|
|
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
|
|
static extern uint PrivateExtractIcons(string file, int index, int cx, int cy, IntPtr[] icons, uint[] ids, uint count, uint flags);
|
|
|
|
[DllImport("user32.dll")]
|
|
static extern IntPtr SendMessage(IntPtr hwnd, uint msg, IntPtr wParam, IntPtr lParam);
|
|
|
|
[DllImport("user32.dll")]
|
|
static extern int GetSystemMetrics(int index);
|
|
|
|
public static void Apply(Window window)
|
|
{
|
|
if (!OperatingSystem.IsWindows()) return;
|
|
var exe = Environment.ProcessPath;
|
|
var hwnd = window.TryGetPlatformHandle()?.Handle ?? IntPtr.Zero;
|
|
if (exe == null || hwnd == IntPtr.Zero || !Path.GetFileName(exe).Equals("CVRManager.exe", StringComparison.OrdinalIgnoreCase)) return;
|
|
|
|
Set(hwnd, exe, GetSystemMetrics(SM_CXSMICON), ICON_SMALL);
|
|
Set(hwnd, exe, GetSystemMetrics(SM_CXICON), ICON_BIG);
|
|
}
|
|
|
|
static void Set(IntPtr hwnd, string exe, int size, int kind)
|
|
{
|
|
var handles = new IntPtr[1];
|
|
var ids = new uint[1];
|
|
if (PrivateExtractIcons(exe, 0, size, size, handles, ids, 1, 0) > 0 && handles[0] != IntPtr.Zero)
|
|
SendMessage(hwnd, WM_SETICON, (IntPtr)kind, handles[0]);
|
|
}
|
|
}
|