Font 類別
定義
定義文字的特定格式,包括字體、大小和樣式屬性 (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如何使用此函式Size和、 SizeInPoints和Unit屬性。The following code example demonstrates how to use the Font constructor and the Size, SizeInPoints, and Unit properties. 這個範例是設計來搭配 Windows Form 使用, 其中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
方法與ComboBox控制項SelectedIndexChanged的事件產生關聯。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 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。Initializes a new Font that uses the specified existing Font and FontStyle enumeration. |
Font(FontFamily, Single) |
使用指定的大小,初始化新的 Font。Initializes a new Font using a specified size. |
Font(FontFamily, Single, FontStyle) |
使用指定的大小和樣式,初始化新的 Font。Initializes a new Font using a specified size and style. |
Font(FontFamily, Single, FontStyle, GraphicsUnit) |
使用指定大小、樣式和單位,初始化新的 Font。Initializes a new Font using a specified size, style, and unit. |
Font(FontFamily, Single, FontStyle, GraphicsUnit, Byte) |
使用指定大小、樣式、單位和字元集,初始化新的 Font。Initializes a new Font using a specified size, style, unit, and character set. |
Font(FontFamily, Single, FontStyle, GraphicsUnit, Byte, Boolean) |
使用指定大小、樣式、單位和字元集,初始化新的 Font。Initializes a new Font using a specified size, style, unit, and character set. |
Font(FontFamily, Single, GraphicsUnit) |
使用指定大小和單位,初始化新的 Font。Initializes a new Font using a specified size and unit. 將樣式設為 Regular。Sets the style to Regular. |
Font(String, Single) |
使用指定的大小,初始化新的 Font。Initializes a new Font using a specified size. |
Font(String, Single, FontStyle) |
使用指定的大小和樣式,初始化新的 Font。Initializes a new Font using a specified size and style. |
Font(String, Single, FontStyle, GraphicsUnit) |
使用指定大小、樣式和單位,初始化新的 Font。Initializes a new Font using a specified size, style, and unit. |
Font(String, Single, FontStyle, GraphicsUnit, Byte) |
使用指定大小、樣式、單位和字元集,初始化新的 Font。Initializes a new Font using a specified size, style, unit, and character set. |
Font(String, Single, FontStyle, GraphicsUnit, Byte, Boolean) |
使用指定大小、樣式、單位和字元集,初始化新的 Font。Initializes a new Font using the specified size, style, unit, and character set. |
Font(String, Single, GraphicsUnit) |
使用指定大小和單位,初始化新的 Font。Initializes a new Font using a specified size and unit. 樣式設定為 Regular。The style is set to Regular. |
屬性
Bold |
取得值,表示這個 Font 是否為粗體。Gets a value that indicates whether this Font is bold. |
FontFamily |
取得與這個 FontFamily 關聯的 Font。Gets the FontFamily associated with this Font. |
GdiCharSet |
取得指定這個 Font 使用之 GDI 字元集的位元組值。Gets a byte value that specifies the GDI character set that this Font uses. |
GdiVerticalFont |
取得布林值,指示這個 Font 是否衍生自 GDI 垂直字型。Gets a Boolean value that indicates whether this Font is derived from a GDI vertical font. |
Height |
取得這個字型的行距。Gets the line spacing of this font. |
IsSystemFont |
取得值,指示字型是否為 SystemFonts 的成員。Gets a value indicating whether the font is a member of SystemFonts. |
Italic |
取得值,這個值表示此字型是否已套用斜體樣式。Gets a value that indicates whether this font has the italic style applied. |
Name | |
OriginalFontName |
取得原本指定之字型的名稱。Gets the name of the font originally specified. |
Size |
取得這個 Font 的 Em 大小,此大小是以 Unit 屬性指定的單位來測量的。Gets the em-size of this Font measured in the units specified by the Unit property. |
SizeInPoints |
取得這個 Font 的 Em 大小 (單位為點)。Gets the em-size, in points, of this Font. |
Strikeout |
取得值,表示這個 Font 是否指定字型中有水平線貫穿。Gets a value that indicates whether this Font specifies a horizontal line through the font. |
Style | |
SystemFontName |
如果 IsSystemFont 屬性傳回 |
Underline |
取得值,表示這個 Font 是否有底線。Gets a value that indicates whether this Font is underlined. |
Unit |
方法
Clone() | |
CreateObjRef(Type) |
建立包含所有相關資訊的物件,這些資訊是產生用來與遠端物件通訊的所需 Proxy。Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object. (繼承來源 MarshalByRefObject) |
Dispose() |
釋放這個 Font 所使用的所有資源。Releases all resources used by this Font. |
Equals(Object) |
表示指定的物件是否為 Font,而且是否具有與這個 Font 相同的 FontFamily、GdiVerticalFont、GdiCharSet、Style、Size 與 Unit 屬性值。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() |
允許物件在記憶體回收進行回收之前,嘗試釋放資源並執行其他清除作業。Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. |
FromHdc(IntPtr) |
從裝置內容的指定 Windows 控制代碼建立 Font。Creates a Font from the specified Windows handle to a device context. |
FromHfont(IntPtr) |
從指定的 Windows 控制代碼建立 Font。Creates a Font from the specified Windows handle. |
FromLogFont(Object) |
從指定的 GDI 邏輯字型 ( |
FromLogFont(Object, IntPtr) |
從指定的 GDI 邏輯字型 ( |
GetHashCode() | |
GetHeight() |
傳回這個字型的行距 (單位為像素)。Returns the line spacing, in pixels, of this font. |
GetHeight(Graphics) |
傳回這個字型的行距 (單位是指定之 Graphics 的目前單位)。Returns the line spacing, in the current unit of a specified Graphics, of this font. |
GetHeight(Single) |
傳回這個 Font 在使用指定之垂直解析度繪製至裝置時的高度 (單位為像素)。Returns the height, in pixels, of this Font when drawn to a device with the specified vertical resolution. |
GetLifetimeService() |
擷取控制這個執行個體存留期 (Lifetime) 原則的目前存留期服務物件。Retrieves the current lifetime service object that controls the lifetime policy for this instance. (繼承來源 MarshalByRefObject) |
GetType() |
取得目前執行個體的 Type。Gets the Type of the current instance. (繼承來源 Object) |
InitializeLifetimeService() |
取得存留期服務物件,以控制這個執行個體的存留期原則。Obtains a lifetime service object to control the lifetime policy for this instance. (繼承來源 MarshalByRefObject) |
MemberwiseClone() |
建立目前 Object 的淺層複製。Creates a shallow copy of the current Object. (繼承來源 Object) |
MemberwiseClone(Boolean) |
建立目前 MarshalByRefObject 物件的淺層複本。Creates a shallow copy of the current MarshalByRefObject object. (繼承來源 MarshalByRefObject) |
ToHfont() | |
ToLogFont(Object) |
從這個 Font 建立 GDI 邏輯字型 ( |
ToLogFont(Object, Graphics) |
從這個 Font 建立 GDI 邏輯字型 ( |
ToString() |
傳回這個 Font 的人們可解讀的字串表示。Returns a human-readable string representation of this Font. |
明確介面實作
ISerializable.GetObjectData(SerializationInfo, StreamingContext) |
將序列化目標物件所需的資料填入 SerializationInfo。Populates a SerializationInfo with the data needed to serialize the target object. |