问题
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