字体处理(针对 Visual Basic 6.0 用户)

更新:2007 年 11 月

本主题对 Visual Basic 6.0 中的字体处理方法与 Visual Basic 2008 中的等效方法进行了比较。

概念差异

在 Visual Basic 6.0 中用两种不同的方法处理字体:一种是作为窗体和控件的字体属性,另一种是作为 stdFont 对象。

在 Visual Basic 2008 中,只有一个 Font 对象:System.Drawing.Font。窗体或控件的 Font 属性将 Font 对象作为参数。

设置字体属性

在 Visual Basic 6.0 中,可通过分配 stdFont 对象或直接在控件上设置属性,在运行时设置字体属性;这两种方法可以交替使用。

在 Visual Basic 2008 中,控件的 Font 属性在运行时为只读属性,您不能直接设置这些属性。每次都必须实例化一个新的 Font 对象,才能设置属性。

字体继承

在 Visual Basic 6.0 中,必须为每个控件或窗体分别设置字体属性;使用 stdFont 对象可简化这一过程但仍需要代码。

而在 Visual Basic 2008 中,除非为子对象显式设置字体属性,否则将从其父级自动继承字体属性。例如,如果在窗体上有两个标签控件并将窗体的字体属性更改为 Arial,则标签控件的字体也将更改为 Arial。如果随后将一个标签的字体更改为 Times Roman,那么对窗体字体的进一步更改将不会重写该标签的字体。

字体兼容性

Visual Basic 6.0 为了向后兼容而支持光栅字体;Visual Basic 2008 仅支持 TrueType 和 OpenType 字体。

枚举字体

在 Visual Basic 6.0 中,可以使用 Screen.Fonts 集合连同 Screen.FontCount 属性一起来枚举可用的屏幕字体。

在 Visual Basic 2008 中,不再提供 Screen 对象;若要枚举系统中的可用字体,应使用 System.Drawing.FontFamily 命名空间。

说明:

Visual Basic 6.0 可以枚举所有字体类型。Visual Basic 2008 仅支持 TrueType 和 OpenType 字体;不会枚举其他字体类型。此外,Visual Basic 6.0 可以枚举字体系列中的每个字符集版本(例如,Arial、Arial Baltic、Arial Greek);而 Visual Basic 2008 仅枚举字体系列。

字体的代码更改

下面的代码示例演示 Visual Basic 6.0 和 Visual Basic 2008 在编码方法上的不同之处。

设置字体属性的代码更改

下面的示例演示在运行时设置字体属性。在 Visual Basic 6.0 中,可以直接在控件上设置属性;在 Visual Basic 2008 中,必须创建一个新的 Font 对象并在每次需要设置属性时将其分配给控件。

' Visual Basic 6.0 
' Set font properties directly on the control.
Label1.FontBold = True
' Create a stdFont object.
Dim f As New stdFont
' Set the stdFont object to the Arial font.
f.Name = "Arial"
' Assign the stdFont to the control's font property.
Set Label1.Font = f
' You can still change properties at run time.
Label1.FontBold = True
Label1.FontItalic = True
' Visual Basic
' Create a new Font object  Name and Size are required.
Dim f As New System.Drawing.Font("Arial", 10)
' Assign the font to the control
Label1.Font = f
' To set additional properties, you must create a new Font object.
Label1.Font = New System.Drawing.Font(Label1.Font, FontStyle.Bold Or FontStyle.Italic)

枚举字体的代码更改

下面的示例演示使用计算机上安装的字体列表填充 ListBox 控件。

说明:

Visual Basic 6.0 可以枚举所有字体类型。Visual Basic 2008 仅支持 TrueType 和 OpenType 字体;不会枚举其他字体类型。此外,Visual Basic 6.0 可以枚举字体系列中的每个字符集版本(例如,Arial、Arial Baltic、Arial Greek);而 Visual Basic 2008 仅枚举字体系列。

' Visual Basic 6.0 
Dim i As Integer
For i = 0 To Screen.FontCount – 1
   List1.AddItem Screen.Fonts(i)
Next i
' Visual Basic 
Dim ff As FontFamily
For Each ff In System.Drawing.FontFamily.Families
  listBox1.Items.Add(ff.Name)
Next

升级说明

当 Visual Basic 6.0 应用程序升级至 Visual Basic 2008 时,将修改所有字体处理代码以使用新的 Font 对象。

Visual Basic 2008 中的字体继承可能导致应用程序的外观发生意外更改。应在转换后的应用程序中检查任何在窗体或容器级别显式设置字体的代码,如有必要,请更改不应继承该字体的任何子控件的字体。

在升级过程中,光栅字体转换为默认的 OpenType 字体 Microsoft Sans Serif。不保留格式设置,如粗体或斜体。有关更多信息,请参见 只支持 OpenType 和 TrueType 字体

如果应用程序包括枚举字体的代码,升级后的应用程序中将不枚举光栅字体,而将枚举字体系列而不是各个字符集版本。

请参见

参考

Font

FontFamily.Families

其他资源

适用于 Visual Basic 6.0 用户的 Windows 窗体控件