Created
February 3, 2023 21:36
-
-
Save maestrow/7a4d84752e960cbcdb6003bd9e51714b to your computer and use it in GitHub Desktop.
List all monospaced fonts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Query Kind="Program"> | |
<Namespace>System.Drawing</Namespace> | |
<Namespace>System.Runtime.InteropServices</Namespace> | |
</Query> | |
// https://stackoverflow.com/questions/12417432/how-to-get-all-monospaced-fonts-in-windows | |
internal class NativeMethods | |
{ | |
public const Int32 LF_FACESIZE = 32; | |
public const Int32 LF_FULLFACESIZE = 64; | |
public const Int32 DEFAULT_CHARSET = 1; | |
public const Int32 FIXED_PITCH = 1; | |
public const Int32 TRUETYPE_FONTTYPE = 0x0004; | |
public delegate Int32 FONTENUMPROC(ref ENUMLOGFONT lpelf, ref NEWTEXTMETRIC lpntm, UInt32 FontType, IntPtr lParam); | |
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)] | |
public struct LOGFONT | |
{ | |
public Int32 lfHeight; | |
public Int32 lfWidth; | |
public Int32 lfEscapement; | |
public Int32 lfOrientation; | |
public Int32 lfWeight; | |
public Byte lfItalic; | |
public Byte lfUnderline; | |
public Byte lfStrikeOut; | |
public Byte lfCharSet; | |
public Byte lfOutPrecision; | |
public Byte lfClipPrecision; | |
public Byte lfQuality; | |
public Byte lfPitchAndFamily; | |
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = LF_FACESIZE)] | |
public String lfFaceName; | |
} | |
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)] | |
public struct TEXTMETRIC | |
{ | |
public Int32 tmHeight; | |
public Int32 tmAscent; | |
public Int32 tmDescent; | |
public Int32 tmInternalLeading; | |
public Int32 tmExternalLeading; | |
public Int32 tmAveCharWidth; | |
public Int32 tmMaxCharWidth; | |
public Int32 tmWeight; | |
public Int32 tmOverhang; | |
public Int32 tmDigitizedAspectX; | |
public Int32 tmDigitizedAspectY; | |
public Char tmFirstChar; | |
public Char tmLastChar; | |
public Char tmDefaultChar; | |
public Char tmBreakChar; | |
public Byte tmItalic; | |
public Byte tmUnderlined; | |
public Byte tmStruckOut; | |
public Byte tmPitchAndFamily; | |
public Byte tmCharSet; | |
} | |
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)] | |
public struct ENUMLOGFONT | |
{ | |
public LOGFONT elfLogFont; | |
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = LF_FULLFACESIZE)] | |
public String elfFullName; | |
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = LF_FACESIZE)] | |
public String elfStyle; | |
} | |
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)] | |
public struct NEWTEXTMETRIC | |
{ | |
public Int32 tmHeight; | |
public Int32 tmAscent; | |
public Int32 tmDescent; | |
public Int32 tmInternalLeading; | |
public Int32 tmExternalLeading; | |
public Int32 tmAveCharWidth; | |
public Int32 tmMaxCharWidth; | |
public Int32 tmWeight; | |
public Int32 tmOverhang; | |
public Int32 tmDigitizedAspectX; | |
public Int32 tmDigitizedAspectY; | |
public Char tmFirstChar; | |
public Char tmLastChar; | |
public Char tmDefaultChar; | |
public Char tmBreakChar; | |
public Byte tmItalic; | |
public Byte tmUnderlined; | |
public Byte tmStruckOut; | |
public Byte tmPitchAndFamily; | |
public Byte tmCharSet; | |
public UInt32 ntmFlags; | |
public UInt32 ntmSizeEM; | |
public UInt32 ntmCellHeight; | |
public UInt32 ntmAvgWidth; | |
} | |
[DllImport("gdi32.dll", CharSet = CharSet.Auto)] | |
public extern static Int32 EnumFontFamiliesEx(IntPtr hdc, ref LOGFONT lpLogfont, FONTENUMPROC lpEnumFontFamExProc, IntPtr lParam, UInt32 dwFlags); | |
} | |
internal static class Program | |
{ | |
private static void Main() | |
{ | |
Graphics graphics = Graphics.FromHwnd(IntPtr.Zero); | |
IntPtr hdc = graphics.GetHdc(); | |
var logfont = new NativeMethods.LOGFONT() { lfCharSet = NativeMethods.DEFAULT_CHARSET }; | |
NativeMethods.EnumFontFamiliesEx(hdc, ref logfont, new NativeMethods.FONTENUMPROC(EnumFontFamExProc), IntPtr.Zero, 0); | |
graphics.ReleaseHdc(); | |
} | |
private static int EnumFontFamExProc(ref NativeMethods.ENUMLOGFONT lpelf, ref NativeMethods.NEWTEXTMETRIC lpntm, uint FontType, IntPtr lParam) | |
{ | |
if ((lpelf.elfLogFont.lfPitchAndFamily & 0x3) == NativeMethods.FIXED_PITCH) | |
{ | |
Console.WriteLine(lpelf.elfLogFont); | |
} | |
return 1; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment