Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I also tried to use <code>System.Windows.Media.Fonts.SystemTypefaces</code> but as you mentioned it only gives the font file name. After doing a lot of reading and research on the web, I decided to use P/Invoke to solve this. If you have the choice of running your application as OOB (Out of Browser) with elevated trust as I had, the following P/Invoke solution will work great. Thanks to <a href="http://www.pinvoke.net" rel="nofollow">www.pinvoke.net</a> for all of the method/structure definitions.</p> <pre><code>using System; using System.Net; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Ink; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Shapes; using System.Runtime.InteropServices; using System.Collections.Generic; using System.Linq; using System.Security; namespace Utils { [SecurityCritical] public class FontsCollection { #region Types delegate int EnumFontDelegate(IntPtr lpelfe, IntPtr lpntme, EnumFontsType FontType, int lParam); enum EnumFontsType { DEVICE_FONTTYPE = 0x0000, RASTER_FONTTYPE = 0x0001, TRUETYPE_FONTTYPE = 0x0004 }; #region LOGFONT definition [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] public class LOGFONT { public int lfHeight; public int lfWidth; public int lfEscapement; public int lfOrientation; public FontWeight lfWeight; [MarshalAs(UnmanagedType.U1)] public bool lfItalic; [MarshalAs(UnmanagedType.U1)] public bool lfUnderline; [MarshalAs(UnmanagedType.U1)] public bool lfStrikeOut; public FontCharSet lfCharSet; public FontPrecision lfOutPrecision; public FontClipPrecision lfClipPrecision; public FontQuality lfQuality; public FontPitchAndFamily lfPitchAndFamily; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)] public string lfFaceName; } public enum FontWeight : int { FW_DONTCARE = 0, FW_THIN = 100, FW_EXTRALIGHT = 200, FW_LIGHT = 300, FW_NORMAL = 400, FW_MEDIUM = 500, FW_SEMIBOLD = 600, FW_BOLD = 700, FW_EXTRABOLD = 800, FW_HEAVY = 900, } public enum FontCharSet : byte { ANSI_CHARSET = 0, DEFAULT_CHARSET = 1, SYMBOL_CHARSET = 2, SHIFTJIS_CHARSET = 128, HANGEUL_CHARSET = 129, HANGUL_CHARSET = 129, GB2312_CHARSET = 134, CHINESEBIG5_CHARSET = 136, OEM_CHARSET = 255, JOHAB_CHARSET = 130, HEBREW_CHARSET = 177, ARABIC_CHARSET = 178, GREEK_CHARSET = 161, TURKISH_CHARSET = 162, VIETNAMESE_CHARSET = 163, THAI_CHARSET = 222, EASTEUROPE_CHARSET = 238, RUSSIAN_CHARSET = 204, MAC_CHARSET = 77, BALTIC_CHARSET = 186, } public enum FontPrecision : byte { OUT_DEFAULT_PRECIS = 0, OUT_STRING_PRECIS = 1, OUT_CHARACTER_PRECIS = 2, OUT_STROKE_PRECIS = 3, OUT_TT_PRECIS = 4, OUT_DEVICE_PRECIS = 5, OUT_RASTER_PRECIS = 6, OUT_TT_ONLY_PRECIS = 7, OUT_OUTLINE_PRECIS = 8, OUT_SCREEN_OUTLINE_PRECIS = 9, OUT_PS_ONLY_PRECIS = 10, } public enum FontClipPrecision : byte { CLIP_DEFAULT_PRECIS = 0, CLIP_CHARACTER_PRECIS = 1, CLIP_STROKE_PRECIS = 2, CLIP_MASK = 0xf, CLIP_LH_ANGLES = (1 &lt;&lt; 4), CLIP_TT_ALWAYS = (2 &lt;&lt; 4), CLIP_DFA_DISABLE = (4 &lt;&lt; 4), CLIP_EMBEDDED = (8 &lt;&lt; 4), } public enum FontQuality : byte { DEFAULT_QUALITY = 0, DRAFT_QUALITY = 1, PROOF_QUALITY = 2, NONANTIALIASED_QUALITY = 3, ANTIALIASED_QUALITY = 4, CLEARTYPE_QUALITY = 5, CLEARTYPE_NATURAL_QUALITY = 6, } [Flags] public enum FontPitchAndFamily : byte { DEFAULT_PITCH = 0, FIXED_PITCH = 1, VARIABLE_PITCH = 2, FF_DONTCARE = (0 &lt;&lt; 4), FF_ROMAN = (1 &lt;&lt; 4), FF_SWISS = (2 &lt;&lt; 4), FF_MODERN = (3 &lt;&lt; 4), FF_SCRIPT = (4 &lt;&lt; 4), FF_DECORATIVE = (5 &lt;&lt; 4), } #endregion #endregion #region Fields private IList&lt;string&gt; _fontNames; private IntPtr _fpEnumProc; private EnumFontDelegate _enumFontDelegate; private static FontsCollection _default; #endregion #region External APIs [DllImport("gdi32.dll")] private static extern int EnumFontFamilies(IntPtr hdc, string fontFamily, IntPtr lpEnumFontFamExProc, IntPtr lParam); [DllImport("user32.dll")] private static extern IntPtr GetDesktopWindow(); [DllImport("user32.dll")] private static extern IntPtr GetDC(IntPtr hwnd); [DllImport("user32.dll")] private static extern IntPtr ReleaseDC(IntPtr hdc); #endregion #region Properties public IEnumerable&lt;string&gt; InstalledFontNames { get { if (null == _fontNames) { BuildFontList(); } return _fontNames.AsEnumerable(); } } public static FontsCollection Default { get { return _default ?? (_default = new FontsCollection()); } } #endregion #region Win32 callback [System.Runtime.InteropServices.AllowReversePInvokeCalls] private int EnumFontFamiliesExProc(IntPtr lpelfe, IntPtr lpntme, EnumFontsType FontType, int lParam) { LOGFONT logFont = new LOGFONT(); Marshal.PtrToStructure(lpelfe, logFont); //we dont like duplicate names if (!_fontNames.Contains(logFont.lfFaceName, StringComparer.OrdinalIgnoreCase)) { _fontNames.Add(logFont.lfFaceName); } // Non-zero return continues enumerating return 1; } #endregion #region Methods private void BuildFontList() { // Need an HDC to pass to EnumFontFamilies IntPtr hwnd = GetDesktopWindow(); IntPtr hdc = GetDC(hwnd); try { LOGFONT logFont = new LOGFONT(); _enumFontDelegate = new EnumFontDelegate(EnumFontFamiliesExProc); _fpEnumProc = Marshal.GetFunctionPointerForDelegate(_enumFontDelegate); _fontNames = new List&lt;String&gt;(); EnumFontFamilies(hdc, null, _fpEnumProc, IntPtr.Zero); _fontNames = _fontNames.OrderBy(font =&gt; font).ToList(); } finally { if (hdc != IntPtr.Zero) { ReleaseDC(hdc); } } } #endregion } } </code></pre> <p>Using the class is very simple. Below is a sample XAML markup</p> <pre><code>&lt;UserControl x:Class="SLTest.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"&gt; &lt;Grid x:Name="LayoutRoot" Background="White"&gt; &lt;ComboBox ItemsSource="{Binding InstalledFontNames}" Height="50"/&gt; &lt;/Grid&gt; &lt;/UserControl&gt; </code></pre> <p>In the code behind of the XAML page, add the following</p> <pre><code>public partial class MainPage : UserControl { public MainPage() { InitializeComponent(); DataContext = Utils.FontsCollection.Default; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload