如何在WPF应用程序中使用标准的Windows警告/错误图标?

时间:2021-05-17 07:14:28

I'm making a custom error dialog in my WPF app and I want to use a standard windows error icon. Can I get the OS-specific icon from WPF? If not, does anyone know where to get .pngs with transparency of them? Or know where in Windows to go extract them from?

我在我的WPF应用中创建了一个自定义错误对话框,我想使用一个标准的windows错误图标。我可以从WPF上获得os特定的图标吗?如果没有,有没有人知道在哪里可以找到透明的。pngs ?或者知道在Windows中从哪里提取它们?

So far my searches have turned up nothing.

到目前为止,我的搜索一无所获。

10 个解决方案

#1


33  

There is a SystemIcons class, but it need adjustment for WPF needs (i.e. converting Icon to ImageSource).

有一个systemicon类,但是它需要根据WPF的需要进行调整(即将图标转换为ImageSource)。

#2


29  

About using Standard Microsoft Icons.

关于使用标准的微软图标。

The vast majority of developers out there don't know that Visual Studio comes with an Image Library. So here goes two links that highlight it:

绝大多数开发人员都不知道Visual Studio附带了一个图像库。这里有两个链接,突出显示:

About using Microsoft Visual Studio 2010 Image Library.

关于使用微软Visual Studio 2010图像库。

About using Microsoft Visual Studio 2008 Image Library.

关于使用microsoftvisual Studio 2008图像库。

#3


7  

This is how I used a System icon in XAML:

这就是我在XAML中使用系统图标的方式:

xmlns:draw="clr-namespace:System.Drawing;assembly=System.Drawing"
...
<Image Source="{Binding Source={x:Static draw:SystemIcons.Warning},
        Converter={StaticResource IconToImageSourceConverter},
        Mode=OneWay}" />

I used this converter to turn an Icon to ImageSource:

我用这个转换器将图标转换为ImageSource:

public class IconToImageSourceConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var icon = value as Icon;
        if (icon == null)
        {
            Trace.TraceWarning("Attempted to convert {0} instead of Icon object in IconToImageSourceConverter", value);
            return null;
        }

        ImageSource imageSource = Imaging.CreateBitmapSourceFromHIcon(
            icon.Handle,
            Int32Rect.Empty,
            BitmapSizeOptions.FromEmptyOptions());
        return imageSource;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

#4


6  

In Visual Studio, use File + Open + File and select c:\windows\system32\user32.dll. Open the Icon node and double-click 103. On my machine that's the Error icon. Back, right-click it and select Export to save it to a file.

在Visual Studio中,使用File + Open +文件并选择c:\windows\system32\user32.dll。打开图标节点,双击103。在我的机器上,这是错误的图标。返回,右键单击它并选择Export将其保存到文件中。

That's the iffy way. These icons are also available in Visual Studio. From your Visual Studio install directory, navigate to Common7\VS2008ImageLibrary\xxxx\VS2008ImageLibrary.zip + VS2008ImageLibrary\Annotations&Buttons\ico_format\WinVista\error.ico. The redist.txt file in the Visual Studio install directly explicitly gives you the right to use this icon in your own application.

这是不确定的。这些图标也可以在Visual Studio中使用。从Visual Studio安装目录中导航到Common7\VS2008ImageLibrary\xxxx\VS2008ImageLibrary。zip + VS2008ImageLibrary \ Annotations&Buttons \ ico_format \ WinVista \ error.ico。redist。Visual Studio安装中的txt文件直接显式地允许您在自己的应用程序中使用此图标。

#5


4  

  • p/invoke LoadImage with the OIC_ERROR (other icons available, but you wanted the error/stop icon), you need the LR_SHARED flag.
  • p/调用LoadImage并带有OIC_ERROR(其他图标可用,但是需要error/stop图标),需要LR_SHARED标志。
  • If you want a different size, p/invoke CopyImage
  • 如果需要不同的大小,请p/invoke CopyImage
  • Call System.Drawing.Icon.FromHandle with the IntPtr you got from LoadImage
  • System.Drawing.Icon打电话。从LoadImage得到的IntPtr
  • For GDI+, Draw your Drawing.Icon, or Clone it for later use.
  • 对于GDI+,画出你的图画。图标,或克隆它供以后使用。
  • For WPF, turn it into a BitmapSource
  • 对于WPF,将它转换为位图源
  • p/invoke DestroyIcon if you used CopyImage
  • 如果你使用的是CopyImage,那么就调用毁灭图标。

You can use .NET's SystemIcons class for roughly the first three steps if the default size is ok, see modosansreves answer

如果默认大小合适,您可以使用.NET的systemicon类来完成前三个步骤,请参阅modosansreves的回答

So it could be as simple as:

所以可以这么简单:

 Imaging.CreateBitmapSourceFromHIcon(SystemIcons.Error.Handle)

#6


2  

Use this:

用这个:

using System;
using System.Drawing;
using System.Windows;
using System.Windows.Interop;
using System.Windows.Markup;
using System.Windows.Media.Imaging;

using Extensions
{
    [ContentProperty("Icon")]
    public class ImageSourceFromIconExtension : MarkupExtension
    {
        public Icon Icon { get; set; }

        public ImageSourceFromIconExtension()
        {
        }

        public ImageSourceFromIconExtension(Icon icon)
        {
            Icon = icon;
        }

        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            return Imaging.CreateBitmapSourceFromHIcon(Icon.Handle, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
        }
    }
}

Don't forget to add System.Drawing reference to your project from global location.

不要忘记添加系统。从全局位置绘制对项目的引用。

Then use this in your xaml:

然后在你的xaml中使用这个:

<WindowOrSomething x:Class="BlaBlaWindow"
    xmlns:draw="clr-namespace:System.Drawing;assembly=System.Drawing"
    xmlns:ext="clr-namespace:Extensions">
    <Image Source="{ext:ImageSourceFromIcon {x:Static draw:SystemIcons.Error}}"/>
</WindowOrSomething>

#7


1  

Can't you simply use Windows API?

你不能简单地使用Windows API吗?

Delphi Example:

Delphi的例子:

procedure TForm1.FormClick(Sender: TObject);
var
  errIcon: HICON;
begin
  errIcon := LoadIcon(0, IDI_ERROR);
  DrawIcon(Canvas.Handle, 10, 10, errIcon)
end;

#8


0  

You can draw it like this:

你可以这样画:

Graphics g = this.CreateGraphics();
g.DrawIcon(SystemIcons.Question, 40, 40);

#9


0  

use SystemIcons and convert them to an ImageSource like so:

使用systemicon并将它们转换为ImageSource,如下所示:

try   
{
    var temp = SystemIcons.WinLogo.ToBitmap();  //Your icon here
    BitmapImage bitmapImage = new BitmapImage();
    using (MemoryStream memory = new MemoryStream())
    {
       temp.Save(memory, ImageFormat.Png);
       memory.Position = 0;
       bitmapImage.BeginInit();
       bitmapImage.StreamSource = memory;
       bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
       bitmapImage.EndInit();
    }
    this.Icon = bitmapImage;
}
catch (Exception ex)
{
    this.Icon = null;
}

#10


0  

Convert SystemIcons.Error etc to .png (maintains transparency) then add output to resources and use as usual in WPF Image control.

SystemIcons进行转换。错误等等。png(保持透明度)然后将输出添加到资源中,并在WPF图像控制中使用。

    System.Drawing.SystemIcons.Error.ToBitmap().Save("Error.png", System.Drawing.Imaging.ImageFormat.Png);

#1


33  

There is a SystemIcons class, but it need adjustment for WPF needs (i.e. converting Icon to ImageSource).

有一个systemicon类,但是它需要根据WPF的需要进行调整(即将图标转换为ImageSource)。

#2


29  

About using Standard Microsoft Icons.

关于使用标准的微软图标。

The vast majority of developers out there don't know that Visual Studio comes with an Image Library. So here goes two links that highlight it:

绝大多数开发人员都不知道Visual Studio附带了一个图像库。这里有两个链接,突出显示:

About using Microsoft Visual Studio 2010 Image Library.

关于使用微软Visual Studio 2010图像库。

About using Microsoft Visual Studio 2008 Image Library.

关于使用microsoftvisual Studio 2008图像库。

#3


7  

This is how I used a System icon in XAML:

这就是我在XAML中使用系统图标的方式:

xmlns:draw="clr-namespace:System.Drawing;assembly=System.Drawing"
...
<Image Source="{Binding Source={x:Static draw:SystemIcons.Warning},
        Converter={StaticResource IconToImageSourceConverter},
        Mode=OneWay}" />

I used this converter to turn an Icon to ImageSource:

我用这个转换器将图标转换为ImageSource:

public class IconToImageSourceConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var icon = value as Icon;
        if (icon == null)
        {
            Trace.TraceWarning("Attempted to convert {0} instead of Icon object in IconToImageSourceConverter", value);
            return null;
        }

        ImageSource imageSource = Imaging.CreateBitmapSourceFromHIcon(
            icon.Handle,
            Int32Rect.Empty,
            BitmapSizeOptions.FromEmptyOptions());
        return imageSource;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

#4


6  

In Visual Studio, use File + Open + File and select c:\windows\system32\user32.dll. Open the Icon node and double-click 103. On my machine that's the Error icon. Back, right-click it and select Export to save it to a file.

在Visual Studio中,使用File + Open +文件并选择c:\windows\system32\user32.dll。打开图标节点,双击103。在我的机器上,这是错误的图标。返回,右键单击它并选择Export将其保存到文件中。

That's the iffy way. These icons are also available in Visual Studio. From your Visual Studio install directory, navigate to Common7\VS2008ImageLibrary\xxxx\VS2008ImageLibrary.zip + VS2008ImageLibrary\Annotations&Buttons\ico_format\WinVista\error.ico. The redist.txt file in the Visual Studio install directly explicitly gives you the right to use this icon in your own application.

这是不确定的。这些图标也可以在Visual Studio中使用。从Visual Studio安装目录中导航到Common7\VS2008ImageLibrary\xxxx\VS2008ImageLibrary。zip + VS2008ImageLibrary \ Annotations&Buttons \ ico_format \ WinVista \ error.ico。redist。Visual Studio安装中的txt文件直接显式地允许您在自己的应用程序中使用此图标。

#5


4  

  • p/invoke LoadImage with the OIC_ERROR (other icons available, but you wanted the error/stop icon), you need the LR_SHARED flag.
  • p/调用LoadImage并带有OIC_ERROR(其他图标可用,但是需要error/stop图标),需要LR_SHARED标志。
  • If you want a different size, p/invoke CopyImage
  • 如果需要不同的大小,请p/invoke CopyImage
  • Call System.Drawing.Icon.FromHandle with the IntPtr you got from LoadImage
  • System.Drawing.Icon打电话。从LoadImage得到的IntPtr
  • For GDI+, Draw your Drawing.Icon, or Clone it for later use.
  • 对于GDI+,画出你的图画。图标,或克隆它供以后使用。
  • For WPF, turn it into a BitmapSource
  • 对于WPF,将它转换为位图源
  • p/invoke DestroyIcon if you used CopyImage
  • 如果你使用的是CopyImage,那么就调用毁灭图标。

You can use .NET's SystemIcons class for roughly the first three steps if the default size is ok, see modosansreves answer

如果默认大小合适,您可以使用.NET的systemicon类来完成前三个步骤,请参阅modosansreves的回答

So it could be as simple as:

所以可以这么简单:

 Imaging.CreateBitmapSourceFromHIcon(SystemIcons.Error.Handle)

#6


2  

Use this:

用这个:

using System;
using System.Drawing;
using System.Windows;
using System.Windows.Interop;
using System.Windows.Markup;
using System.Windows.Media.Imaging;

using Extensions
{
    [ContentProperty("Icon")]
    public class ImageSourceFromIconExtension : MarkupExtension
    {
        public Icon Icon { get; set; }

        public ImageSourceFromIconExtension()
        {
        }

        public ImageSourceFromIconExtension(Icon icon)
        {
            Icon = icon;
        }

        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            return Imaging.CreateBitmapSourceFromHIcon(Icon.Handle, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
        }
    }
}

Don't forget to add System.Drawing reference to your project from global location.

不要忘记添加系统。从全局位置绘制对项目的引用。

Then use this in your xaml:

然后在你的xaml中使用这个:

<WindowOrSomething x:Class="BlaBlaWindow"
    xmlns:draw="clr-namespace:System.Drawing;assembly=System.Drawing"
    xmlns:ext="clr-namespace:Extensions">
    <Image Source="{ext:ImageSourceFromIcon {x:Static draw:SystemIcons.Error}}"/>
</WindowOrSomething>

#7


1  

Can't you simply use Windows API?

你不能简单地使用Windows API吗?

Delphi Example:

Delphi的例子:

procedure TForm1.FormClick(Sender: TObject);
var
  errIcon: HICON;
begin
  errIcon := LoadIcon(0, IDI_ERROR);
  DrawIcon(Canvas.Handle, 10, 10, errIcon)
end;

#8


0  

You can draw it like this:

你可以这样画:

Graphics g = this.CreateGraphics();
g.DrawIcon(SystemIcons.Question, 40, 40);

#9


0  

use SystemIcons and convert them to an ImageSource like so:

使用systemicon并将它们转换为ImageSource,如下所示:

try   
{
    var temp = SystemIcons.WinLogo.ToBitmap();  //Your icon here
    BitmapImage bitmapImage = new BitmapImage();
    using (MemoryStream memory = new MemoryStream())
    {
       temp.Save(memory, ImageFormat.Png);
       memory.Position = 0;
       bitmapImage.BeginInit();
       bitmapImage.StreamSource = memory;
       bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
       bitmapImage.EndInit();
    }
    this.Icon = bitmapImage;
}
catch (Exception ex)
{
    this.Icon = null;
}

#10


0  

Convert SystemIcons.Error etc to .png (maintains transparency) then add output to resources and use as usual in WPF Image control.

SystemIcons进行转换。错误等等。png(保持透明度)然后将输出添加到资源中,并在WPF图像控制中使用。

    System.Drawing.SystemIcons.Error.ToBitmap().Save("Error.png", System.Drawing.Imaging.ImageFormat.Png);