App Icon - Windows App SDK

Hemanth B 886 Reputation points
2022-04-22T14:40:12.65+00:00

I've tried this https://github.com/microsoft/microsoft-ui-xaml/issues/4056 but no use.
How do I add an icon for my WinUI 3 Windows App SDK app both in window title bar as well as the taskbar?

Windows App SDK
Windows App SDK
A set of Microsoft open-source libraries, frameworks, components, and tools to be used in apps to access Windows platform functionality on many versions of Windows. Previously known as Project Reunion.
725 questions
0 comments No comments
{count} votes

Accepted answer
  1. Castorix31 81,721 Reputation points
    2022-04-22T15:32:27.22+00:00

    For the exe/Taskbar, in Properties :

    195598-winui-exe-ico.jpg

    For the main window, a way :

    IntPtr hWnd = WinRT.Interop.WindowNative.GetWindowHandle(this);  
    string sExe = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;  
    System.Drawing.Icon ico = System.Drawing.Icon.ExtractAssociatedIcon(sExe);  
    SendMessage(hWnd, WM_SETICON, ICON_BIG, ico.Handle);  
    

    with :

    public const int ICON_SMALL = 0;  
    public const int ICON_BIG = 1;  
    public const int ICON_SMALL2 = 2;  
      
    public const int WM_GETICON = 0x007F;  
    public const int WM_SETICON = 0x0080;  
      
    [DllImport("User32.dll", SetLastError = true, CharSet = CharSet.Auto)]  
    public static extern int SendMessage(IntPtr hWnd, uint msg, int wParam, IntPtr lParam);  
    
    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Marty Ellis 5 Reputation points
    2023-05-22T17:59:59.7166667+00:00

    I had to add in this to get it to show in the title bar - because I am using a hack to modify the title bar when the theme changes. The base code always picks the light theme for the title bar.

    
        public MainViewModel DataContext { get; set; }
        public MainViewModel ViewModel => DataContext;
        public MainWindow()
        {
            this.InitializeComponent();
            var content = (Content as FrameworkElement)!;
            content.ActualThemeChanged += (s, e) =>
            {
                ModifyTitlebarTheme();
            };
            ModifyTitlebarTheme();
    
            DataContext = Ioc.Default.GetRequiredService<MainViewModel>();
            ApplicationView.PreferredLaunchViewSize = new Size(1525, 861);
            ApplicationView.PreferredLaunchWindowingMode = ApplicationViewWindowingMode.PreferredLaunchViewSize;
    
            var hWnd = WinRT.Interop.WindowNative.GetWindowHandle(this);
            var windowId = Win32Interop.GetWindowIdFromWindow(hWnd);
            var appWindow = AppWindow.GetFromWindowId(windowId);
            var size = new Windows.Graphics.SizeInt32();
            size.Width = 1150;
            size.Height = 1200;
            appWindow.Resize(size);
    
            Microsoft.UI.Windowing.DisplayArea displayArea = Microsoft.UI.Windowing.DisplayArea.GetFromWindowId(windowId, Microsoft.UI.Windowing.DisplayAreaFallback.Nearest);
            if (displayArea is not null)
            {
                var CenteredPosition = appWindow.Position;
                CenteredPosition.X = ((displayArea.WorkArea.Width - appWindow.Size.Width) / 2);
                CenteredPosition.Y = ((displayArea.WorkArea.Height - appWindow.Size.Height) / 2);
                appWindow.Move(CenteredPosition);
            }
    
            var appSplash = ((App)Application.Current)._splashScreen;
            appSplash?.CenterToScreen(hWnd);
            appSplash?.HideSplash(2);
            appWindow.Title = $"Multi-Site ARC Plus Config Archive Tool";
    
            hWnd = WinRT.Interop.WindowNative.GetWindowHandle(this);
            string? sExe = Environment.ProcessPath;
            if (sExe != null)
            {
                System.Drawing.Icon? ico = System.Drawing.Icon.ExtractAssociatedIcon(sExe);
                if (ico != null)
                {
                    SendMessage(hWnd, WM_SETICON, ICON_BIG, ico.Handle);
    // Had to add these
                    SendMessage(hWnd, WM_SETICON, ICON_SMALL, ico.Handle);
                    SendMessage(hWnd, WM_SETICON, ICON_SMALL2, ico.Handle);
                }
            }
        }
        private void ModifyTitlebarTheme()
        {
            var content = (Content as FrameworkElement)!;
            var value = content.ActualTheme;
    
            var titleBar = AppWindow.TitleBar;
            if (value == ElementTheme.Light)
            {
                titleBar.ForegroundColor = Colors.Black;
                titleBar.BackgroundColor = Colors.White;
                titleBar.InactiveForegroundColor = Colors.Gray;
                titleBar.InactiveBackgroundColor = Colors.White;
    
                titleBar.ButtonForegroundColor = Colors.Black;
                titleBar.ButtonBackgroundColor = Colors.White;
                titleBar.ButtonInactiveForegroundColor = Colors.Gray;
                titleBar.ButtonInactiveBackgroundColor = Colors.White;
    
                titleBar.ButtonHoverForegroundColor = Colors.Black;
                titleBar.ButtonHoverBackgroundColor = Color.FromArgb(255, 245, 245, 245);
                titleBar.ButtonPressedForegroundColor = Colors.Black;
                titleBar.ButtonPressedBackgroundColor = Colors.White;
            }
            else if (value == ElementTheme.Dark)
            {
                titleBar.ForegroundColor = Colors.White;
                titleBar.BackgroundColor = Color.FromArgb(255, 31, 31, 31);
                titleBar.InactiveForegroundColor = Colors.Gray;
                titleBar.InactiveBackgroundColor = Color.FromArgb(255, 31, 31, 31);
    
                titleBar.ButtonForegroundColor = Colors.White;
                titleBar.ButtonBackgroundColor = Color.FromArgb(255, 31, 31, 31);
                titleBar.ButtonInactiveForegroundColor = Colors.Gray;
                titleBar.ButtonInactiveBackgroundColor = Color.FromArgb(255, 31, 31, 31);
    
                titleBar.ButtonHoverForegroundColor = Colors.White;
                titleBar.ButtonHoverBackgroundColor = Color.FromArgb(255, 51, 51, 51);
                titleBar.ButtonPressedForegroundColor = Colors.White;
                titleBar.ButtonPressedBackgroundColor = Colors.Gray;
            }
    
            // Fixed bug where icon background color was not applied 
            titleBar.IconShowOptions = IconShowOptions.HideIconAndSystemMenu;
            titleBar.IconShowOptions = IconShowOptions.ShowIconAndSystemMenu;
        }
    
    
    1 person found this answer helpful.
    0 comments No comments