我如何获得的各种System.Windows.MessageBoxImage(S)和/或System.Windows.Forms.MessageBoxIcon(一个或多个)为System.Drawing.Image
Answer 1:
SystemIcons就是我一直在寻找:
SystemIcons.Warning.ToBitmap();
Answer 2:
您也可以在您的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}"/>
这里是转换器的实现:
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();
}
}
}
Answer 3:
正如其他人指出SystemIcons
是应该包含这些图标的类,但在Windows 8.1(也可能是在较早版本太)出现在图标SystemIcons
从上所显示的情况有所不同MessageBoxes
的星号,信息和问题的情况下。 该对话框上的图标看起来更平坦 。 见 - 例如 - 问号图标:
对话框中的图标是天然对话图标,并在后台窗体上最左边的图标是从检索的图标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();
请注意 :
如果这个函数返回SHSTOCKICONINFO结构的惠康成员的图标句柄由PSII指出,你有责任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);
文章来源: How do i get an Image for the various MessageBoxImage(s) or MessageBoxIcon(s)