Search code examples
svgwinui-3titlebarcustom-titlebar

How to change/Remove Window Title bar icon in WinUI 3


Is it possible to change the windows title bar icon in windows 10.

I have tried out the docs but they do not really provide any help to get it done. It says it can only be done in Windows 11 ??? providing an svg image should be the ideal solution.

enter image description here


Solution

  • This is the minimal code to change the icon. Worked on my Win 10 but you need to convert your *.svg to *.ico.

    using Microsoft.UI;
    using Microsoft.UI.Windowing;
    using Microsoft.UI.Xaml;
    using System;
    using WinRT.Interop;
    
    namespace TitleBarIconExample;
    
    public sealed partial class MainWindow : Window
    {
        public MainWindow()
        {
            this.InitializeComponent();
    
            if (AppWindowTitleBar.IsCustomizationSupported() is true)
            {
                IntPtr hWnd = WindowNative.GetWindowHandle(this);
                WindowId wndId = Win32Interop.GetWindowIdFromWindow(hWnd);
                AppWindow appWindow = AppWindow.GetFromWindowId(wndId);
                appWindow.SetIcon(@"Assets\WinUI.ico");
            }
        }
    }