Dot-Net
VB.NET:如何在執行時編寫字型並將其應用於標籤?
我正在使用 Visual Studio 2008 在 Visual Basic .NET 中開發 Windows 窗體應用程序。
我正在嘗試根據使用者偏好在執行時編寫字型(系列名稱、字型大小和样式),並將它們應用於標籤。
為了更簡單的使用者界面,以及需要使用相同字型的多台機器之間的兼容性,我不會使用InstalledFontCollection,而是使用一組按鈕來設置一些我知道存在的選定字型在所有機器中(像 Verdana 這樣的字型)。
因此,我必須在將創建字型的模組上創建一個 Public Sub,但我不知道如何編寫程式碼。還有四個 CheckBox 用於設置樣式,Bold、Italic、Underline 和 Strikeout。
我應該如何編碼?SomeLabel.Font.Bold屬性是只讀的,將“Times New Roman”之類的字元串轉換為 FontFamily 類型時似乎存在問題。(它只是說它做不到)
喜歡
Dim NewFontFamily As FontFamily = "Times New Roman"提前致謝。
這應該可以解決您的字型問題:
Label1.Font = New Drawing.Font("Times New Roman", _ 16, _ FontStyle.Bold or FontStyle.Italic)創建此字型的函式的可能實現可能如下所示:
Public Function CreateFont(ByVal fontName As String, _ ByVal fontSize As Integer, _ ByVal isBold As Boolean, _ ByVal isItalic As Boolean, _ ByVal isStrikeout As Boolean) As Drawing.Font Dim styles As FontStyle = FontStyle.Regular If (isBold) Then styles = styles Or FontStyle.Bold End If If (isItalic) Then styles = styles Or FontStyle.Italic End If If (isStrikeout) Then styles = styles Or FontStyle.Strikeout End If Dim newFont As New Drawing.Font(fontName, fontSize, styles) Return newFont End Function字型是不可變的,這意味著一旦創建它們就無法更新。因此,您注意到的所有隻讀屬性。