Skip to content

Commit b426236

Browse files
committed
WIP
1 parent 78ff6da commit b426236

File tree

3 files changed

+96
-0
lines changed

3 files changed

+96
-0
lines changed

App/Controls/PInvoke.cs

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Runtime.InteropServices;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
8+
namespace Coder.Desktop.App.Controls
9+
{
10+
internal static class PInvoke
11+
{
12+
public const uint IMAGE_ICON = 1;
13+
public const uint LR_LOADFROMFILE = 0x00000010;
14+
public const uint WM_SETICON = 0x0080;
15+
public const int ICON_SMALL = 0;
16+
public const int ICON_BIG = 1;
17+
18+
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
19+
public static extern IntPtr LoadImage(
20+
IntPtr hInst,
21+
string lpszName,
22+
uint uType,
23+
int cxDesired,
24+
int cyDesired,
25+
uint fuLoad);
26+
27+
[DllImport("user32.dll", SetLastError = true)]
28+
public static extern IntPtr SendMessage(
29+
IntPtr hWnd,
30+
uint Msg,
31+
IntPtr wParam,
32+
IntPtr lParam);
33+
34+
[DllImport("user32.dll", SetLastError = true)]
35+
[return: MarshalAs(UnmanagedType.Bool)]
36+
public static extern bool DestroyIcon(IntPtr hIcon);
37+
38+
}
39+
}

App/Controls/TitleBarIcon.cs

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using System;
2+
using Microsoft.UI;
3+
using Microsoft.UI.Windowing;
4+
using Microsoft.UI.Xaml;
5+
using WinRT.Interop;
6+
7+
namespace Coder.Desktop.App.Controls
8+
{
9+
public static class TitleBarIcon
10+
{
11+
public static void InjectIcon(Window window)
12+
{
13+
var hwnd = WindowNative.GetWindowHandle(window);
14+
var windowId = Win32Interop.GetWindowIdFromWindow(hwnd);
15+
AppWindow.GetFromWindowId(windowId).SetIcon("coder.ico");
16+
}
17+
18+
public static void SetTitlebarIcon(Window window)
19+
{
20+
var hwnd = WindowNative.GetWindowHandle(window);
21+
22+
string iconPathDark = "Assets/coder_icon_32_dark.ico";
23+
string iconPathLight = "Assets/coder_icon_32_light.ico";
24+
25+
var hIconDark = PInvoke.LoadImage(
26+
IntPtr.Zero,
27+
iconPathDark,
28+
PInvoke.IMAGE_ICON,
29+
0,
30+
0,
31+
PInvoke.LR_LOADFROMFILE
32+
);
33+
34+
var hIconLight = PInvoke.LoadImage(
35+
IntPtr.Zero,
36+
iconPathLight,
37+
PInvoke.IMAGE_ICON,
38+
0,
39+
0,
40+
PInvoke.LR_LOADFROMFILE
41+
);
42+
43+
PInvoke.SendMessage(hwnd, PInvoke.WM_SETICON, (IntPtr)PInvoke.ICON_SMALL, hIconDark);
44+
PInvoke.SendMessage(hwnd, PInvoke.WM_SETICON, (IntPtr)PInvoke.ICON_BIG, hIconLight);
45+
}
46+
}
47+
}

App/Views/FileSyncListWindow.xaml.cs

+10
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
using Coder.Desktop.App.ViewModels;
22
using Coder.Desktop.App.Views.Pages;
3+
using Microsoft.UI.Xaml;
4+
using Microsoft.UI;
35
using Microsoft.UI.Xaml.Media;
6+
using WinRT.Interop;
47
using WinUIEx;
8+
using Microsoft.UI.Windowing;
9+
using System;
10+
using System.IO;
11+
using Coder.Desktop.App.Controls;
512

613
namespace Coder.Desktop.App.Views;
714

@@ -18,6 +25,9 @@ public FileSyncListWindow(FileSyncListViewModel viewModel)
1825
ViewModel.Initialize(this, DispatcherQueue);
1926
RootFrame.Content = new FileSyncListMainPage(ViewModel);
2027

28+
TitleBarIcon.SetTitlebarIcon(this);
29+
2130
this.CenterOnScreen();
2231
}
32+
2333
}

0 commit comments

Comments
 (0)