将系统图标分配给标签

问题描述 投票:2回答:3

我目前正在开发一个c#应用程序。我正在创建自己的自定义消息框,所以我想知道是否可以将一个默认系统图标(即您在标准MessageBox上看到的图标之一)分配给标签。

c# wpf
3个回答
6
投票
System.Drawing.Icon myIcon = new System.Drawing.Icon(System.Drawing.SystemIcons.Question,32,32);
label.Image = myIcon.ToBitmap();

4
投票

您可以与System.Drawing和System.Windows.Forms进行互操作:

System.Drawing.Icon icon = System.Drawing.SystemIcons.Warning;

BitmapSource bs = System.Windows.Interop.Imaging.CreateBitmapSourceFromHIcon(icon.Handle, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());

image1.Source = bs;

1
投票

SLaks的解决方案可能是最简单的方法。如果由于某种原因,您不想使用Windows窗体功能,那么您自己很容易实现:

public enum SystemIcons
{
    Application = 32512,
    Error = 32513,
    Hand = Error,
    Question = 32514,
    Warning = 32515,
    Exclamation = Warning,
    Information = 32516,
    Asterisk = Information,
    WinLogo = 32517,
    Shield = 32518,
}

public static ImageSource LoadSystemIcon(SystemIcons iconId)
{
    string iconName = "#" + ((int)iconId);
    IntPtr hIcon = LoadIcon(IntPtr.Zero, iconName);
    if (hIcon == IntPtr.Zero)
        return null;

    return System.Windows.Interop.Imaging.CreateBitmapSourceFromHIcon(
        hIcon, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
}

[DllImport("user32.dll")]
static extern IntPtr LoadIcon(IntPtr hInstance, string lpIconName);
© www.soinside.com 2019 - 2024. All rights reserved.