Font Font Font Font Class

定義

定義文字的特定格式,包括字體、大小和樣式屬性 (Attribute)。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建構函式與SizeSizeInPoints,和Unit屬性。The following code example demonstrates how to use the Font constructor and the Size, SizeInPoints, and Unit properties. 這個範例設計來搭配 Windows 表單,其中包含ComboBox名為ComboBox1,並填入字串"越大 」 和 「 小 」 並Label名為Label1This 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方法SelectedIndexChanged事件的ComboBox控制項。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

備註

如需如何建構字型的詳細資訊,請參閱How to:建構字型系列和字型For more information about how to construct fonts, see How to: Construct Font Families and Fonts. Windows Forms 應用程式支援 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)

初始化使用指定之現有 FontFont 列舉的新 FontStyleInitializes 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 的 Em 大小,此大小是以 Unit 屬性指定的單位來測量的。Gets the em-size of this Font measured in the units specified by the Unit property.

SizeInPoints SizeInPoints SizeInPoints SizeInPoints

取得這個 Font 的 Em 大小 (單位為點)。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)

建立包含所有相關資訊的物件,這些資訊是產生用來與遠端物件通訊的所需 Proxy。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,而且是否具有與這個 Font 相同的 FontFamilyGdiVerticalFontGdiCharSetStyleSizeUnit 屬性值。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()

擷取控制這個執行個體存留期 (Lifetime) 原則的目前存留期服務物件。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 的淺層複本 (Shallow Copy)。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.

適用於

另請參閱