SystemFonts
Specifies the fonts used to display text in Windows display elements.
using System.Drawing.Interop;
using System.IO;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using Windows.Win32;
using Windows.Win32.Graphics.Gdi;
using Windows.Win32.UI.WindowsAndMessaging;
namespace System.Drawing
{
[NullableContext(2)]
[Nullable(0)]
public static class SystemFonts
{
public static Font CaptionFont {
get {
Font font = null;
if (GetNonClientMetrics(out NONCLIENTMETRICSW metrics)) {
font = GetFontFromData(ref metrics.lfCaptionFont);
font.SetSystemFontName("CaptionFont");
}
return font;
}
}
public static Font SmallCaptionFont {
get {
Font font = null;
if (GetNonClientMetrics(out NONCLIENTMETRICSW metrics)) {
font = GetFontFromData(ref metrics.lfSmCaptionFont);
font.SetSystemFontName("SmallCaptionFont");
}
return font;
}
}
public static Font MenuFont {
get {
Font font = null;
if (GetNonClientMetrics(out NONCLIENTMETRICSW metrics)) {
font = GetFontFromData(ref metrics.lfMenuFont);
font.SetSystemFontName("MenuFont");
}
return font;
}
}
public static Font StatusFont {
get {
Font font = null;
if (GetNonClientMetrics(out NONCLIENTMETRICSW metrics)) {
font = GetFontFromData(ref metrics.lfStatusFont);
font.SetSystemFontName("StatusFont");
}
return font;
}
}
public static Font MessageBoxFont {
get {
Font font = null;
if (GetNonClientMetrics(out NONCLIENTMETRICSW metrics)) {
font = GetFontFromData(ref metrics.lfMessageFont);
font.SetSystemFontName("MessageBoxFont");
}
return font;
}
}
public unsafe static Font IconTitleFont {
get {
Font font = null;
LOGFONT logFont = default(LOGFONT);
if ((bool)PInvokeCore.SystemParametersInfo(SYSTEM_PARAMETERS_INFO_ACTION.SPI_GETICONTITLELOGFONT, (uint)sizeof(LOGFONT), &logFont, (SYSTEM_PARAMETERS_INFO_UPDATE_FLAGS)0)) {
font = GetFontFromData(ref logFont);
font.SetSystemFontName("IconTitleFont");
}
return font;
}
}
[Nullable(1)]
public static Font DefaultFont {
[NullableContext(1)]
get {
Font font = null;
if (PInvokeCore.GetSystemDefaultLCID() == 1)
try {
font = new Font("Tahoma", 8);
} catch (Exception ex) when (!IsCriticalFontException(ex)) {
}
if (font == null) {
HFONT value = (HFONT)PInvokeCore.GetStockObject(GET_STOCK_OBJECT_FLAGS.DEFAULT_GUI_FONT);
try {
using (Font font2 = Font.FromHfont(value))
font = FontInPoints(font2);
} catch (ArgumentException) {
}
}
if (font == null)
try {
font = new Font("Tahoma", 8);
} catch (ArgumentException) {
}
if (font == null)
font = new Font(FontFamily.GenericSansSerif, 8);
if (font.Unit != GraphicsUnit.Point)
font = FontInPoints(font);
font.SetSystemFontName("DefaultFont");
return font;
}
}
[Nullable(1)]
public static Font DialogFont {
[NullableContext(1)]
get {
Font font = null;
if (PInvokeCore.GetSystemDefaultLCID() != 17)
try {
font = new Font("MS Shell Dlg 2", 8);
} catch (ArgumentException) {
}
else
font = DefaultFont;
if (font == null)
font = DefaultFont;
else if (font.Unit != GraphicsUnit.Point) {
font = FontInPoints(font);
}
font.SetSystemFontName("DialogFont");
return font;
}
}
[NullableContext(1)]
[return: Nullable(2)]
public static Font GetFontByName(string systemFontName)
{
if ("CaptionFont".Equals(systemFontName))
return CaptionFont;
if ("DefaultFont".Equals(systemFontName))
return DefaultFont;
if ("DialogFont".Equals(systemFontName))
return DialogFont;
if ("IconTitleFont".Equals(systemFontName))
return IconTitleFont;
if ("MenuFont".Equals(systemFontName))
return MenuFont;
if ("MessageBoxFont".Equals(systemFontName))
return MessageBoxFont;
if ("SmallCaptionFont".Equals(systemFontName))
return SmallCaptionFont;
if ("StatusFont".Equals(systemFontName))
return StatusFont;
return null;
}
private unsafe static bool GetNonClientMetrics(out NONCLIENTMETRICSW metrics)
{
metrics = new NONCLIENTMETRICSW {
cbSize = sizeof(NONCLIENTMETRICSW)
};
return PInvokeCore.SystemParametersInfo(ref metrics);
}
[NullableContext(1)]
private static bool IsCriticalFontException(Exception ex)
{
bool flag = (ex is ExternalException || ex is ArgumentException || ex is OutOfMemoryException || ex is InvalidOperationException || ex is NotImplementedException || ex is FileNotFoundException) ? true : false;
return !flag;
}
[NullableContext(1)]
private static Font FontInPoints(Font font)
{
return new Font(font.FontFamily, font.SizeInPoints, font.Style, GraphicsUnit.Point, font.GdiCharSet, font.GdiVerticalFont);
}
[NullableContext(1)]
private static Font GetFontFromData([In] [IsReadOnly] ref LOGFONTW logFont)
{
return GetFontFromData(ref Unsafe.As<LOGFONTW, LOGFONT>(ref Unsafe.AsRef(ref logFont)));
}
[NullableContext(1)]
private static Font GetFontFromData([In] [IsReadOnly] ref LOGFONT logFont)
{
Font font = null;
try {
font = Font.FromLogFont(ref logFont);
} catch (Exception ex) when (!IsCriticalFontException(ex)) {
}
if (font != null) {
if (font.Unit == GraphicsUnit.Point)
return font;
return FontInPoints(font);
}
return DefaultFont;
}
}
}