天天看點

C#--GDI+的字型設定

在GDI+中可以用FontFamily和Font設定字型,其中FontFamily構造函數帶一字型參數,如:FontFamily ff = new FontFamily("Times New Roman");

Font類則有多個重載的函數:

  Font(IntPtr)  基礎結構。使用指定的指針初始化新的 Font。

  Font(Font, FontStyle)  初始化新 Font,它使用指定的現有 Font 和 FontStyle 枚舉。

  Font(FontFamily, Single)  使用指定的大小初始化新 Font。

  Font(String, Single)  使用指定的大小初始化新 Font。 

  Font(FontFamily, Single, FontStyle)  使用指定的大小和樣式初始化新 Font。

  Font(FontFamily, Single, GraphicsUnit)  使用指定的大小和機關初始化新的 Font。将此樣式設定為 FontStyle..::.Regular。 

  Font(String, Single, FontStyle)  使用指定的大小和樣式初始化新 Font。

  Font(String, Single, GraphicsUnit)  使用指定的大小和機關初始化新的 Font。将樣式設定為 FontStyle..::.Regular。

  Font(FontFamily, Single, FontStyle, GraphicsUnit)  使用指定的大小、樣式和機關初始化新的 Font。

  Font(String, Single, FontStyle, GraphicsUnit)  使用指定的大小、樣式和機關初始化新的 Font。

  Font(FontFamily, Single, FontStyle, GraphicsUnit, Byte)  使用指定的大小、樣式、機關和字元集初始化新的 Font。

  Font(String, Single, FontStyle, GraphicsUnit, Byte)  使用指定的大小、樣式、機關和字元集初始化新的 Font。

  Font(FontFamily, Single, FontStyle, GraphicsUnit, Byte, Boolean)  使用指定的大小、樣式、機關和字元集初始化新的 Font。

  Font(String, Single, FontStyle, GraphicsUnit, Byte, Boolean)  使用指定的大小、樣式、機關和字元集初始化新 Font。

最後用Graphics類的DrawString方法:

e.Graphics.DrawString("你好", new Font(new FontFamily("黑體"),12), Brushes.Black, new PointF(5,5));

示例代碼如下:

1 private void Form1_Paint( object sender, PaintEventArgs e)

2 {

3 Graphics g = e.Graphics;

4 g.FillRectangle(Brushes.White, this .ClientRectangle);

5

6 FontFamily ff = new FontFamily( " Times New Roman " );

7 Font f = new Font(ff, 12 );

8 string s = " Height: " + f.Height;

9 SizeF sf = g.MeasureString(s, f, Int32.MaxValue, StringFormat.GenericTypographic);

10 RectangleF r = new RectangleF( 0 , 0 , sf.Width, f.Height);

11 g.DrawRectangle(Pens.Black, r.Left, r.Top, r.Width, r.Height);

12 g.DrawString(s, f, Brushes.Black, r, StringFormat.GenericTypographic);

13

14 f.Dispose();

15 }

c#