我如何获得各种
System.Drawing.Image
(s)和/或
System.Windows.MessageBoxImage
(小)在您的 XAML 中包含一个转换器(参见下面的代码)作为资源和一个图像控件。 此图像示例显示信息图标。
SystemIcons.Warning.ToBitmap();
这里是转换器的实现:
<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();
}
}
}
是应该包含这些图标的类,但是在 Windows 8.1 上(也可能在更早的版本上),
SystemIcons
中显示的图标与案例中 SystemIcons
上显示的图标不同星号、信息和问题。对话框上的图标看起来更加 flatter。参见 - 例如 - 问题图标:
对话框中的图标是原生对话框图标,后台窗体上最左边的图标是从
MessageBoxes
类中取出来的图标
有关如何从 MessageBox 获取图标的各种方法和详细信息,请参阅此answer
功能:
SHGetStockIconInfo
请注意如果此函数在
hIcon成员中返回一个图标句柄 psii指向的SHSTOCKICONINFO结构,你负责 当你不再需要它时,使用 DestroyIcon 释放图标。
当然要让它工作,你必须定义一些枚举和结构:
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();
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);
扩展 MessageBoxImage 枚举:
[DllImport("gdi32.dll", SetLastError = true)]
private static extern bool DeleteObject(IntPtr hObject);
public static ImageSource ToImageSource(this Icon icon)
{
Bitmap bitmap = icon.ToBitmap();
IntPtr hBitmap = bitmap.GetHbitmap();
ImageSource wpfBitmap = Imaging.CreateBitmapSourceFromHBitmap(
hBitmap,
IntPtr.Zero,
Int32Rect.Empty,
BitmapSizeOptions.FromEmptyOptions());
if (!DeleteObject(hBitmap))
{
throw new Win32Exception();
}
return wpfBitmap;
}
利润:(不会在这里发布整个 WPF 窗口代码,但“图标”和“图像”是“ImageSource”类型的属性,通过数据绑定链接到图像控件)
public static ImageSource ToImageSource(this MessageBoxImage enumValue)
{
switch (enumValue)
{
case MessageBoxImage.Error:
return SystemIcons.Error.ToImageSource();
case MessageBoxImage.Question:
return SystemIcons.Question.ToImageSource();
case MessageBoxImage.Warning:
return SystemIcons.Warning.ToImageSource();
case MessageBoxImage.Information:
return SystemIcons.Information.ToImageSource();
}
return null;
}
就这么简单。