【问题标题】:Getting information about installed fonts获取有关已安装字体的信息
【发布时间】:2013-04-08 10:54:05
【问题描述】:

我想检索已安装字体的信息,我试过这样:

Private Function Get_Installed_Fonts() As Array
    Dim AllFonts As New Drawing.Text.InstalledFontCollection ' Get the installed fonts collection.
    Dim FontFamilies() As FontFamily = AllFonts.Families()   ' Get an array of the system's font familiies.
    Return FontFamilies ' Return the array
End Function

那我可以做...:

   For Each Font As FontFamily In Get_Installed_Fonts()
       MsgBox(Font.Name)
   Next

但我找不到这样做的方法:

    For Each Font As FontFamily In Get_Installed_Fonts()
        MsgBox(Font.IsSystemFont)
        MsgBox(Font.OriginalFontName)
        MsgBox(Font.SizeInPoints)
    Next

我在那里缺少什么?

这是我将获得的东西,我也需要一种方法来搜索是否安装了字体,例如:

If FontsArray.contains("FontName") Then...

【问题讨论】:

    标签: arrays vb.net visual-studio fonts font-family


    【解决方案1】:

    问题在于 .IsSystemFont、.OriginalFontName 和 .SizeInPoints 属性是 Font 类的成员,而不是 FontFamily。 FontFamily 用于创建 Font,此时您可以使用上述语言获取信息。

    所以,你可以这样做......

    For Each FontFam As FontFamily In Get_Installed_Fonts()
        Dim tFont as new Font(FontFam.Name, 8)
        MsgBox(tFont.IsSystemFont)
        MsgBox(tFont.OriginalFontName)
        MsgBox(tFont.SizeInPoints)
        'tFont = nothing
    Next
    

    【讨论】:

    • 不要在 .Net 中将变量设置为 Nothing like that。在 vb6 时代它曾经是必需的,但对于 .Net 的垃圾收集器来说,它对内存收集要么没有影响,要么负面影响。
    • 记下并注释掉。谢谢
    【解决方案2】:
    Private Function Get_Installed_Fonts() As FontFamily()
        Using AllFonts As New Drawing.Text.InstalledFontCollection 
            Return AllFonts.Families
        End Using
    End Function
    

    【讨论】:

    • 糟糕:我起初误读了这个问题。我想删除我的答案,但我想我会保留代码,因为它仍然比你现在正在做的事情有一些改进。
    • 非常感谢您对函数的修改
    猜你喜欢
    • 1970-01-01
    • 2021-03-01
    • 2021-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-28
    • 2023-03-24
    • 2012-01-29
    相关资源
    最近更新 更多