/// <summary> /// Gets a collection of fonts based on the specified criteria. /// </summary> /// <param name="families">Bit Field representing which font families to retrieve.</param> /// <param name="pitch">Specify the desired pitch.</param> /// <param name="charset">Specify the desired character set.</param> /// <param name="weight">Specify the desired weight.</param> /// <param name="Script">Specify the desired script(s) (this can be a String or an array of Strings).</param> /// <param name="Style">Specify the desired style(s) (this can be a String or an array of Strings).</param> /// <returns></returns> public static FontCollection GetFonts(FontFamilies families = FontFamilies.DontCare, FontPitch pitch = FontPitch.Default, FontCharSet charset = FontCharSet.Default, FontWeight weight = FontWeight.DontCare, object Script = null, object Style = null) { IntPtr hdc; var fonts = new List <ENUMLOGFONTEX>(); var lf = new LOGFONT(); string s; MemPtr mm = new MemPtr(); string[] wscript; string[] wstyle; if (Script is null) { wscript = new[] { "Western" }; } else if (Script is string) { wscript = new[] { (string)(Script) }; } else if (Script is string[]) { wscript = (string[])Script; } else { throw new ArgumentException("Invalid parameter type for Script"); } if (Style is null) { wstyle = new[] { "", "Normal", "Regular" }; } else if (Style is string) { wstyle = new[] { (string)(Style) }; } else if (Style is string[]) { wstyle = (string[])Style; } else { throw new ArgumentException("Invalid parameter type for Style"); } lf.lfCharSet = (byte)charset; lf.lfFaceName = ""; mm.Alloc(Marshal.SizeOf(lf)); mm.FromStruct(lf); hdc = User32.CreateDC("DISPLAY", null, IntPtr.Zero, IntPtr.Zero); int e; bool bo = false; e = EnumFontFamiliesEx(hdc, mm, (ref ENUMLOGFONTEX lpelfe, IntPtr lpntme, uint FontType, IntPtr lParam) => { int z; if (fonts is null) { z = 0; } else { z = fonts.Count; } // make sure it's the normal, regular version bo = false; foreach (var y in wstyle) { if ((y.ToLower() ?? "") == (lpelfe.elfStyle.ToLower() ?? "")) { bo = true; break; } } if (bo == false) { return(1); } bo = false; foreach (var y in wscript) { if ((y.ToLower() ?? "") == (lpelfe.elfScript.ToLower() ?? "")) { bo = true; break; } } if (bo == false) { return(1); } bo = false; if (weight != FontWeight.DontCare && lpelfe.elfLogFont.lfWeight != (int)weight) { return(1); } // we don't really need two of the same font. if (z > 0) { if ((lpelfe.elfFullName ?? "") == (fonts[z - 1].elfFullName ?? "")) { return(1); } } // the @ indicates a vertical writing font which we definitely do not want. if (lpelfe.elfFullName.Substring(0, 1) == "@") { return(1); } if (!CheckFamily(lpelfe.elfLogFont, families)) { return(1); } // lpelfe.elfLogFont.lfCharSet = charset // If (lpelfe.elfLogFont.lfCharSet <> charset) Then Return 1 if (pitch != FontPitch.Default && (lpelfe.elfLogFont.lfPitchAndFamily & 3) != (int)pitch) { return(1); } fonts.Add(lpelfe); return(1); }, IntPtr.Zero, 0U); User32.DeleteDC(hdc); mm.Free(); if (e == 0) { e = User32.GetLastError(); s = NativeError.Message; } FontInfo nf; var ccol = new FontCollection(); foreach (var f in fonts) { nf = new FontInfo(f); ccol.Add(nf); } ccol.Sort(); return(ccol); }