Font 对象、Bold、Italic、Size、StrikeThrough、Underline 和 Weight 属性示例
下例中,演示 Font 对象及与字体相关的 Bold、Italic、Size、StrikeThrough、Underline 和 Weight 属性。该示例还演示如何直接或者用一个别名来操作对象的字体属性。
窗体包含:
- 名为 Label1 的标签。
- 名为 ToggleButton1 到 ToggleButton4 的四个切换按钮控件。
- 名为 Label2 的第二个标签和名为 TextBox1 的文本框。
示例
Dim MyFont
Sub UserForm_ToggleButton1_Click()
If UserForm_ToggleButton1.Value = True Then
MyFont.Bold = True
'使用 MyFont 别名来控制字体
UserForm_ToggleButton1.Caption = "Bold On"
MyFont.Size = 22
'增大字体的尺寸
Else
MyFont.Bold = False
UserForm_ToggleButton1.Caption = "Bold Off"
MyFont.Size = 8
'将字体尺寸恢复到初始尺寸
End If
UserForm_TextBox1.Text = CStr(MyFont.Weight)
'Bold 和 Weight 是相关的。
End Sub
Sub UserForm_ToggleButton2_Click()
If UserForm_ToggleButton2.Value = True Then
UserForm_Label1.Font.Italic = True
'直接使用 Label1.Font
UserForm_ToggleButton2.Caption = "Italic On"
Else
UserForm_Label1.Font.Italic = False
UserForm_ToggleButton2.Caption = "Italic Off"
End If
End Sub
Sub UserForm_ToggleButton3_Click()
If UserForm_ToggleButton3.Value = True Then
UserForm_Label1.Font.Strikethrough = True
'直接使用 Label1.Font
UserForm_ToggleButton3.Caption = "StrikeThrough On"
Else
UserForm_Label1.Font.Strikethrough = False
UserForm_ToggleButton3.Caption = "StrikeThrough Off"
End If
End Sub
Sub UserForm_ToggleButton4_Click()
If UserForm_ToggleButton4.Value = True Then
MyFont.Underline = True
'使用 Label1.Font 的别名 MyFont
UserForm_ToggleButton4.Caption = "Underline On"
Else
UserForm_Label1.Font.Underline = False
UserForm_ToggleButton4.Caption = "Underline Off"
End If
End Sub
Sub UserForm_Initialize()
Set MyFont = UserForm_Label1.Font
UserForm_ToggleButton1.Value = True
UserForm_ToggleButton1.Caption = "Bold On"
UserForm_Label1.AutoSize = True '设置 Label1 的尺寸
UserForm_Label1.AutoSize = False
UserForm_ToggleButton2.Value = False
UserForm_ToggleButton2.Caption = "Italic Off"
UserForm_ToggleButton3.Value = False
UserForm_ToggleButton3.Caption = "StrikeThrough Off"
UserForm_ToggleButton4.Value = False
UserForm_ToggleButton4.Caption = "Underline Off"
UserForm_Label2.Caption = "Font Weight"
UserForm_TextBox1.Text = CStr(UserForm_Label1.Font.Weight)
UserForm_TextBox1.Enabled = False
End Sub
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76