AppWindow.SetIcon 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
重载
| 名称 | 说明 |
|---|---|
| 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.icoAssets其生成操作设置为“内容”。
此代码放置在 App.xaml.cs 文件中,但图标也可以设置为 Window 类的文件(MainWindow.xaml.cs 默认情况下)。
此示例演示如何使用 平台调用(P/Invoke) 使用 Win32 LoadImage 函数获取图标的句柄。 然后,可以获取 IconId 并在调用 SetIcon中使用它。
DLLImport用于访问LoadImage函数。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,
}
此示例执行与上一个示例相同的操作。 但是,它使用 DLLImport 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);
}
注解
如果已有一个HICON(如 CreateIcon 或 LoadImage)中的图标()句柄,则可以使用 GetIconIdFromIcon 互操作 API 获取 IconId。 然后,可以向方法传递IconIdSetIcon(IconId)以设置窗口图标。
Important
将.ico文件包含在应用资产中时,必须在“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属性”窗格中将“生成操作”设置为“内容”。
传递给此方法的字符串是 .ico 文件的完全限定路径。 可通过多种方式获取应用包中包含的图标文件的路径。
-
ms-appx使用 URI 方案引用图标文件,然后调用 StorageFile.GetFileFromApplicationUriAsync。 将文件 的路径 传递给SetIcon.
此处设置图标 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;如果应用仅面向版本 2004(10.0.19041.0)或更高版本Windows 10,则可以使用 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"));
}