SystemIcons
Each property of the SystemIcons class is an Icon object for Windows system-wide icons. This class cannot be inherited.
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using Windows.Win32;
using Windows.Win32.Foundation;
using Windows.Win32.UI.Shell;
using Windows.Win32.UI.WindowsAndMessaging;
namespace System.Drawing
{
[NullableContext(1)]
[Nullable(0)]
public static class SystemIcons
{
[Nullable(2)]
private static Icon s_application;
[Nullable(2)]
private static Icon s_asterisk;
[Nullable(2)]
private static Icon s_error;
[Nullable(2)]
private static Icon s_exclamation;
[Nullable(2)]
private static Icon s_hand;
[Nullable(2)]
private static Icon s_information;
[Nullable(2)]
private static Icon s_question;
[Nullable(2)]
private static Icon s_warning;
[Nullable(2)]
private static Icon s_winlogo;
[Nullable(2)]
private static Icon s_shield;
public static Icon Application => GetIcon(ref s_application, PInvokeCore.IDI_APPLICATION);
public static Icon Asterisk => GetIcon(ref s_asterisk, PInvokeCore.IDI_ASTERISK);
public static Icon Error => GetIcon(ref s_error, PInvokeCore.IDI_ERROR);
public static Icon Exclamation => GetIcon(ref s_exclamation, PInvokeCore.IDI_EXCLAMATION);
public static Icon Hand => GetIcon(ref s_hand, PInvokeCore.IDI_HAND);
public static Icon Information => GetIcon(ref s_information, PInvokeCore.IDI_INFORMATION);
public static Icon Question => GetIcon(ref s_question, PInvokeCore.IDI_QUESTION);
public static Icon Warning => GetIcon(ref s_warning, PInvokeCore.IDI_WARNING);
public static Icon WinLogo => GetIcon(ref s_winlogo, PInvokeCore.IDI_WINLOGO);
public static Icon Shield => s_shield ?? (s_shield = new Icon(typeof(SystemIcons), "ShieldIcon.ico"));
private static Icon GetIcon([Nullable(2)] ref Icon icon, PCWSTR iconId)
{
return icon ?? (icon = new Icon(PInvokeCore.LoadIcon(HINSTANCE.Null, iconId)));
}
public unsafe static Icon GetStockIcon(StockIconId stockIcon, StockIconOptions options = StockIconOptions.Default)
{
SHSTOCKICONINFO sHSTOCKICONINFO = default(SHSTOCKICONINFO);
sHSTOCKICONINFO.cbSize = (uint)sizeof(SHSTOCKICONINFO);
SHSTOCKICONINFO sHSTOCKICONINFO2 = sHSTOCKICONINFO;
Marshal.ThrowExceptionForHR(PInvoke.SHGetStockIconInfo((SHSTOCKICONID)stockIcon, (SHGSI_FLAGS)(options | (StockIconOptions)256), &sHSTOCKICONINFO2));
return new Icon(sHSTOCKICONINFO2.hIcon, true);
}
public unsafe static Icon GetStockIcon(StockIconId stockIcon, int size)
{
SHSTOCKICONINFO sHSTOCKICONINFO = default(SHSTOCKICONINFO);
sHSTOCKICONINFO.cbSize = (uint)sizeof(SHSTOCKICONINFO);
SHSTOCKICONINFO sHSTOCKICONINFO2 = sHSTOCKICONINFO;
Marshal.ThrowExceptionForHR(PInvoke.SHGetStockIconInfo((SHSTOCKICONID)stockIcon, SHGSI_FLAGS.SHGSI_ICONLOCATION, &sHSTOCKICONINFO2));
HICON handle = default(HICON);
Marshal.ThrowExceptionForHR(PInvoke.SHDefExtractIcon(sHSTOCKICONINFO2.szPath, sHSTOCKICONINFO2.iIcon, 0, &handle, null, (uint)(((ushort)size << 16) | (ushort)size)));
return new Icon(handle, true);
}
}
}