作法:建構字型家族和字型How to: Construct Font Families and Fonts

GDI + 劃分為具有相同的字樣,但不同的字型的字型系列。GDI+ groups fonts with the same typeface but different styles into font families. 比方說,新細明體字型系列包含下列字型:For example, the Arial font family contains the following fonts:

  • 新細明體的一般Arial Regular

  • 新細明體粗體Arial Bold

  • 新細明體斜體Arial Italic

  • 新細明體Arial Bold Italic

GDI + 會使用表單系列的四個樣式: 一般、 粗體、 斜體和粗體斜體。GDI+ uses four styles to form families: regular, bold, italic, and bold italic. 這類的形容詞縮小捨入不會被視為樣式; 而是它們屬於的系列名稱。Adjectives such as narrow and rounded are not considered styles; rather they are part of the family name. 比方說,新細明體窄時,為字型家族,具有下列成員:For example, Arial Narrow is a font family with the following members:

  • 新細明體窄一般Arial Narrow Regular

  • 粗體的新細明體Arial Narrow Bold

  • 新細明體窄斜體Arial Narrow Italic

  • 窄的新細明體Arial Narrow Bold Italic

您可以繪製使用 GDI + 的文字之前,您必須建構FontFamily物件和Font物件。Before you can draw text with GDI+, you need to construct a FontFamily object and a Font object. FontFamily物件會指定 (例如,新細明體),字樣和Font物件指定大小、 樣式和單位。The FontFamily object specifies the typeface (for example, Arial), and the Font object specifies the size, style, and units.

範例Example

下列範例會建構規則的樣式新細明體字型的大小為 16 個像素。The following example constructs a regular style Arial font with a size of 16 pixels. 在下列程式碼中,第一個引數傳遞給Font建構函式是FontFamily物件。In the following code, the first argument passed to the Font constructor is the FontFamily object. 第二個引數會指定在第四個引數所識別的單位中測量的字型的大小。The second argument specifies the size of the font measured in units identified by the fourth argument. 第三個引數識別的樣式。The third argument identifies the style.

Pixel 隸屬GraphicsUnit列舉型別,並Regular隸屬FontStyle列舉型別。Pixel is a member of the GraphicsUnit enumeration, and Regular is a member of the FontStyle enumeration.

FontFamily fontFamily = new FontFamily("Arial");
Font font = new Font(
   fontFamily,
   16,
   FontStyle.Regular,
   GraphicsUnit.Pixel);
Dim fontFamily As New FontFamily("Arial")
Dim font As New Font( _
   fontFamily, _
   16, _
   FontStyle.Regular, _
   GraphicsUnit.Pixel)

編譯程式碼Compiling the Code

上述範例中專為搭配 Windows Form 使用,而且需要PaintEventArgs e,這是參數的PaintEventHandlerThe preceding example is designed for use with Windows Forms, and it requires PaintEventArgs e, which is a parameter of PaintEventHandler.

另請參閱See also