Font 对象、Bold、Italic、Size、StrikeThrough、Underline 和 Weight 属性示例

以下示例演示了 Font 对象以及与字体相关的 Bold、Italic、Size、StrikeThrough、Underline、Weight 属性。 可以直接管理对象的字体属性,也可以通过使用别名来管理,如本示例所示。

若要使用此示例,请将此示例代码复制到窗体的 Declarations 部分。 确保该窗体包含:

  • 一个名为"Label1"的 Label
  • 四个名称分别为"ToggleButton1"到"ToggleButton4"的 ToggleButton 控件。
  • 名为"Label2"的第二个 Label 和名为"TextBox1"的 TextBox
Dim MyFont As StdFont 
 
Private Sub ToggleButton1_Click() 
 If ToggleButton1.Value = True Then 
 MyFont.Bold = True 
 'Using MyFont alias to control font 
 ToggleButton1.Caption = "Bold On" 
 MyFont.Size = 22 
 'Increase the font size 
 Else 
 MyFont.Bold = False 
 ToggleButton1.Caption = "Bold Off" 
 MyFont.Size = 8 
 'Return font size to initial size 
 End If 
 
 TextBox1.Text = Str(MyFont.Weight) 
 'Bold and Weight are related 
End Sub 
 
Private Sub ToggleButton2_Click() 
 If ToggleButton2.Value = True Then 
 Label1.Font.Italic = True 
 'Using Label1.Font directly 
 ToggleButton2.Caption = "Italic On" 
 Else 
 Label1.Font.Italic = False 
 ToggleButton2.Caption = "Italic Off" 
 End If 
End Sub 
 
Private Sub ToggleButton3_Click() 
 If ToggleButton3.Value = True Then 
 Label1.Font.Strikethrough = True 
 'Using Label1.Font directly 
 ToggleButton3.Caption = "StrikeThrough On" 
 Else 
 Label1.Font.Strikethrough = False 
 ToggleButton3.Caption = "StrikeThrough Off" 
 End If 
End Sub 
 
Private Sub ToggleButton4_Click() 
 If ToggleButton4.Value = True Then 
 MyFont.Underline = True 
 'Using MyFont alias for Label1.Font 
 ToggleButton4.Caption = "Underline On" 
 Else 
 Label1.Font.Underline = False 
 ToggleButton4.Caption = "Underline Off" 
 End If 
End Sub 
 
Private Sub UserForm_Initialize() 
 Set MyFont = Label1.Font 
 
 ToggleButton1.Value = True 
 ToggleButton1.Caption = "Bold On" 
 
 Label1.AutoSize = True 'Set size of Label1 
 Label1.AutoSize = False 
 
 ToggleButton2.Value = False 
 ToggleButton2.Caption = "Italic Off" 
 
 ToggleButton3.Value = False 
 ToggleButton3.Caption = "StrikeThrough Off" 
 
 ToggleButton4.Value = False 
 ToggleButton4.Caption = "Underline Off" 
 
 Label2.Caption = "Font Weight" 
 TextBox1.Text = Str(Label1.Font.Weight) 
 TextBox1.Enabled = False 
End Sub

支持和反馈

有关于 Office VBA 或本文档的疑问或反馈? 请参阅 Office VBA 支持和反馈,获取有关如何接收支持和提供反馈的指南。