zoukankan      html  css  js  c++  java
  • 字体

    FontFamily类

    用FontFamily类的实例表示字体系列--一组基本设计类似、但在样式上有某些变化的字样。标准字体样式中的变化一般有黑体、斜体和黑斜体,如Arial字体系列包含4个字样:Arial Regular 、Arial Bold、Arial Italic、Arial Bold Italic。

    创建FontFamily对象:

    方法一:FontFamily ff = new FontFamily(“Arial”)

    方法二:使用GenericFontFamily枚举的值(该枚举是System。Drawing。Text命名空间的一部分)FontFamily ff = new FontFamily(GenericFontFamily。Serif)

    可以使用InstalledFontCollection对象来验证字体是否已安装。

    创建好FontFamily对象后,就可以使用它定义某个Font对象,FontFamily ff = new FontFamily("Arial"); Font f = new Font(ff,12);另外,还可以直接定义字体Font f = new Font("Arial",12);

    使用默认的Font对象时不需要创建该对象也不需要释放它,如:

    Graphics g = e.Graphics;

    g.DrawString("Hello world",this.Font,Brushes.Black,0,0);

    格式化文本的最简单方式,也是最常用的方式是在创建Font对象时设置字体样式。字体样式有5个基本选项:Bold、Italic、Regular、Strikeout和Underline,可以使用布尔运算符OR或|把这些值组合在一起,获得样式的综合效果。

    行、对齐和方向

    GDI+可以使用单词自动换行功能来绘制对行文本,实现时只需调用DrawString方法,传送一个矩形,其宽度小于文本字符串的宽度,其高度则足以容纳多行文本:

    Graphics g = e.Graphics;             g.FillRectangle(Brushes.White, this.ClientRectangle);             String s = "This string is long enough to wrap.";             s += "With a 200px - width rectangle,and a 12pt font.";             s += "it requires six lines to display the string in its entirety.";             Font f = new Font("Arial", 12);             Rectangle r = new Rectangle(20, 20, 200, f.Height * 6);             g.DrawRectangle(Pens.Black, r);             g.DrawString(s, f, Brushes.Black, r);             f.Dispose();

      在运行时控制行数可以使用MeasureString方法计算Size对象,该对象的长度反映了字符串的长度和宽度限制,然后使用该Size对象设置矩形的宽度和高度。,下面的例子在矩形中绘制一些文本,该矩形的宽度不大于150像素,但可根据需要容纳任意多行文本。

    Graphics g = e.Graphics;             g.FillRectangle(Brushes.White, this.ClientRectangle);             String s = "This string is long enough to wrap.";             s += "With a 200px - width rectangle,and a 12pt font.";             s += "it requires six lines to display the string in its entirety.";             Font f = new Font("Arial", 12);             SizeF sf = g.MeasureString(s, f, 150);             RectangleF rf = new RectangleF(20, 20, sf.Width, sf.Height);             g.DrawRectangle(Pens.Black, rf.Left, rf.Top, rf.Width, rf.Height);             g.DrawString(s, f, Brushes.Black, rf);

     

    禁止文本自动换行

    创建一个StringFormat对象,把FormatFlags属性设置为StringFormatFlags.NoWrap,并把StringFormat对象传送给DrawString方法,禁止文本自动换行。

    Graphics g = e.Graphics;             g.FillRectangle(Brushes.White, this.ClientRectangle);             String s = "This string is long enough to wrap.";             Font f = new Font("Arial", 12);             Rectangle r = new Rectangle(20, 20, 150, f.Height * 4);             StringFormat sf = new StringFormat();             sf.FormatFlags = StringFormatFlags.NoWrap;             g.DrawRectangle(Pens.Black, r);             g.DrawString(s, f, Brushes.Black, r, sf);             f.Dispose();

     

    默认情况下文本时左对齐,可以设置文本的水平和垂直居中,为此可以再次使用StringFormat对象的功能,这次把它的Alignment和LineAlignment属性设置为相应的值,并把该对象传送给DrawString方法。

    StringFormat sf = new StringFormat();

    sf.Alignment = StringAlignment.Center;

    sf.LineAlignment = StringAlignment.Center;

    绘制竖向文本有两个方法,一是定义一些横向文本,然后把坐标系空间旋转90°,二是创建一个StringFormat对象来绘制竖向文本(使用stringformat类构造函数的一个重载版本,给它传送一个directionvertical值)

    Graphics g = e.Graphics;             g.FillRectangle(Brushes.White, this.ClientRectangle);             String s = "This string is long enough to wrap.";             StringFormat sf = new StringFormat(StringFormatFlags.DirectionVertical);             Font f = new Font("Times New Roman", 14);             SizeF sizef = g.MeasureString(s, f, Int32.MaxValue, sf);             RectangleF rf = new RectangleF(20, 20, sizef.Width, sizef.Height);             g.DrawRectangle(Pens.Black, rf.Left, rf.Top, rf.Width, rf.Height);             g.DrawString(s, f, Brushes.Black, rf, sf);             f.Dispose();

  • 相关阅读:
    好用的在线作图软件
    xml简介
    P1832 A+B Problem(再升级)
    P1831 杠杆数
    P1802 5倍经验日
    P1679 神奇的四次方数
    Android学习四、Android中的Adapter
    Android Activity Fragment 生命周期
    《C语言编写 学生成绩管理系统》
    线程池QueueUserWorkItem
  • 原文地址:https://www.cnblogs.com/ttssrs/p/2396498.html
Copyright © 2011-2022 走看看