如何获取所有已安装的固定宽度字体?

发布于 2024-07-06 22:51:42 字数 205 浏览 8 评论 0原文

我想知道是否有任何简单的方法可以获取 C# 中用户系统上安装的所有固定宽度(等宽)字体的列表?

我使用的是 .net 3.5,因此可以访问 WPF System.Windows.Media 命名空间和 LINQ 来获取字体信息,但我不确定我在寻找什么。

我希望能够提供等宽字体的过滤列表和/或从更大的字体列表中挑选出等宽字体(如 VS 选项对话框中所示)。

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#?

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.

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).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

缘字诀 2024-07-13 22:51:42

看一下:

http://www.pinvoke.net/default.aspx/ Structures/LOGFONT.html

使用其中的结构之一,然后循环遍历系列,实例化 Font,获取 LogFont 值并检查 lfPitchAndFamily。

以下代码是即时编写的且未经测试,但类似以下内容应该可以工作:

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......
        }
    }
}

Have a look at:

http://www.pinvoke.net/default.aspx/Structures/LOGFONT.html

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......
        }
    }
}
ヤ经典坏疍 2024-07-13 22:51:42

不幸的是,ToLogFont函数无法将lfPitchAndFamily字段填充为正确的值。 在我的例子中,它始终为 0。

检测哪些字体可能被修复的一种近似值如下所示。

    foreach ( FontFamily ff in FontFamily.Families ) {
            if ( ff.IsStyleAvailable( FontStyle.Regular ) ) {
                float diff;
                using ( Font font = new Font( ff, 16 ) ) {
                    diff = TextRenderer.MeasureText( "WWW", font ).Width - TextRenderer.MeasureText( "...", font ).Width;
                }
                if ( Math.Abs( diff ) < float.Epsilon * 2 ) {
                    Debug.WriteLine( ff.ToString() );
                }
            }

        }

请记住,它们是几个误报,例如 Wingdings

Unfortunately ToLogFont function does not fill lfPitchAndFamily field to correct values. In my case it's always 0.

One approximation to detect which fonts might be fixed is the following

    foreach ( FontFamily ff in FontFamily.Families ) {
            if ( ff.IsStyleAvailable( FontStyle.Regular ) ) {
                float diff;
                using ( Font font = new Font( ff, 16 ) ) {
                    diff = TextRenderer.MeasureText( "WWW", font ).Width - TextRenderer.MeasureText( "...", font ).Width;
                }
                if ( Math.Abs( diff ) < float.Epsilon * 2 ) {
                    Debug.WriteLine( ff.ToString() );
                }
            }

        }

Keep in mind that they are several false positives, for example Wingdings

我乃一代侩神 2024-07-13 22:51:42

AFAIK 仅使用 BCL 库无法做到这一点。 您必须使用 WinAPI 互操作。

您需要分析 LOGFONT.lfPitchAndFamily 成员。 有一个常量 FIXED_PITCH (意味着字体是固定宽度的),可以用作 lfPitchAndFamily 的位掩码。

这是一篇有用的文章:

枚举字体

枚举字体可能有点
令人困惑,除非你想
枚举系统上的所有字体,
可能比
MSDN 建议。 本文将
准确解释您需要的步骤
用于查找每个固定宽度字体
你的系统,并枚举每一个
每个人可能的尺寸
字体。

AFAIK you can't do it using BCL libraries only. You have to use WinAPI interop.

You need to analyze 2 lowest bits of LOGFONT.lfPitchAndFamily member. There is a constant FIXED_PITCH (means that font is fixed-width) that can be used as a bit mask for lfPitchAndFamily.

Here is a useful article:

Enumerating Fonts

Enumerating fonts can be a little
confusing, and unless you want to
enumerate all fonts on your system,
can be a little more difficult than
MSDN suggests. This article will
explain exactly the steps you need to
use to find every fixed-width font on
your system, and also enumerate every
possible size for each individual
font.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文