Font 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
定义文本的特定格式,包括字体、字号和样式属性。 此类不能被继承。
public ref class Font sealed : MarshalByRefObject, ICloneable, IDisposable, System::Runtime::Serialization::ISerializable
[System.ComponentModel.TypeConverter(typeof(System.Drawing.FontConverter))]
[System.Runtime.InteropServices.ComVisible(true)]
[System.Serializable]
public sealed class Font : MarshalByRefObject, ICloneable, IDisposable, System.Runtime.Serialization.ISerializable
[<System.ComponentModel.TypeConverter(typeof(System.Drawing.FontConverter))>]
[<System.Runtime.InteropServices.ComVisible(true)>]
[<System.Serializable>]
type Font = class
inherit MarshalByRefObject
interface ICloneable
interface ISerializable
interface IDisposable
Public NotInheritable Class Font
Inherits MarshalByRefObject
Implements ICloneable, IDisposable, ISerializable
- 继承
- 属性
- 实现
示例
下面的代码示例演示如何使用 Font 构造函数和 Size属性 SizeInPoints和 Unit 属性。 此示例旨在与 Windows 窗体一起使用,其中包含一个ComboBox用字符串“Bigger”和“Smaller”和一个ComboBox1命名填充的命名LabelLabel1。 将以下代码粘贴到窗体中,并将 ComboBox1_SelectedIndexChanged 该方法与 SelectedIndexChanged 控件的事件 ComboBox 相关联。
private:
void ComboBox1_SelectedIndexChanged(System::Object^ sender,
System::EventArgs^ e)
{
// Cast the sender object back to a ComboBox.
ComboBox^ ComboBox1 = (ComboBox^) sender;
// Retrieve the selected item.
String^ selectedString = (String^) ComboBox1->SelectedItem;
// Convert it to lowercase.
selectedString = selectedString->ToLower();
// Declare the current size.
float currentSize;
// If Bigger is selected, get the current size from the
// Size property and increase it. Reset the font to the
// new size, using the current unit.
if (selectedString == "bigger")
{
currentSize = Label1->Font->Size;
currentSize += 2.0F;
Label1->Font =gcnew System::Drawing::Font(Label1->Font->Name,
currentSize, Label1->Font->Style, Label1->Font->Unit);
}
// If Smaller is selected, get the current size, in
// points, and decrease it by 2. Reset the font with
// the new size in points.
if (selectedString == "smaller")
{
currentSize = Label1->Font->Size;
currentSize -= 2.0F;
Label1->Font = gcnew System::Drawing::Font(Label1->Font->Name,
currentSize, Label1->Font->Style);
}
}
private void ComboBox1_SelectedIndexChanged(System.Object sender,
System.EventArgs e)
{
// Cast the sender object back to a ComboBox.
ComboBox ComboBox1 = (ComboBox) sender;
// Retrieve the selected item.
string selectedString = (string) ComboBox1.SelectedItem;
// Convert it to lowercase.
selectedString = selectedString.ToLower();
// Declare the current size.
float currentSize;
// Switch on the selected item.
switch(selectedString)
{
// If Bigger is selected, get the current size from the
// Size property and increase it. Reset the font to the
// new size, using the current unit.
case "bigger":
currentSize = Label1.Font.Size;
currentSize += 2.0F;
Label1.Font = new Font(Label1.Font.Name, currentSize,
Label1.Font.Style, Label1.Font.Unit);
// If Smaller is selected, get the current size, in points,
// and decrease it by 1. Reset the font with the new size
// in points.
break;
case "smaller":
currentSize = Label1.Font.SizeInPoints;
currentSize -= 1;
Label1.Font = new Font(Label1.Font.Name, currentSize,
Label1.Font.Style);
break;
}
}
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
' Cast the sender object back to a ComboBox.
Dim ComboBox1 As ComboBox = CType(sender, ComboBox)
' Retrieve the selected item.
Dim selectedString As String = CType(ComboBox1.SelectedItem, String)
' Convert it to lowercase.
selectedString = selectedString.ToLower()
' Declare the current size.
Dim currentSize As Single
' Switch on the selected item.
Select Case selectedString
' If Bigger is selected, get the current size from the
' Size property and increase it. Reset the font to the
' new size, using the current unit.
Case "bigger"
currentSize = Label1.Font.Size
currentSize += 2.0F
Label1.Font = New Font(Label1.Font.Name, currentSize, _
Label1.Font.Style, Label1.Font.Unit)
' If Smaller is selected, get the current size, in points,
' and decrease it by 1. Reset the font with the new size
' in points.
Case "smaller"
currentSize = Label1.Font.SizeInPoints
currentSize -= 1
Label1.Font = New Font(Label1.Font.Name, currentSize, _
Label1.Font.Style)
End Select
End Sub
注解
有关如何构造字体的详细信息,请参阅 如何:构造字体系列和字体。 Windows 窗体应用程序支持 TrueType 字体,并且对 OpenType 字体的支持有限。 如果尝试使用不支持的字体,或者运行应用程序的计算机上未安装该字体,则会替换 Microsoft Sans Serif 字体。
Note
在 .NET 6 及更高版本中,包含此类型的 System.Drawing.Common 包 仅支持在 Windows 操作系统上运行。 在跨平台应用中使用此类型会导致编译时警告和运行时异常。 有关详细信息,请参阅 仅在 Windows 上支持的 System.Drawing.Common。
构造函数
| 名称 | 说明 |
|---|---|
| Font(Font, FontStyle) | |
| Font(FontFamily, Single, FontStyle, GraphicsUnit, Byte, Boolean) |
使用指定的大小、样式、单元和字符集初始化新 Font 内容。 |
| Font(FontFamily, Single, FontStyle, GraphicsUnit, Byte) |
使用指定的大小、样式、单元和字符集初始化新 Font 内容。 |
| Font(FontFamily, Single, FontStyle, GraphicsUnit) |
使用指定的大小、样式和单元初始化新 Font 内容。 |
| Font(FontFamily, Single, FontStyle) |
使用指定的大小和样式初始化新 Font 对象。 |
| Font(FontFamily, Single, GraphicsUnit) | |
| Font(FontFamily, Single) |
使用指定大小初始化新 Font 项。 |
| Font(String, Single, FontStyle, GraphicsUnit, Byte, Boolean) |
使用指定的大小、样式、单元和字符集初始化新 Font 内容。 |
| Font(String, Single, FontStyle, GraphicsUnit, Byte) |
使用指定的大小、样式、单元和字符集初始化新 Font 内容。 |
| Font(String, Single, FontStyle, GraphicsUnit) |
使用指定的大小、样式和单元初始化新 Font 内容。 |
| Font(String, Single, FontStyle) |
使用指定的大小和样式初始化新 Font 对象。 |
| Font(String, Single, GraphicsUnit) | |
| Font(String, Single) |
使用指定大小初始化新 Font 项。 |
属性
| 名称 | 说明 |
|---|---|
| Bold |
获取一个值,该值指示这 Font 是否为粗体。 |
| FontFamily |
获取与此FontFamily关联的值Font。 |
| GdiCharSet |
获取一个字节值,该值指定使用此 Font 字符集的 GDI 字符集。 |
| GdiVerticalFont |
获取一个布尔值,该值指示是否 Font 派生自 GDI 垂直字体。 |
| Height |
获取此字体的行距。 |
| IsSystemFont |
获取一个值,该值指示字体是否为 SystemFonts. |
| Italic |
获取一个值,该值指示此字体是否应用斜体样式。 |
| Name |
获取此 Font对象的人脸名称。 |
| OriginalFontName |
获取最初指定的字体的名称。 |
| Size | |
| SizeInPoints |
获取此 Font值的 em 大小(以磅为单位)。 |
| Strikeout |
获取一个值,该值指示该值是否 Font 通过字体指定水平线。 |
| Style |
获取此 Font对象的样式信息。 |
| SystemFontName |
如果属性返回IsSystemFont, |
| Underline |
获取一个值,该值指示是否 Font 为下划线。 |
| Unit |
获取此 Font度量单位。 |
方法
显式接口实现
| 名称 | 说明 |
|---|---|
| ISerializable.GetObjectData(SerializationInfo, StreamingContext) |
使用序列化目标对象所需的数据填充数据 SerializationInfo 。 |