当前位置:C++技术网 > 资讯 > GDI+学习记录-字体Font

GDI+学习记录-字体Font

更新时间:2015-10-05 18:56:21浏览次数:1+次

     字体,对于大部分人来说都不陌生,在文本编辑软件中(如 Word)字体是必不可少的,同样,在GDI+中,绘制字符串也是需要字体的。在介绍字体Font类的使用之前,先引入一些与其有关的类或者枚举:
1)字体系列 FontFamily:

            GDI+中将具有相同的样式成为字体系列,如我们常见的 “宋体”、“仿宋”  、“微软雅黑”、 “Arial”等。


(2) 字体风格枚举 FontSytle:
            FontStyle 枚举列出了常见的字体风格,其定义如下:

enum FontStyle  
  
{  
    Regular,        //常规  
     Bold,           //加粗  
     Italic,         //倾斜  
     Underline,      //下划线  
     Strikout        //强调线  
}  
(3)字体的大小单位 GraphicsUnit :
            在GDI+ 中进行字体描述,文本输出时,不可避免的使用到描述宽度或高度的单位(如像素、磅),GraphicsUnit 枚举列出了GDI+中常用的单位,其定义如下:
enum GraphicsUnit  
  
{  
     Display,       //指定显示设备的度量单位。通常,视频显示使用的单位是像素;打印机使用的单位是 1/100 英寸。   
      Document,      //将文档单位(1/300 英寸)指定为度量单位。  
      Inch,          //将英寸指定为度量单位。   
      Millimeter,    //将毫米指定为度量单位。  
      Pixel,         //将设备像素指定为度量单位。   
      Point,         //将打印机点(1/72 英寸)指定为度量单位。  
      World          //将世界坐标系单位指定为度量单位。  
}  
构建字体Font对象:
             再来看看Font类的一个构造函数,在结合以上的信息,聪明的你应该知道这个构造函数该如下去写了吧:
public Font (FontFamily family,float emSize,FontStyle style,GraphicsUnit unit)  
下列Demo示范font 的基本使用:
private void Form1_Paint(object sender, PaintEventArgs e)  
       {  
           Graphics g = e.Graphics;  
           g.Clear(Color.White);  
  
           //消除锯齿  
           g.SmoothingMode = SmoothingMode.AntiAlias;  
  
           FontFamily fontFamily = new FontFamily("宋体");  
           Font myFont1 = new Font(fontFamily,15,FontStyle.Regular,GraphicsUnit.Pixel);  
           g.DrawString("字体系列为宋体,大小为15,字体风格为常规的黑色字体",myFont1,Brushes.Black,new PointF(10,50));  
  
           Font myFont2 = new Font("Arial",25.5f,FontStyle.Italic,GraphicsUnit.Pixel);  
           g.DrawString("字体系列为Arial,大小为25.5,字体风格为倾斜的红色字体", myFont2, Brushes.Red, new PointF(10, 100));  
  
           Font myFont3 = new Font("微软雅黑", 20, FontStyle.Underline, GraphicsUnit.Pixel);  
           TextRenderer.DrawText(g,"使用TextRenderer类绘制字符串",myFont3,new Point(10,150),Color.Green);  
  
  
           //释放资源  
           fontFamily.Dispose();  
           myFont1.Dispose();  
           myFont2.Dispose();  
           myFont3.Dispose();  
  
       }  

效果图:

原文摘自CSDN。