AppWindow.SetIcon メソッド

定義

オーバーロード

名前 説明
SetIcon(IconId)

指定したアイコン ID を使用して、ウィンドウのアイコンを設定します。

SetIcon(String)

指定したアイコン パスを使用して、ウィンドウのアイコンを設定します。

SetIcon(IconId)

指定したアイコン 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)

パラメーター

iconId
IconId

アイコンの ID。

属性

これらの例では、プロジェクトの MyAppIcon.ico フォルダーに Assets という名前のアイコン ファイルがあり、そのビルド アクション[コンテンツ] に設定されていることを前提としています。

このコードは App.xaml.cs ファイルに配置されますが、 アイコンは Window クラスのファイルに設定することもできます (既定ではMainWindow.xaml.cs )。

この例では、 プラットフォーム呼び出し (P/Invoke) を使用して、Win32 LoadImage 関数を使用してアイコンへのハンドルを取得する方法を示します。 その後、 IconId を取得し、 SetIconの呼び出しで使用できます。

DLLImportは、LoadImageuser32.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,
}

この例では、前の例と同じことを行います。 ただし、 DLLImportを使用する代わりに、 CsWin32 NuGet パッケージを使用して LoadImage 関数にアクセスします。

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);
}

注釈

HICONLoadImage などのいずれかのアイコン関数からアイコン () へのハンドルが既にある場合は、GetIconIdFromIcon 相互運用 API を使用して IconId を取得できます。 その後、 IconIdSetIcon(IconId) メソッドに渡して、ウィンドウ アイコンを設定できます。

Important

.ico ファイルをアプリ資産に含める場合は、Visual Studioプロパティ ウィンドウで [ビルド アクション] を [コンテンツ] に設定する必要があります。

ビルド アクションがコンテンツに設定されていることを示すアイコン ファイルのプロパティ U I をVisual Studioします。

こちらもご覧ください

適用対象

SetIcon(String)

指定したアイコン パスを使用して、ウィンドウのアイコンを設定します。

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)

パラメーター

iconPath
String

Platform::String

winrt::hstring

アイコンのパスを指定します。

属性

注釈

SetIcon(String) メソッドは、.ico ファイルでのみ機能します。

通常、 SetIcon は、 App.xaml.cs または MainWindow.xaml.csから呼び出します。

Important

.ico ファイルをアプリ資産に含める場合は、Visual Studioプロパティ ウィンドウで [ビルド アクション] を [コンテンツ] に設定する必要があります。

ビルド アクションがコンテンツに設定されていることを示すアイコン ファイルのプロパティ U I をVisual Studioします。

このメソッドには、.ico ファイルへの完全修飾パスを渡します。 アプリ パッケージに含まれているアイコン ファイルへのパスを取得するには、いくつかの方法があります。

ここでは、アイコンは 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);
    }
}

次のメソッドは、アプリのパッケージへのパスを取得します。 その後、そのパスをアイコン ファイルへのローカル パスと組み合わせて、完全修飾パスを作成する必要があります。

  • .NET アプリの場合は、AppContext.BaseDirectory プロパティを使用できます。

    string path = AppContext.BaseDirectory;
    
  • アプリの対象が Windows 10 バージョン 2004 (10.0.19041.0) 以降の場合は、Package.InstalledPath プロパティを使用できます。

    string path = Package.Current.InstalledPath;
    
  • アプリがWindows 10バージョン 2004 (10.0.19041.0) より前のバージョンのWindowsを対象とする場合は、Package.InstalledLocation を使用してフォルダーを取得し、フォルダーの Path プロパティを使用できます。

    string path = Package.Current.InstalledLocation.Path;
    

アプリ パッケージへのパスを取得したら、それをアイコン ファイルへのパスと組み合わせます。 この例では、 Path.Combine メソッドを使用して、アイコン ファイルへの完全修飾パスを作成します。

ここでは、アイコンは 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"));
}

こちらもご覧ください

適用対象