[ACCEPTED]-How do i get an Image for the various MessageBoxImage(s) or MessageBoxIcon(s)-messagebox
You can also include SystemIcons in your 5 XAML as follows:
Include a converter (see 4 code below) as a Resource, and an Image 3 control in your XAML. This Image sample 2 here shows the information icon.
<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}"/>
Here is 1 the implementation for the converter:
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();
}
}
}
As others have stated SystemIcons
is the class that 20 should contain those icons, but on Windows 19 8.1 (and possibly on earlier versions too) the 18 icons present in the SystemIcons
differ from the ones 17 displayed on the MessageBoxes
in the case of Asterisk, Information 16 and Question. The icons on the dialog look 15 much flatter. See - for example - the Question 14 icon:
The icon in the dialog is the native 13 dialog icon, and the leftmost icon on the 12 form in the background is the icon retrieved 11 from the SystemIcons
class.
For various methods and 10 details on how to get the icon from the 9 MessageBox see this answer, but I include here 8 a quick summary, just for the sake of completeness:
You 7 should use the SHGetStockIconInfo
function:
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();
Please note:
If this 6 function returns an icon handle in the hIcon member 5 of the SHSTOCKICONINFO structure pointed to by psii, you 4 are responsible for freeing the icon with 3 DestroyIcon when you no longer need it.
Of course for 2 this to work you will have to define a few 1 enums and structs:
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);
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.