问题描述
我想知道是否有任何简单的方法可以在 C# 中获取用户系统上安装的所有固定宽度(等宽)字体的列表?
I'm wondering if there are any simple ways to get a list of all fixed-width (monospaced) fonts installed on a user's system in C#?
我使用的是 .net 3.5,因此可以访问 WPF System.Windows.Media 命名空间和 LINQ 以获取字体信息,但我不确定我在寻找什么.
I'm using .net 3.5 so have access to the WPF System.Windows.Media namespace and LINQ to get font information, but I'm not sure what I'm looking for.
我希望能够提供过滤的等宽字体列表和/或从更大的字体列表中挑选出等宽字体(如 VS 选项对话框中所示).
I want to be able to provide a filtered list of monospaced fonts and/or pick out monospaced fonts from a larger list of fonts (as seen in the VS options dialog).
推荐答案
看看:
http://www.pinvoke.net/default.aspx/Structures/LOGFONT.html
使用其中的一种结构,然后遍历系列,实例化一个 Font,并获取 LogFont 值并检查 lfPitchAndFamily.
Use one of the structures in there, then loop over families, instantiating a Font, and getting the LogFont value and checking lfPitchAndFamily.
以下代码是即时编写的,未经测试,但应该可以使用以下代码:
The following code is written on the fly and untested, but something like the following should work:
foreach (FontFamily ff in System.Drawing.FontFamily.Families)
{
if (ff.IsStyleAvailable(FontStyle.Regular))
{
Font font = new Font(ff, 10);
LOGFONT lf = new LOGFONT();
font.ToLogFont(lf);
if (lf.lfPitchAndFamily ^ 1)
{
do stuff here......
}
}
}
这篇关于如何获取所有已安装的固定宽度字体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!