如何使用C#在Windows 8 Metro/Store应用程序中用可用字体列表填充组合框? [英] How to fill combobox with list of available fonts in windows 8 metro/store app using c#?
本文介绍了如何使用C#在Windows 8 Metro/Store应用程序中用可用字体列表填充组合框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在Windows 8的Visual Studio 2013中构建一个简单的应用程序(通用应用程序),我想使用选定的combobox字体更改文本框的字体系列.
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.
我知道如何用Windows窗体应用程序中的可用字体填充组合框,例如:
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);
}
但这在Metro/Store应用中不起作用...请帮帮我
but this is not working in metro/store app... please help me out
推荐答案
您将需要使用DirectX DirectWrite来获取字体名称.这是一个代码示例:
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);
}
}
}
代码使用 SharpDX 库.
这篇关于如何使用C#在Windows 8 Metro/Store应用程序中用可用字体列表填充组合框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文