How to populate combobox with a list of available fonts in windows 8 metro / store app using c #?

I am creating a simple application in Visual Studio 2013 for Windows 8 (universal application), I want to change the font family of the text field using the selected combobox font.

I know how to populate combobox with available fonts in a Windows Form application, for example, for example:

    List<string> fonts = new List<string>();

    foreach (FontFamily font in System.Drawing.FontFamily.Families)
    {
        fonts.Add(font.Name);
    }

but this does not work in the metro / store app ... please help me

+1
source share
1 answer

You will need to use DirectX DirectWrite to get the font names. Here is a sample code:

 using SharpDX.DirectWrite;
 using System.Collections.Generic;
 using System.Linq;

 namespace WebberCross.Helpers
 {
     public class FontHelper
     {
         public static IEnumerable<string> GetFontNames()
         {
             var fonts = new List<string>();

             // DirectWrite factory
             var factory = new Factory();

             // Get font collections
             var fc = factory.GetSystemFontCollection(false);

             for (int i = 0; i < fc.FontFamilyCount; i++)
             {
                 // Get font family and add first name
                 var ff = fc.GetFontFamily(i);

                 var name = ff.FamilyNames.GetString(0);
                 fonts.Add(name);
             }

             // Always dispose DirectX objects
             factory.Dispose();

             return fonts.OrderBy(f => f);
         }
     }
 }

The code uses the SharpDX library.

+1
source

Source: https://habr.com/ru/post/1614804/


All Articles