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

人盡茶涼 提交于 2019-12-11 09:28:12

问题


I am building a simple app in Visual Studio 2013 for Windows 8 (a universal app), I want to change the font family of textbox using the selected font of combobox.

I know how to fill a combobox with available fonts in windows form app, like for example:

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

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

but this is not working in metro/store app... please help me out


回答1:


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

 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 SharpDX library.



来源:https://stackoverflow.com/questions/29588696/how-to-fill-combobox-with-list-of-available-fonts-in-windows-8-metro-store-app-u

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!