Font Font Font Font Class

定义

定义特定的文本格式,包括字体、字号和样式特性。Defines a particular format for text, including font face, size, and style attributes. 此类不能被继承。This class cannot be inherited.

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]
[System.ComponentModel.TypeConverter("System.Drawing.FontConverter, System.Windows.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51")]
public sealed class Font : MarshalByRefObject, ICloneable, IDisposable, System.Runtime.Serialization.ISerializable
type Font = class
    inherit MarshalByRefObject
    interface ICloneable
    interface ISerializable
    interface IDisposable
Public NotInheritable Class Font
Inherits MarshalByRefObject
Implements ICloneable, IDisposable, ISerializable
继承
属性
实现

示例

下面的代码示例Font演示如何使用构造函数Size和、 SizeInPointsUnit属性。The following code example demonstrates how to use the Font constructor and the Size, SizeInPoints, and Unit properties. 此示例旨在与 Windows 窗体一起使用, 该窗体包含ComboBox一个ComboBox1名为的, 该窗体使用字符串 "放大" 和 "缩小Label " Label1以及一个名为的。This example is designed to be used with a Windows Form that contains a ComboBox named ComboBox1 that is populated with the strings "Bigger" and "Smaller" and a Label named Label1. 将以下代码粘贴到窗体中, 并ComboBox1_SelectedIndexChanged将该方法SelectedIndexChangedComboBox控件的事件相关联。Paste the following code into the form and associate the ComboBox1_SelectedIndexChanged method with the SelectedIndexChanged event of the ComboBox control.

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

注解

有关如何构造字体的详细信息, 请参阅如何:构造字体系列和字体For more information about how to construct fonts, see How to: Construct Font Families and Fonts. Windows 窗体应用程序支持 TrueType 字体, 并且对 OpenType 字体的支持有限。Windows Forms applications support TrueType fonts and have limited support for OpenType fonts. 如果尝试使用不支持的字体, 或者在运行该应用程序的计算机上未安装该字体, 则会替换 Microsoft Sans Serif 字体。If you attempt to use a font that is not supported, or the font is not installed on the machine that is running the application, the Microsoft Sans Serif font will be substituted.

构造函数

Font(Font, FontStyle) Font(Font, FontStyle) Font(Font, FontStyle) Font(Font, FontStyle)

初始化新 Font,它使用指定的现有 FontFontStyle 枚举。Initializes a new Font that uses the specified existing Font and FontStyle enumeration.

Font(FontFamily, Single) Font(FontFamily, Single) Font(FontFamily, Single) Font(FontFamily, Single)

使用指定的大小初始化新 FontInitializes a new Font using a specified size.

Font(FontFamily, Single, FontStyle) Font(FontFamily, Single, FontStyle) Font(FontFamily, Single, FontStyle) Font(FontFamily, Single, FontStyle)

使用指定的大小和样式初始化新 FontInitializes a new Font using a specified size and style.

Font(FontFamily, Single, FontStyle, GraphicsUnit) Font(FontFamily, Single, FontStyle, GraphicsUnit) Font(FontFamily, Single, FontStyle, GraphicsUnit) Font(FontFamily, Single, FontStyle, GraphicsUnit)

使用指定的大小、样式和单位初始化新的 FontInitializes a new Font using a specified size, style, and unit.

Font(FontFamily, Single, FontStyle, GraphicsUnit, Byte) Font(FontFamily, Single, FontStyle, GraphicsUnit, Byte) Font(FontFamily, Single, FontStyle, GraphicsUnit, Byte) Font(FontFamily, Single, FontStyle, GraphicsUnit, Byte)

使用指定的大小、样式、单位和字符集初始化新的 FontInitializes a new Font using a specified size, style, unit, and character set.

Font(FontFamily, Single, FontStyle, GraphicsUnit, Byte, Boolean) Font(FontFamily, Single, FontStyle, GraphicsUnit, Byte, Boolean) Font(FontFamily, Single, FontStyle, GraphicsUnit, Byte, Boolean) Font(FontFamily, Single, FontStyle, GraphicsUnit, Byte, Boolean)

使用指定的大小、样式、单位和字符集初始化新的 FontInitializes a new Font using a specified size, style, unit, and character set.

Font(FontFamily, Single, GraphicsUnit) Font(FontFamily, Single, GraphicsUnit) Font(FontFamily, Single, GraphicsUnit) Font(FontFamily, Single, GraphicsUnit)

使用指定的大小和单位初始化新的 FontInitializes a new Font using a specified size and unit. 将此样式设置为 RegularSets the style to Regular.

Font(String, Single) Font(String, Single) Font(String, Single) Font(String, Single)

使用指定的大小初始化新 FontInitializes a new Font using a specified size.

Font(String, Single, FontStyle) Font(String, Single, FontStyle) Font(String, Single, FontStyle) Font(String, Single, FontStyle)

使用指定的大小和样式初始化新 FontInitializes a new Font using a specified size and style.

Font(String, Single, FontStyle, GraphicsUnit) Font(String, Single, FontStyle, GraphicsUnit) Font(String, Single, FontStyle, GraphicsUnit) Font(String, Single, FontStyle, GraphicsUnit)

使用指定的大小、样式和单位初始化新的 FontInitializes a new Font using a specified size, style, and unit.

Font(String, Single, FontStyle, GraphicsUnit, Byte) Font(String, Single, FontStyle, GraphicsUnit, Byte) Font(String, Single, FontStyle, GraphicsUnit, Byte) Font(String, Single, FontStyle, GraphicsUnit, Byte)

使用指定的大小、样式、单位和字符集初始化新的 FontInitializes a new Font using a specified size, style, unit, and character set.

Font(String, Single, FontStyle, GraphicsUnit, Byte, Boolean) Font(String, Single, FontStyle, GraphicsUnit, Byte, Boolean) Font(String, Single, FontStyle, GraphicsUnit, Byte, Boolean) Font(String, Single, FontStyle, GraphicsUnit, Byte, Boolean)

使用指定的大小、样式、单位和字符集初始化新 FontInitializes a new Font using the specified size, style, unit, and character set.

Font(String, Single, GraphicsUnit) Font(String, Single, GraphicsUnit) Font(String, Single, GraphicsUnit) Font(String, Single, GraphicsUnit)

使用指定的大小和单位初始化新的 FontInitializes a new Font using a specified size and unit. 样式设置为 RegularThe style is set to Regular.

属性

Bold Bold Bold Bold

获取一个值,该值指示此 Font 是否为粗体。Gets a value that indicates whether this Font is bold.

FontFamily FontFamily FontFamily FontFamily

获取与此 FontFamily 关联的 FontGets the FontFamily associated with this Font.

GdiCharSet GdiCharSet GdiCharSet GdiCharSet

获取一个字节值,该值指定此 Font 使用的 GDI 字符集。Gets a byte value that specifies the GDI character set that this Font uses.

GdiVerticalFont GdiVerticalFont GdiVerticalFont GdiVerticalFont

获取一个布尔值,该值指示此 Font 是否从 GDI 垂直字体派生。Gets a Boolean value that indicates whether this Font is derived from a GDI vertical font.

Height Height Height Height

获取此字体的行距。Gets the line spacing of this font.

IsSystemFont IsSystemFont IsSystemFont IsSystemFont

获取一个值,该值表示此字体是否是 SystemFonts 的一个成员。Gets a value indicating whether the font is a member of SystemFonts.

Italic Italic Italic Italic

获取一个值,该值指示此字体是否已应用斜体样式。Gets a value that indicates whether this font has the italic style applied.

Name Name Name Name

获取此 Font 的字体名称。Gets the face name of this Font.

OriginalFontName OriginalFontName OriginalFontName OriginalFontName

获取最初指定的字体的名称。Gets the name of the font originally specified.

Size Size Size Size

获取此 Font 的全身大小,单位采用 Unit 属性指定的单位。Gets the em-size of this Font measured in the units specified by the Unit property.

SizeInPoints SizeInPoints SizeInPoints SizeInPoints

获取此 Font 的全身大小(以点为单位)。Gets the em-size, in points, of this Font.

Strikeout Strikeout Strikeout Strikeout

获取一个值,该值指示此 Font 是否指定贯穿字体的横线。Gets a value that indicates whether this Font specifies a horizontal line through the font.

Style Style Style Style

获取此 Font 的样式信息。Gets style information for this Font.

SystemFontName SystemFontName SystemFontName SystemFontName

如果 IsSystemFont 属性返回 true,则获取系统字体的名称。Gets the name of the system font if the IsSystemFont property returns true.

Underline Underline Underline Underline

获取一个值,该值指示此 Font 是否有下划线。Gets a value that indicates whether this Font is underlined.

Unit Unit Unit Unit

获取此 Font 的度量单位。Gets the unit of measure for this Font.

方法

Clone() Clone() Clone() Clone()

创建此 Font 的一个精确副本。Creates an exact copy of this Font.

CreateObjRef(Type) CreateObjRef(Type) CreateObjRef(Type) CreateObjRef(Type)

创建一个对象,该对象包含生成用于与远程对象进行通信的代理所需的全部相关信息。Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object.

(Inherited from MarshalByRefObject)
Dispose() Dispose() Dispose() Dispose()

释放此 Font 使用的所有资源。Releases all resources used by this Font.

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

指示指定对象是否为 Font,以及是否具有与此 FontFamily 相同的 GdiVerticalFontGdiCharSetStyleSizeUnitFont 属性值。Indicates whether the specified object is a Font and has the same FontFamily, GdiVerticalFont, GdiCharSet, Style, Size, and Unit property values as this Font.

Finalize() Finalize() Finalize() Finalize()

在垃圾回收将某一对象回收前允许该对象尝试释放资源并执行其他清理操作。Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.

FromHdc(IntPtr) FromHdc(IntPtr) FromHdc(IntPtr) FromHdc(IntPtr)

从设备上下文的指定 Windows 句柄创建 FontCreates a Font from the specified Windows handle to a device context.

FromHfont(IntPtr) FromHfont(IntPtr) FromHfont(IntPtr) FromHfont(IntPtr)

从指定的 Windows 句柄创建一个 FontCreates a Font from the specified Windows handle.

FromLogFont(Object) FromLogFont(Object) FromLogFont(Object) FromLogFont(Object)

从指定的 GDI 逻辑字体 (LOGFONT) 结构创建一个 FontCreates a Font from the specified GDI logical font (LOGFONT) structure.

FromLogFont(Object, IntPtr) FromLogFont(Object, IntPtr) FromLogFont(Object, IntPtr) FromLogFont(Object, IntPtr)

从指定的 GDI 逻辑字体 (LOGFONT) 结构创建一个 FontCreates a Font from the specified GDI logical font (LOGFONT) structure.

GetHashCode() GetHashCode() GetHashCode() GetHashCode()

获取此 Font 的哈希代码。Gets the hash code for this Font.

GetHeight() GetHeight() GetHeight() GetHeight()

返回此字体的行距(以像素为单位)。Returns the line spacing, in pixels, of this font.

GetHeight(Graphics) GetHeight(Graphics) GetHeight(Graphics) GetHeight(Graphics)

采用指定的 Graphics 的当前单位,返回此字体的行距。Returns the line spacing, in the current unit of a specified Graphics, of this font.

GetHeight(Single) GetHeight(Single) GetHeight(Single) GetHeight(Single)

当用指定的垂直分辨率绘制到设备时,返回此 Font 的高度(以像素为单位)。Returns the height, in pixels, of this Font when drawn to a device with the specified vertical resolution.

GetLifetimeService() GetLifetimeService() GetLifetimeService() GetLifetimeService()

检索控制此实例的生存期策略的当前生存期服务对象。Retrieves the current lifetime service object that controls the lifetime policy for this instance.

(Inherited from MarshalByRefObject)
GetType() GetType() GetType() GetType()

获取当前实例的 TypeGets the Type of the current instance.

(Inherited from Object)
InitializeLifetimeService() InitializeLifetimeService() InitializeLifetimeService() InitializeLifetimeService()

获取生存期服务对象来控制此实例的生存期策略。Obtains a lifetime service object to control the lifetime policy for this instance.

(Inherited from MarshalByRefObject)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(Inherited from Object)
MemberwiseClone(Boolean) MemberwiseClone(Boolean) MemberwiseClone(Boolean) MemberwiseClone(Boolean)

创建当前 MarshalByRefObject 对象的浅表副本。Creates a shallow copy of the current MarshalByRefObject object.

(Inherited from MarshalByRefObject)
ToHfont() ToHfont() ToHfont() ToHfont()

返回此 Font 的句柄。Returns a handle to this Font.

ToLogFont(Object) ToLogFont(Object) ToLogFont(Object) ToLogFont(Object)

从此 Font 创建一个 GDI 逻辑字体 (LOGFONT) 结构。Creates a GDI logical font (LOGFONT) structure from this Font.

ToLogFont(Object, Graphics) ToLogFont(Object, Graphics) ToLogFont(Object, Graphics) ToLogFont(Object, Graphics)

从此 Font 创建一个 GDI 逻辑字体 (LOGFONT) 结构。Creates a GDI logical font (LOGFONT) structure from this Font.

ToString() ToString() ToString() ToString()

返回此 Font 可读的字符串表示形式。Returns a human-readable string representation of this Font.

显式界面实现

ISerializable.GetObjectData(SerializationInfo, StreamingContext) ISerializable.GetObjectData(SerializationInfo, StreamingContext) ISerializable.GetObjectData(SerializationInfo, StreamingContext) ISerializable.GetObjectData(SerializationInfo, StreamingContext)

使用将目标对象序列化所需的数据填充 SerializationInfoPopulates a SerializationInfo with the data needed to serialize the target object.

适用于

另请参阅