如何获取各种MessageBoxImage或MessageBoxIcon的图像

Sim*_*mon 45 .net wpf messagebox winforms

我如何为各种System.Windows.MessageBoxImage和/或System.Windows.Forms.MessageBoxIcon获取System.Drawing.Image

Sim*_*mon 57

我正在寻找SystemIcons:

SystemIcons.Warning.ToBitmap();
Run Code Online (Sandbox Code Playgroud)


Zam*_*oni 32

您还可以在XAML中包含SystemIcons,如下所示:

包括转换器(请参阅下面的代码)作为资源,以及XAML中的Image控件.此图像示例显示信息图标.

     <Window.Resources>
        <Converters:SystemIconConverter x:Key="iconConverter"/>
     </Window.Resources>

     <Image Visibility="Visible"  
            Margin="10,10,0,1"
            Stretch="Uniform"
            MaxHeight="25"
            VerticalAlignment="Top"
            HorizontalAlignment="Left"
            Source="{Binding Converter={StaticResource iconConverter}, ConverterParameter=Information}"/>
Run Code Online (Sandbox Code Playgroud)

以下是转换器的实现:

using System;
using System.Drawing;
using System.Globalization;
using System.Reflection;
using System.Windows;
using System.Windows.Data;
using System.Windows.Interop;
using System.Windows.Media.Imaging;

namespace Converters
{
   [ValueConversion(typeof(string), typeof(BitmapSource))]
   public class SystemIconConverter : IValueConverter
   {
      public object Convert(object value, Type type, object parameter, CultureInfo culture)
      {
         Icon icon = (Icon)typeof(SystemIcons).GetProperty(parameter.ToString(), BindingFlags.Public | BindingFlags.Static).GetValue(null, null);
         BitmapSource bs = Imaging.CreateBitmapSourceFromHIcon(icon.Handle, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
         return bs;
      }

      public object ConvertBack(object value, Type type, object parameter, CultureInfo culture)
      {
         throw new NotSupportedException();
      }
   }
}
Run Code Online (Sandbox Code Playgroud)


qqb*_*enq 14

正如其他人所说的SystemIcons是应该包含这些图标的类,但是在Windows 8.1上(也可能在早期版本中),存在的图标SystemIconsMessageBoxesAsterisk,Information and Question中显示的图标不同.对话框上的图标看起来更平坦.请参阅 - 例如 - 问题图标:

问题图标

对话框中的图标是本机对话框图标,背景中表单上最左边的图标是从SystemIcons类中检索的图标.

有关如何从MessageBox获取图标的各种方法和详细信息,请参阅此答案,但我在此处包含一个快速摘要,仅为了完整性:

你应该使用这个SHGetStockIconInfo功能:

 SHSTOCKICONINFO sii = new SHSTOCKICONINFO();
 sii.cbSize = (UInt32)Marshal.SizeOf(typeof(SHSTOCKICONINFO));

 Marshal.ThrowExceptionForHR(SHGetStockIconInfo(SHSTOCKICONID.SIID_INFO,
         SHGSI.SHGSI_ICON ,
         ref sii));
 pictureBox1.Image = Icon.FromHandle(sii.hIcon).ToBitmap();
Run Code Online (Sandbox Code Playgroud)

注意:

如果此函数返回psii指向的SHSTOCKICONINFO结构的hIcon成员中的 图标句柄,则您负责在不再需要时使用DestroyIcon释放该图标.

当然,要实现这一点,您必须定义一些枚举和结构:

public enum SHSTOCKICONID : uint
{
    //...
    SIID_INFO = 79,
    //...
}

[Flags]
public enum SHGSI : uint
{
    SHGSI_ICONLOCATION = 0,
    SHGSI_ICON = 0x000000100,
    SHGSI_SYSICONINDEX = 0x000004000,
    SHGSI_LINKOVERLAY = 0x000008000,
    SHGSI_SELECTED = 0x000010000,
    SHGSI_LARGEICON = 0x000000000,
    SHGSI_SMALLICON = 0x000000001,
    SHGSI_SHELLICONSIZE = 0x000000004
}

[StructLayoutAttribute(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct SHSTOCKICONINFO
{
    public UInt32 cbSize;
    public IntPtr hIcon;
    public Int32 iSysIconIndex;
    public Int32 iIcon;
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260/*MAX_PATH*/)]
    public string szPath;
}

[DllImport("Shell32.dll", SetLastError = false)]
public static extern Int32 SHGetStockIconInfo(SHSTOCKICONID siid, SHGSI uFlags, ref SHSTOCKICONINFO psii);
Run Code Online (Sandbox Code Playgroud)