Share via


AppWindow.SetIcon Method

Definition

Overloads

SetIcon(String)

Sets the icon for the window using the specified icon path.

SetIcon(IconId)

Sets the icon for the window using the specified icon ID.

SetIcon(String)

Sets the icon for the window using the specified icon path.

public:
 virtual void SetIcon(Platform::String ^ iconPath) = SetIcon;
/// [Windows.Foundation.Metadata.DefaultOverload]
/// [Windows.Foundation.Metadata.Overload("SetIcon")]
void SetIcon(winrt::hstring const& iconPath);
[Windows.Foundation.Metadata.DefaultOverload]
[Windows.Foundation.Metadata.Overload("SetIcon")]
public void SetIcon(string iconPath);
function setIcon(iconPath)
Public Sub SetIcon (iconPath As String)

Parameters

iconPath
String

Platform::String

winrt::hstring

The path of the icon.

Attributes

Remarks

The SetIcon(String) method works only with .ico files.

You typically call SetIcon either from App.xaml.cs or from MainWindow.xaml.cs.

Important

When you include the .ico file with your apps assets, you have to set the Build Action to Content in the Visual Studio properties pane.

Visual Studio properties U I for the icon file showing build action set to content.

The string you pass to this method is the fully qualified path to the .ico file. There are several ways to get the path to an icon file that's included in your app package.

Here, the icon is set in App.xaml.cs.

protected async override void OnLaunched(Microsoft.UI.Xaml.LaunchActivatedEventArgs args)
{
    m_window = new MainWindow();
    await SetIconAsync();
    m_window.Activate();
}

private async Task SetIconAsync()
{
    Uri uri = new Uri("ms-appx:///Assets/MyAppIcon.ico");
    StorageFile? storageFile = null;
    try
    {
        storageFile = await StorageFile.GetFileFromApplicationUriAsync(uri);
    }
    catch (Exception ex)
    {
        // Use default icon.
    }

    if (storageFile is not null)
    {
        m_window?.AppWindow.SetIcon(storageFile.Path);
    }
}

The following methods get the path to the app's package. Then you need to combine that path with the local path to the icon file to create the fully qualified path.

  • For .NET apps, you can use the AppContext.BaseDirectory property.

    string path = AppContext.BaseDirectory;
    
  • If your app will only target Windows 10, version 2004 (10.0.19041.0) or later, you can use the Package.InstalledPath property.

    string path = Package.Current.InstalledPath;
    
  • If your app targets versions of Windows prior to Windows 10, version 2004 (10.0.19041.0), you can use Package.InstalledLocation to get the folder, then use the folder's Path property.

    string path = Package.Current.InstalledLocation.Path;
    

After you have the path to the app package, combine it with the path to the icon file. In this example, we use the Path.Combine method to create the fully qualified path to the icon file.

Here, the icon is set in MainWindow.xaml.cs.

public MainWindow()
{
    this.InitializeComponent();

    //string path = Package.Current.InstalledLocation.Path;
    // OR
    //string path = Package.Current.InstalledPath;
    // OR
    string path = AppContext.BaseDirectory;
    string iconFileName = "Assets/MyAppIcon.ico";
    string iconPath = Path.Combine(path, iconFileName);
    AppWindow.SetIcon(iconPath);

    // This code can also be condensed like this:
    // AppWindow.SetIcon(Path.Combine(AppContext.BaseDirectory, "Assets\\MyAppIcon.ico"));
}

See also

Applies to

SetIcon(IconId)

Sets the icon for the window using the specified icon ID.

public:
 virtual void SetIcon(IconId iconId) = SetIcon;
/// [Windows.Foundation.Metadata.Overload("SetIconWithIconId")]
void SetIcon(IconId const& iconId);
[Windows.Foundation.Metadata.Overload("SetIconWithIconId")]
public void SetIcon(IconId iconId);
function setIcon(iconId)
Public Sub SetIcon (iconId As IconId)

Parameters

iconId
IconId

The ID of the icon.

Attributes

Examples

These examples assume you have an icon file named MyAppIcon.ico in the Assets folder of your project with its Build Action set to Content.

This code is placed in the App.xaml.cs file, but the icon can also be set in the file for the Window class (MainWindow.xaml.cs by default).

This example shows how to use Platform Invoke (P/Invoke) get a handle to an icon using the Win32 LoadImage function. You can then get the IconId and use it in the call to SetIcon.

DLLImport is used to access the LoadImage function in user32.dll.

using Microsoft.UI;
using System;
using System.Runtime.InteropServices;

// ...

protected override void OnLaunched(Microsoft.UI.Xaml.LaunchActivatedEventArgs args)
{
    m_window = new MainWindow();
    LoadIconById("Assets/MyAppIcon.ico");
    m_window.Activate();
}

private void LoadIconById(string iconName)
{
    nint hwnd = WinRT.Interop.WindowNative.GetWindowHandle(m_window);
    IntPtr hIcon = LoadImage(
        IntPtr.Zero, iconName, ImageType.IMAGE_ICON, 16, 16, LoadImageFlags.LR_LOADFROMFILE);
    IconId iconID = Microsoft.UI.Win32Interop.GetIconIdFromIcon(hIcon);

    // SetIcon
    m_window?.AppWindow.SetIcon(iconID);
}

[DllImport("user32.dll", SetLastError = true)]
public static extern unsafe IntPtr LoadImage(
    IntPtr hInst,
    string name,
    ImageType type,
    int cx,
    int cy,
    LoadImageFlags fuLoad);

public enum ImageType : uint
{
    IMAGE_BITMAP = 0,
    IMAGE_ICON = 1,
    IMAGE_CURSOR = 2,
}

[Flags]
public enum LoadImageFlags : uint
{
    LR_CREATEDIBSECTION = 0x00002000,
    LR_DEFAULTCOLOR = 0x0,
    LR_DEFAULTSIZE = 0x00000040,
    LR_LOADFROMFILE = 0x00000010,
    LR_LOADMAP3DCOLORS = 0x00001000,
    LR_LOADTRANSPARENT = 0x00000020,
    LR_MONOCHROME = 0x00000001,
    LR_SHARED = 0x00008000,
    LR_VGACOLOR = 0x00000080,
}

This example does the same thing as the previous example. However, instead of using DLLImport, it uses the CsWin32 NuGet package to access the LoadImage function.

using Microsoft.UI;
using Microsoft.Win32.SafeHandles;
using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
using Windows.Win32.UI.WindowsAndMessaging;

// ...

protected override void OnLaunched(Microsoft.UI.Xaml.LaunchActivatedEventArgs args)
{
    m_window = new MainWindow();
    LoadIconById("Assets/MyAppIcon.ico");
    m_window.Activate();
}

private void LoadIconById(string iconName)
{
    nint hwnd = WinRT.Interop.WindowNative.GetWindowHandle(m_window);
    SafeFileHandle hIcon_handle = Windows.Win32.PInvoke.LoadImage(
        null, iconName, GDI_IMAGE_TYPE.IMAGE_ICON, 32, 32, IMAGE_FLAGS.LR_LOADFROMFILE);

    if (hIcon_handle.IsInvalid || hIcon_handle.IsClosed)
    {
        Win32Exception ex = new Win32Exception(Marshal.GetLastWin32Error());
        throw ex;
    }
    bool success = false;
    hIcon_handle.DangerousAddRef(ref success);
    IconId iconID = Win32Interop.GetIconIdFromIcon(hIcon_handle.DangerousGetHandle());

    // SetIcon
    m_window?.AppWindow.SetIcon(iconID);
}

Remarks

If you already have a handle to an icon (HICON) from one of the Icon functions like CreateIcon or LoadImage, you can use the GetIconIdFromIcon interop API to get an IconId. You can then pass the IconId to the SetIcon(IconId) method to set your window icon.

Important

When you include the .ico file with your apps assets, you have to set the Build Action to Content in the Visual Studio properties pane.

Visual Studio properties U I for the icon file showing build action set to content.

See also

Applies to