0

我想使用 c# 将可用字体列表加载到 winrt 应用程序的组合框中。这是 .Net 中的简单任务,但我在 winrt 中找不到相应的 API。我该怎么做?

4

2 回答 2

4

您需要使用 DirectWrite。这里有一个片段可以做到这一点:

https://github.com/christophwille/winrt-snippets/tree/master/EnumerateFonts

复制粘贴:

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;

using SharpDX.DirectWrite;

namespace EnumerateFonts
{
    public class InstalledFont
    {
        public string Name { get; set; }

        // Code taken straight from SharpDX\Samples\DirectWrite\FontEnumeration\Program.cs
        public static List<InstalledFont> GetFonts()
        {
            var fontList = new List<InstalledFont>();

            var factory = new Factory();
            var fontCollection = factory.GetSystemFontCollection(false);
            var familyCount = fontCollection.FontFamilyCount;

            for (int i = 0; i < familyCount; i++)
            {
                var fontFamily = fontCollection.GetFontFamily(i);
                var familyNames = fontFamily.FamilyNames;
                int index;

                if (!familyNames.FindLocaleName(CultureInfo.CurrentCulture.Name, out index))
                    familyNames.FindLocaleName("en-us", out index);

                string name = familyNames.GetString(index);
                fontList.Add(new InstalledFont()
                                 {
                                     Name = name,
                                 });
            }

            return fontList;
        }
    }
}
于 2013-01-18T18:10:39.797 回答
0

还有一种方法可以在没有 SharpDX 的情况下枚举字体。https://code.msdn.microsoft.com/FontExplorer-lets-you-f01d415e#content

于 2015-11-06T10:53:29.163 回答