VB.Net 如何取得 字型 所對應的 檔案名稱

VB.Net 如何取得 字型 所對應的 檔案名稱

VB.Net 如何取得 字型 所對應的 檔案名稱

由 登錄檔 取得 字型 所對應的 檔案名稱

字型所對應之檔案

Imports System.IO

Imports Microsoft.Win32

Imports System.Environment

Public Class Form1

Private Sub Button1_Click(ByVal s As Object, ByVal e As EventArgs) Handles Button1.Click

Dim m As String = " -> 字型檔案: "

Dim c As New String(ControlChars.CrLf, 2)

Dim f As String() = New String() {"Arial", "細明體& 新細明體", "標楷體", "華康人物篇"}

MessageBox.Show(f(0) & m & GetFontFile(f(0)) & c & f(1) & m & GetFontFile(f(1)) & c & _

f(2) & m & GetFontFile(f(2)) & c & f(3) & m & GetFontFile(f(3)), _

"顯示字型所對應的檔案")

End Sub

Private Function GetFontFile(ByVal FontName As String) As String

Dim key As String = "SOFTWARE\Microsoft\Windows" & _

IIf(GetEnvironmentVariable("OS") = "", "", " NT") & "\CurrentVersion\Fonts"

Dim RegKey As RegistryKey = Registry.LocalMachine.OpenSubKey(key)

GetFontFile = RegKey.GetValue(FontName)

If GetFontFile = "" Then GetFontFile = RegKey.GetValue(FontName & " (TrueType)")

If GetFontFile = "" Then GetFontFile = RegKey.GetValue(FontName & " (All res)")

RegKey.Close()

If GetFontFile <> "" And Path.GetDirectoryName(GetFontFile) = "" Then

Return GetEnvironmentVariable("Windir") & "\Fonts\" & GetFontFile

End If

End Function

End Class