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

同じ質問があります: 2 番目の方法で改善するのが最善ですか、それとももっと改善できますか?

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 に答える