Get lists of all installed fonts in combo box uwp

Binamra Lamsal 21 Reputation points
2021-01-04T11:34:21.443+00:00

Please help me to get all installed fonts in UWP. I have seen so many articles to use System.Drawing.Text but I did not find any reference to it. So, please help me to get list all installed font families for combo box

Developer technologies | Universal Windows Platform (UWP)
0 comments No comments
{count} votes

Accepted answer
  1. Nico Zhu (Shanghai Wicresoft Co,.Ltd.) 12,866 Reputation points
    2021-01-04T13:12:37.373+00:00

    Hello @Binamra Lamsal Welcome to Microsoft Q&A,

    As @Ken Tucker said, you could use Win2D api to get list of install font. and render it in the listview.

    System.NullReferenceException: 'Object reference not set to an instance of an object.'

    comboFonts was null.

    The problem is you set the ItemsSource before this.InitializeComponent(); method that will cause the listview has not render yet.

    please call above method in the OnNavigatedTo method.

     protected override void OnNavigatedTo(NavigationEventArgs e)  
        {  
            base.OnNavigatedTo(e);  
    
            var fonts = Microsoft.Graphics.Canvas.Text.CanvasTextFormat.GetSystemFontFamilies();  
            comboFonts.ItemsSource = fonts;  
        }  
    

    Xaml Code

      <Grid>  
            <ListView x:Name="comboFonts"></ListView>  
        </Grid>  
    

    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

2 additional answers

Sort by: Most helpful
  1. Ken Tucker 5,861 Reputation points
    2021-01-04T12:04:03.177+00:00

    Add the Nuget package Win2D to your app then you can get a font list like this

             var fonts = Microsoft.Graphics.Canvas.Text.CanvasTextFormat.GetSystemFontFamilies();
             comboFonts.ItemsSource = fonts;
    
    2 people found this answer helpful.

  2. Castorix31 90,686 Reputation points
    2021-01-04T14:33:52.913+00:00

    You can use EnumFontFamiliesEx

    A test (with ComboBox1 ComboBox, you can change the added string sText ) :

    public int FontCallback(ref ENUMLOGFONTEX lpelfe, ref NEWTEXTMETRICEX lpntme, uint FontType, IntPtr lParam)  
    {  
        string sText = lpelfe.lfFaceName + " (" + lpelfe.elfScript + ", " + (lpelfe.elfStyle !=""?lpelfe.elfStyle:"No style") + ")";  
        ComboBox1.Items.Add(sText);          
        return 1;  
    }  
    

    :

    IntPtr hDC = GetDC(IntPtr.Zero);                                                                   
    LOGFONT lf = new LOGFONT();                                                     
    lf.lfCharSet = DEFAULT_CHARSET;                                                 
    EnumFontFamExProc enumFontFamExProc = new EnumFontFamExProc(FontCallback);    [DllImport("Gdi32.dll", CharSet = CharSet.Unicode)]  
    int nRet = EnumFontFamiliesEx(hDC, ref lf, enumFontFamExProc, IntPtr.Zero, 0);public static extern int EnumFontFamiliesEx(IntPtr hdc, ref LOGFONT lpLogfont, EnumFontFamExProc lpEnumFontFamExProc, IntPtr lParam, uint dwFlags);  
    ReleaseDC(IntPtr.Zero, hDC);  
    

    Declarations :

    [DllImport("Gdi32.dll", CharSet = CharSet.Unicode)]  
    public static extern int EnumFontFamiliesEx(IntPtr hdc, ref LOGFONT lpLogfont, EnumFontFamExProc lpEnumFontFamExProc, IntPtr lParam, uint dwFlags);  
                                                                         
    public delegate int EnumFontFamExProc(ref ENUMLOGFONTEX lpelfe, ref NEWTEXTMETRICEX lpntme, uint FontType, IntPtr lParam);  
      
    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]  
    public struct LOGFONT  
    {  
          public int lfHeight;  
          public int lfWidth;  
          public int lfEscapement;  
          public int lfOrientation;  
          public int 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 = 32)]  
          public string lfFaceName;  
      }  
      
    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]  
    public struct ENUMLOGFONTEX  
    {  
          // LOGFONT  
          public int lfHeight;  
          public int lfWidth;  
          public int lfEscapement;  
          public int lfOrientation;  
          public int 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 = 32)]  
          public string lfFaceName;  
          // ENUMLOGFONTEX   
          [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 64)]  
          public string elfFullName;  
          [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]  
          public string elfStyle;  
          [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]  
          public string elfScript;  
      }  
      
    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]  
    public struct NEWTEXTMETRIC  
    {  
        public int tmHeight;  
        public int tmAscent;  
        public int tmDescent;  
        public int tmInternalLeading;  
        public int tmExternalLeading;  
        public int tmAveCharWidth;  
        public int tmMaxCharWidth;  
        public int tmWeight;  
        public int tmOverhang;  
        public int tmDigitizedAspectX;  
        public int tmDigitizedAspectY;  
        public char tmFirschar;  
        public char tmLaschar;  
        public char tmDefaulchar;  
        public char tmBreakChar;  
        public byte tmItalic;  
        public byte tmUnderlined;  
        public byte tmStruckOut;  
        public byte tmPitchAndFamily;  
        public byte tmCharSet;  
        public uint ntmFlags;  
        public uint ntmSizeEM;  
        public uint ntmCellHeight;  
        public uint ntmAvgWidth;  
    }  
      
    [StructLayout(LayoutKind.Sequential)]  
    public struct FONTSIGNATURE  
    {  
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]  
        public uint[] fsUsb;  
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]  
        public uint[] fsCsb;  
    }  
      
    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]  
    public struct NEWTEXTMETRICEX  
    {  
        public NEWTEXTMETRIC ntmTm;  
        public FONTSIGNATURE ntmFontSig;  
    }  
      
    public const byte DEFAULT_CHARSET = 1;  
      
    [DllImport("User32", ExactSpelling = true, CharSet = CharSet.Unicode)]  
    private static extern IntPtr GetDC(IntPtr hWnd);  
      
    [DllImport("User32", ExactSpelling = true, CharSet = CharSet.Unicode)]  
    private static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);  
    
    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.