【问题标题】:More easy way to show if a Font is installed or not显示是否安装字体的更简单方法
【发布时间】:2016-10-07 23:14:26
【问题描述】:

我正在使用此代码并且工作正常

Imports System.Drawing.Text

Dim fontInstalled As Boolean = False
Dim fontToSearch As String
Dim fonts As New InstalledFontCollection

fontToSearch = "Verdana"

For Each one As FontFamily In fonts.Families
        'If I want to show every installed family name
        'MsgBox(one.Name)
        If one.Name = fontToSearch Then
            fontInstalled = True
            MsgBox("Font " & fontToSearch & " IS installed!!!")
            Exit For
        End If
    Next

If fontInstalled = False Then MsgBox("Font " & fontToSearch & " is NOT installed")

但我确信使用 InstalledFontCollection 或其他东西会有一个更清洁的解决方案,但我无法将此代码改编为 VB.NET Test if a Font is installed

var fontsCollection = new InstalledFontCollection();
foreach (var fontFamiliy in fontsCollection.Families)
{
if (fontFamiliy.Name == fontName) ... \\ installed
}

【问题讨论】:

    标签: vb.net fonts


    【解决方案1】:

    每当您使用关键字 New 时,请检查它是否包含 Dispose 方法。如果是这样,则意味着它可能分配了一些需要为Disposed 的资源。所以,在Using 块中使用它

    1.检查是否安装

    Dim IsInstalled As Boolean
    
    Using fnts As New InstalledFontCollection()
        IsInstalled = fnts.Families.
                    AsEnumerable().
                    Select(Function(s) s.Name).
                    Contains("Verdana")
    End Using
    

    2。试试看

    Using fnt As New Font("Verdana", 12)
        IsInstalled = (fnt IsNot Nothing)
    End Using
    

    3.获取已安装名称列表

    Dim fontNames As String()
    Using fnts As New InstalledFontCollection()
        fontNames = fnts.Families.
                    AsEnumerable().
                    Select(Function(s) s.Name).
                    ToArray()
    End Using
    

    奖金提示

    MsgBox / MessageBox 是一种可怕的调试方式。从菜单中选择 调试 |窗户 |输出。您可以将其停靠在您想要的任何位置并将其设置为“自动隐藏”,这样它就会隐藏,除非鼠标悬停在它上面。输出到它:

    For Each s As String In fontNames
        Console.WriteLine(s)
    Next
    

    等等不再点击处理 172 个字体名称对话框。

    【讨论】:

    • 工作得很好,谢谢。此外,您的答案是一个带有提示的完整教程。我仍然有滥用旧 Visual Basic 的恶习。再次感谢。
    【解决方案2】:

    这样的检查可能会有所帮助:

    imports system.drawing.text
    ...
    Function IsFontInstalled(FontName As String) As Boolean
        Dim fonts As New InstalledFontCollection
        For Each one As FontFamily In fonts.Families
            If one.Name = FontName Then
                Return True
            End If
        Next
        Return False
    End Function
    

    遇到了这种技术:

    Function IsFontInstalled(FontName As String) As Boolean
    
        Using fnt As New Font(FontName, 12)
            Return (fnt IsNot Nothing)
        End Using
    
    End Function
    

    如此之短,您可以直接使用该技术。

    【讨论】:

    • 工作出色且非常优雅的解决方案。谢谢。
    猜你喜欢
    • 1970-01-01
    • 2012-04-27
    • 1970-01-01
    • 1970-01-01
    • 2016-03-20
    • 2011-12-29
    • 2010-09-11
    • 1970-01-01
    • 2011-02-17
    相关资源
    最近更新 更多