Font 類別

定義

定義文字的特定格式,包括字體、大小和樣式屬性 (Attribute)。 此類別無法獲得繼承。

public ref class Font sealed : MarshalByRefObject, ICloneable, IDisposable, System::Runtime::Serialization::ISerializable
public sealed class Font : MarshalByRefObject, ICloneable, IDisposable, System.Runtime.Serialization.ISerializable
[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
[System.ComponentModel.TypeConverter(typeof(System.Drawing.FontConverter))]
public sealed class Font : MarshalByRefObject, ICloneable, IDisposable, System.Runtime.Serialization.ISerializable
[System.ComponentModel.TypeConverter(typeof(System.Drawing.FontConverter))]
[System.Serializable]
public sealed class Font : MarshalByRefObject, ICloneable, IDisposable, System.Runtime.Serialization.ISerializable
[System.ComponentModel.TypeConverter(typeof(System.Drawing.FontConverter))]
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public sealed class Font : MarshalByRefObject, ICloneable, IDisposable, System.Runtime.Serialization.ISerializable
type Font = class
    inherit MarshalByRefObject
    interface ICloneable
    interface IDisposable
    interface ISerializable
[<System.ComponentModel.TypeConverter("System.Drawing.FontConverter, System.Windows.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51")>]
type Font = class
    inherit MarshalByRefObject
    interface ICloneable
    interface IDisposable
    interface ISerializable
[<System.ComponentModel.TypeConverter(typeof(System.Drawing.FontConverter))>]
type Font = class
    inherit MarshalByRefObject
    interface ICloneable
    interface IDisposable
    interface ISerializable
[<System.ComponentModel.TypeConverter(typeof(System.Drawing.FontConverter))>]
[<System.Serializable>]
type Font = class
    inherit MarshalByRefObject
    interface ICloneable
    interface IDisposable
    interface ISerializable
[<System.ComponentModel.TypeConverter(typeof(System.Drawing.FontConverter))>]
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type Font = class
    inherit MarshalByRefObject
    interface ICloneable
    interface ISerializable
    interface IDisposable
Public NotInheritable Class Font
Inherits MarshalByRefObject
Implements ICloneable, IDisposable, ISerializable
繼承
屬性
實作

範例

下列程式碼範例示範如何使用 Font 建構函式和 SizeSizeInPointsUnit 屬性。 此範例的設計目的是要與包含 ComboBoxComboBox1 名為 的 Windows Form 搭配使用,此名稱會填入字串 「Bigger」 和 「Smaller」 和 Label 具名 Label1 。 將下列程式碼貼到表單中,並將 方法與 SelectedIndexChanged 控制項的 ComboBox 事件產生關聯 ComboBox1_SelectedIndexChanged

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 Forms應用程式支援 TrueType 字型,而且對 OpenType 字型的支援有限。 如果您嘗試使用不支援的字型,或在執行應用程式的電腦上未安裝字型,則會取代 Microsoft Sans Serif 字型。

注意

在 .NET 6 和更新版本中,只有 Windows 作業系統才支援包含此類型的 System.Drawing.Common 套件。 在跨平臺應用程式中使用此類型會導致編譯時間警告和執行時間例外狀況。 如需詳細資訊,請參閱 僅限 Windows 上支援的 System.Drawing.Common

建構函式

Font(Font, FontStyle)

初始化使用指定之現有 FontFont 列舉的新 FontStyle

Font(FontFamily, Single)

使用指定的大小,初始化新的 Font

Font(FontFamily, Single, FontStyle)

使用指定的大小和樣式,初始化新的 Font

Font(FontFamily, Single, FontStyle, GraphicsUnit)

使用指定大小、樣式和單位,初始化新的 Font

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

使用指定大小、樣式、單位和字元集,初始化新的 Font

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

使用指定大小、樣式、單位和字元集,初始化新的 Font

Font(FontFamily, Single, GraphicsUnit)

使用指定大小和單位,初始化新的 Font。 將樣式設為 Regular

Font(String, Single)

使用指定的大小,初始化新的 Font

Font(String, Single, FontStyle)

使用指定的大小和樣式,初始化新的 Font

Font(String, Single, FontStyle, GraphicsUnit)

使用指定大小、樣式和單位,初始化新的 Font

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

使用指定大小、樣式、單位和字元集,初始化新的 Font

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

使用指定大小、樣式、單位和字元集,初始化新的 Font

Font(String, Single, GraphicsUnit)

使用指定大小和單位,初始化新的 Font。 樣式設定為 Regular

屬性

Bold

取得值,表示這個 Font 是否為粗體。

FontFamily

取得與這個 FontFamily 關聯的 Font

GdiCharSet

取得指定這個 Font 使用之 GDI 字元集的位元組值。

GdiVerticalFont

取得布林值,指示這個 Font 是否衍生自 GDI 垂直字型。

Height

取得這個字型的行距。

IsSystemFont

取得值,指示字型是否為 SystemFonts 的成員。

Italic

取得值,這個值表示此字型是否已套用斜體樣式。

Name

取得這個 Font 的字樣名稱。

OriginalFontName

取得原本指定之字型的名稱。

Size

取得這個 Font 的 Em 大小,此大小是以 Unit 屬性指定的單位來測量的。

SizeInPoints

取得這個 Font 的 Em 大小 (單位為點)。

Strikeout

取得值,表示這個 Font 是否指定字型中有水平線貫穿。

Style

取得這個 Font 的樣式資訊。

SystemFontName

如果 IsSystemFont 屬性傳回 true,則取得系統字型的名稱。

Underline

取得值,表示這個 Font 是否有底線。

Unit

取得這個 Font 的測量單位。

方法

Clone()

建立這個 Font 的完全相同複本。

CreateObjRef(Type)

建立包含所有相關資訊的物件,這些資訊是產生用來與遠端物件通訊的所需 Proxy。

(繼承來源 MarshalByRefObject)
Dispose()

釋放這個 Font 所使用的所有資源。

Equals(Object)

表示指定的物件是否為 Font,而且是否具有與這個 Font 相同的 FontFamilyGdiVerticalFontGdiCharSetStyleSizeUnit 屬性值。

Finalize()

允許物件在記憶體回收進行回收之前,嘗試釋放資源並執行其他清除作業。

FromHdc(IntPtr)

從裝置內容的指定 Windows 控制代碼建立 Font

FromHfont(IntPtr)

從指定的 Windows 控制代碼建立 Font

FromLogFont(LOGFONT)

定義文字的特定格式,包括字體、大小和樣式屬性 (Attribute)。 此類別無法獲得繼承。

FromLogFont(LOGFONT, IntPtr)

定義文字的特定格式,包括字體、大小和樣式屬性 (Attribute)。 此類別無法獲得繼承。

FromLogFont(Object)

Font從指定的 GDI 邏輯字型建立 (LOGFONT) 結構。

FromLogFont(Object, IntPtr)

Font從指定的 GDI 邏輯字型建立 (LOGFONT) 結構。

GetHashCode()

取得這個 Font 的雜湊碼。

GetHeight()

傳回這個字型的行距 (單位為像素)。

GetHeight(Graphics)

傳回這個字型的行距 (單位是指定之 Graphics 的目前單位)。

GetHeight(Single)

傳回這個 Font 在使用指定之垂直解析度繪製至裝置時的高度 (單位為像素)。

GetLifetimeService()
已淘汰.

擷取控制這個執行個體存留期 (Lifetime) 原則的目前存留期服務物件。

(繼承來源 MarshalByRefObject)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
InitializeLifetimeService()
已淘汰.

取得存留期服務物件,以控制這個執行個體的存留期原則。

(繼承來源 MarshalByRefObject)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
MemberwiseClone(Boolean)

建立目前 MarshalByRefObject 物件的淺層複本。

(繼承來源 MarshalByRefObject)
ToHfont()

傳回這個 Font 的控制代碼。

ToLogFont(LOGFONT)

定義文字的特定格式,包括字體、大小和樣式屬性 (Attribute)。 此類別無法獲得繼承。

ToLogFont(LOGFONT, Graphics)

定義文字的特定格式,包括字體、大小和樣式屬性 (Attribute)。 此類別無法獲得繼承。

ToLogFont(Object)

從這個 Font 建立 GDI 邏輯字型 (LOGFONT) 結構。

ToLogFont(Object, Graphics)

從這個 Font 建立 GDI 邏輯字型 (LOGFONT) 結構。

ToString()

傳回這個 Font 的人們可解讀的字串表示。

明確介面實作

ISerializable.GetObjectData(SerializationInfo, StreamingContext)

將序列化目標物件所需的資料填入 SerializationInfo

適用於

另請參閱