将系统图标分配给标签

时间:2010-12-06 00:57:45

标签: c# wpf

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

3 个答案:

答案 0 :(得分:6)

System.Drawing.Icon myIcon = new System.Drawing.Icon(System.Drawing.SystemIcons.Question,32,32);
label.Image = myIcon.ToBitmap();

答案 1 :(得分: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;

答案 2 :(得分: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);