3

我可以改进我的功能以不按每个元素搜索?

#Region " Font Is Installed? Function "

    ' [ Font Is Installed? Function ]
    '
    ' Examples :
    ' MsgBox(Font_Is_Installed("Lucida Console"))

    Private Function Font_Is_Installed(ByVal FontName As String) As Boolean
        Dim AllFonts As New Drawing.Text.InstalledFontCollection
        For Each Font As FontFamily In AllFonts.Families
            If Font.Name.ToLower = FontName.ToLower Then Return True
        Next
        Return False
    End Function

#End Region

更新:

好的,现在我看到了“.tolist”函数,现在我的代码是这样的:

Private Function Font_Is_Installed(ByVal FontName As String) As Boolean
    Dim AllFonts As New Drawing.Text.InstalledFontCollection
    Dim FontFamily As New FontFamily(FontName)
    If AllFonts.Families.ToList().Contains(FontFamily) Then Return True Else Return False
End Function

我有同样的问题:第二种方法最好改进,还是我可以更好地改进它?

4

3 回答 3

4

这是

    Public Shared Function IsFontInstalled(ByVal FontName As String) As Boolean
        Using TestFont As Font = New Font(FontName, 10)
            Return CBool(String.Compare(FontName, TestFont.Name, StringComparison.InvariantCultureIgnoreCase) = 0)
        End Using
    End Function
于 2013-04-09T09:24:40.383 回答
3
    Dim SomeTextBox As TextBox = New TextBox()
    Dim SomeFontFamily As FontFamily = Nothing
    Dim SomeFontCollection As PrivateFontCollection = Nothing

    Try
        SomeFontFamily = New FontFamily("SomeFontFamilyName")
    Catch ex As Exception
        SomeFontCollection = New PrivateFontCollection()
        SomeFontCollection.AddFontFile("SomeFontFileName")
        SomeFontFamily = SomeFontCollection.Families(0)
    End Try

    SomeTextBox.Font = New Font(SomeFontFamily, 12)

这样 SomeFontFamily 只有在不能从本地字体创建的情况下才会从文件创建。SomeTextBox 将显示正确的字体。

于 2014-07-12T13:09:19.227 回答
1

这是一个例子。只需尝试分配一个字体名称,如果它产生错误,则捕获它并返回 false。

Private Function isFontInstalled(ByVal FontName As String) As Boolean
    Try
        Dim FontFamily As New FontFamily(FontName)
        FontFamily.Dispose()
        Return True
    Catch
        Return False
    End Try
End Function
于 2015-05-23T21:01:17.450 回答