How to change/Remove Window Title bar icon in WinUI 3
See the question and my original answer on StackOverflowHere is one solution that doesn't require a physical file in assets but just uses the output .exe's icon, if any.
First of all, make sure there's an ApplicationIcon
property in the .csproj (this can be set in project's properties UI too):
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
...
<ApplicationIcon>WinUI3App1.ico</ApplicationIcon>
...
</PropertyGroup>
...
</Project>
The exe will now be presented with a nice icon in Explorer:
Then use this code in your window constructor:
public sealed partial class MainWindow : Window
{
private OtherWindow _myOtherWindow;
public MainWindow()
{
this.InitializeComponent();
var exeHandle = GetModuleHandle(Path.GetFileName(Process.GetCurrentProcess().MainModule.FileName));
var icon = LoadImage(exeHandle, IDI_APPLICATION, IMAGE_ICON, 16, 16, 0);
AppWindow.SetIcon(Win32Interop.GetIconIdFromIcon(icon));
// possibly call DestroyIcon after window has been closed
}
private const int IDI_APPLICATION = 32512;
private const int IMAGE_ICON = 1;
[DllImport("kernel32", CharSet = CharSet.Unicode)]
private static extern nint GetModuleHandle(string lpModuleName);
[DllImport("user32", CharSet = CharSet.Unicode)]
private static extern nint LoadImage(nint hinst, nint name, int type, int cx, int cy, int fuLoad);
// call this if/when you want to destroy the icon (window closed, etc.)
[DllImport("user32")]
private static extern bool DestroyIcon(nint hIcon);
}