当前位置: 首页 > article >正文

17.2 图形绘制7

版权声明:本文为博主原创文章,转载请在显著位置标明本文出处以及作者网名,未经作者允许不得用于商业目的。

17.2.9 字体

17.2.9.1 Font类

Font类定义特定的文本格式,包括字体、字号和样式特性。

Font常用属性:

  1. Name:字体名称。
  2. FontFamily:获取与此FontFamily关联的 Font。
  3. Bold:是否设置为粗体。
  4. Italic:是否设置为斜体。
  5. Underline:是否设置下划线。
  6. Strikeout:是否设置删除线(文字中间有一横线)。
  7. Unit:度量单位,包括Point(打印机点,1/72 英寸)、Pixel(像素)等。
  8. Size:以设置的Unit为单位的字体大小。
  9. Style:获取此 Font 的样式信息。这是一个FontStyle枚举,包含以下成员:
  1. Bold:粗体。
  2. Italic:斜体。
  3. Regular:常规。
  4. Strikeout:删除线。
  5. Underline:下划线。
  1. Height:获取此字体的行距。

Font构造函数常用的重载版本:

1、public Font( FontFamily family, float emSize )

参数说明:

  1. familyName:字体名称。
  2. emSize:大小(单位:磅)。

2、public Font( FontFamily family, float emSize, FontStyle style )

参数说明:

  1. family:新字体的 FontFamily。
  2. emSize:大小(单位:磅)。
  3. style:新字体的FontStyle,这是一个FontStyle枚举。
17.2.9.2 FontFamily类

FontFamily类定义有着相似的基本设计但在形式上有某些差异的一组字样。

FontFamily常用属性:

  1. Families:静态成员,返回一个包含与当前图形上下文相关的所有FontFamily对象的数组。
  2. Name:获取此 FontFamily 的名称。

FontFamily常用方法:

  1. IsStyleAvailable:指定的 FontStyle 枚举是否可用。
17.2.9.3 InstalledFontCollection类

InstalledFontCollection 类表示安装在系统上的字体,它位于System.Drawing.Text命名空间下。

InstalledFontCollection常用属性:

  1. Families:获取关联的FontFamily对象的数组,这个数组包含了计算机上安装的字体。在实际使用时,此属性和FontFamily的Families属性类似。

【例 17.22【项目:code17-022】枚举本地计算机上安装的字体。

        private void Form1_Load(object sender, EventArgs e)

        {

            comFont.DropDownStyle = ComboBoxStyle.DropDownList;

            //获得字体名称

            //InstalledFontCollection 允许您获取使用 Families 属性运行应用程序的计算机上安装的字体系列的列表--Msdn

            InstalledFontCollection Installfont = new InstalledFontCollection();

            //开始枚举字体名称并加入到comFont中

            //foreach(FontFamily ff in FontFamily.Families)

            //或者

            foreach (FontFamily ff in Installfont.Families)

                comFont.Items.Add(ff.Name);

            comFont.Text = comFont.Items[0].ToString();

        }

        private void comFont_SelectedIndexChanged(object sender, EventArgs e)

        {

            //使用comFont中选择的字体

            Font f =new Font(comFont.Text, 12);

            //lblFontShow的文本为选择字体的名称

            lblFontShow.Text = f.Name;

            //lblFontShow的字体为选择的字体

        l

运行结果如下图所示:

图17-25 枚举并显示选中的字体

17.2.9.4 绘制字符串

在Graphics上绘制字符串的方法是DrawString,常用的一个重载版本:

public void DrawString( string s, Font font, Brush brush, PointF point )

参数说明:

  1. s:要绘制的字符串。
  2. font:Font,定义字符串的文本格式。
  3. brush:Brush,确定所绘制文本的颜色和纹理。
  4. point:PointF结构,指定所绘制文本的左上角。

【例 17.23【项目:code17-023】绘制字符串。

        private void button1_Click(object sender, EventArgs e)

        {

            //声明一个字体名称为隶书,大小20,粗体,以点为单位的Font

            Font f = new Font("隶书", 20, FontStyle.Bold, GraphicsUnit.Point);

            //需要绘制的字符串

            string strDrawString = "绘制字符串:" + f.Name;

            //绘制字符串的左上角位置

            Point posDrawString = new Point(10, 20);

            Graphics g = this.CreateGraphics();

            //使用DrawString方法绘制字符串

            g.DrawString(strDrawString, f, new SolidBrush(Color.Red), posDrawString);

            g.Dispose();

        }

运行结果如下图所示:

图17-26 绘制字符串

DrawString方法另一个重载版本指定在一个矩形区域内绘制字符串:

public void DrawString( string s, Font font, Brush brush, RectangleF layoutRectangle )

参数说明:

  1. s:要绘制的字符串。
  2. font:Font,定义字符串的文本格式。
  3. brush:Brush,确定所绘制文本的颜色和纹理。
  4. ayoutRectangle:RectangleF 结构,指定所绘制文本的位置。

【例 17.24【项目:code17-024】在指定的矩形区域内绘制字符串。

        private void button1_Click(object sender, EventArgs e)

        {

            Font f = new Font("隶书", 20, FontStyle.Bold, GraphicsUnit.Point);

            string strDrawString = "绘制字符串:" + f.Name;

            //绘制字符串的矩形区域

            Rectangle posDrawString = new Rectangle(10, 20, 120, 50);

            Graphics g = this.CreateGraphics();

            //使用DrawString方法绘制字符串

            g.DrawString(strDrawString, f, new SolidBrush(Color.Red), posDrawString);

            g.DrawRectangle(new Pen(Color.Blue, 2), posDrawString);

            g.Dispose();

        }

运行结果如下图所示:

图17-27 在矩形区域内绘制字符串

以上两个重载方法还可以增加对文本布局信息的支持:

  1. public void DrawString( string s, Font font, Brush brush, PointF point, StringFormat format)
  2. public void DrawString( string s, Font font, Brush brush, RectangleF layoutRectangle, StringFormat format)

其中参数format是一个StringFormat 类,它封装文本布局信息(如对齐方式、方向和制表位)、显示操作(如省略号插入和区域数字替换)和OpenType功能。几个常用的属性:

  1. Alignment:文本字符串相对于其布局矩形的对齐方式。这是一个StringAlignment枚举,包含以下成员:
    1. Center:指定文本在布局矩形中居中对齐。
    2. Far:指定文本靠远布局位置对齐。如果是从左到右书写,起始点位于左侧,离右侧远,那么右对齐;否则左对齐。
    3. Near:指定文本靠近布局位置对齐。如果是从左到右书写,起始点位于左侧,右侧远,那么左对齐;否则右对齐。
  2. FormatFlags:文本字符串的显示和布局信息。这是一个 StringFormatFlags 枚举,常用成员有:
    1. DirectionRightToLeft:按从右向左的顺序显示文本。
    2. DirectionVertical:文本垂直对齐。
  3. Trimming:指定如何在不完全适合布局形状的字符串中修整字符。这是一个StringTrimming枚举,包含以下成员:
  1. Character:指定将文本修整成最接近的字符。
  2. EllipsisCharacter:指定将文本修整成最接近的字符,并在被修整的行的末尾插入一个省略号。
  3. EllipsisPath:中心从被修整的行移除并用省略号替换。 这种算法尽可能多地保留了行中的最后一个由斜杠分隔的段。
  4. EllipsisWord:指定将文本修整成最接近的单词,并在被修整的行的末尾插入一个省略号。
  5. None:指定不进行任何修整。
  6. Word:指定将文本修整成最接近的单词。

【例 17.25【项目:code17-025】绘制字符串设置布局信息。

        private void button1_Click(object sender, EventArgs e)

        {

            Font f = new Font("隶书", 15, FontStyle.Bold, GraphicsUnit.Point);

            string strDrawString = "垂直绘制字符串";

            PointF posDrawString =new PointF(10, 20);

            StringFormat sf =new StringFormat();

            //文本垂直对齐

            sf.FormatFlags = StringFormatFlags.DirectionVertical;

            Graphics g = this.CreateGraphics();

            g.DrawString(strDrawString, f, new SolidBrush(Color.Red), posDrawString, sf);

            g.Dispose();

        }

        private void button2_Click(object sender, EventArgs e)

        {

            Font f = new Font("隶书", 15, FontStyle.Bold, GraphicsUnit.Point);

            string strDrawString = "这个例子示范了如何在矩形内绘制字符串";

            Rectangle posDrawString = new Rectangle(50, 20, 120, 60);

            StringFormat sf = new StringFormat();

            //超出矩形空间时截断并插入一个省略号

            sf.Trimming = StringTrimming.EllipsisCharacter;

            Graphics g = this.CreateGraphics();

            g.DrawString(strDrawString, f, new SolidBrush(Color.Red), posDrawString, sf);

            g.DrawRectangle(new Pen(Color.Blue, 1), posDrawString);

            g.Dispose();

        }

运行结果如下图所示:

图17-28 绘制垂直字符串以及截断字符串

17.2.9.5 获得绘制字符串的高度和宽度

通过Graphics的DrawString方法很容易就“画”出了字符串,但是也有个问题,如何获得绘制字符串得高度和宽度,从而有效地在给定空间内绘制。

常用的有两种方法可以获得绘制字符串的高度和宽度。

1、使用Graphics类的MeasureString方法。常用的一个重载版本:

public SizeF MeasureString( string text, Font font )

参数说明:

  1. text:要测量的字符串。
  2. font:Font,它定义字符串的文本格式。

返回值:

  1. 返回 SizeF 结构,该结构表示 text 参数指定的、使用 font 参数绘制的字符串的大小,单位由 PageUnit 属性指定。

2、使用TextRenderer类,它提供用于测量和呈现文本的方法。

TextRenderer常用方法都是静态方法:

  1. DrawText:使用指定的设备上下文、字体和颜色在指定位置绘制指定文本。
  2. MeasureText:在使用指定字体绘制时,提供指定文本的尺寸(以像素为单位)。

【例 17.26【项目:code17-026】获得绘制字符串的高度和宽度。

        private void button1_Click(object sender, EventArgs e)

        {

            Font f = new Font("隶书", 20, FontStyle.Bold, GraphicsUnit.Point);

            string strDrawText = "字体测试:" + f.Name;

            //方法1:Graphics.MeasureString

            SizeF sizeDrawText1;

            Point posDrawText1 = new Point(10, 10);

            Graphics g = this.CreateGraphics();

            //此方法返回 SizeF 结构,该结构表示 text 参数指定的、使用 font 参数绘制的字符串的大小,单位由 PageUnit 属性指定。

            sizeDrawText1 = g.MeasureString(strDrawText, f);

            g.DrawString(strDrawText, f, new SolidBrush(Color.Red), posDrawText1);

            g.DrawRectangle(new Pen(Color.Blue), posDrawText1.X, posDrawText1.Y, sizeDrawText1.Width, sizeDrawText1.Height);

            //方法2:TextRenderer.MeasureText

            Point posDrawText2 =new Point(10, 50);

            //使用指定字体进行绘制时测量指定的文本。

            SizeF sizeDrawtext2 = TextRenderer.MeasureText(strDrawText, f);

            g.DrawString(strDrawText, f, new SolidBrush(Color.Red), posDrawText2);

            g.DrawRectangle(new Pen(Color.Green), posDrawText2.X, posDrawText2.Y, sizeDrawtext2.Width, sizeDrawtext2.Height);

            //方法2扩展

            Point posDrawText3 = new Point(10, 90);

            //增加TextFormatFlags.NoPadding参数

            SizeF sizeDrawtext3 = TextRenderer.MeasureText(g, strDrawText, f, new Size(int.MaxValue, int.MaxValue), TextFormatFlags.NoPadding);

            g.DrawString(strDrawText, f, new SolidBrush(Color.Red), posDrawText3);

            g.DrawRectangle(new Pen(Color.Green), posDrawText3.X, posDrawText3.Y, sizeDrawtext3.Width, sizeDrawtext3.Height);

            //输出各个方法获得文字长宽的信息

            textBox1.Text = "使用MeasureString:" + "\r\n";

            textBox1.Text += "高度:" + sizeDrawText1.Height + "\r\n";

            textBox1.Text += "宽度:" + sizeDrawText1.Width + "\r\n";

            textBox1.Text += "使用TextRenderer:" + "\r\n";

            textBox1.Text += "高度:" + sizeDrawtext2.Height + "\r\n";

            textBox1.Text += "宽度:" + sizeDrawtext2.Width + "\r\n";

            textBox1.Text += "加上TextFormatFlags.NoPadding:" + "\r\n";

            textBox1.Text += "高度:" + sizeDrawtext3.Height + "\r\n";

            textBox1.Text += "宽度:" + sizeDrawtext3.Width + "\r\n";

            g.Dispose();

        }

运行结果如下图所示:

图17-29 绘制字符串的宽度和高度信息

从图17-29可以看出,第二种方法的扩展形态更准确一些。当然,具体问题具体分析,请根据实际情况来选择处理方法。

17.2.9.6 使用字体文件

有时候会在应用程序中使用字体文件(未安装到计算机上的字体),这个时候就可以使用PrivateFontCollection 类。

PrivateFontCollection 类可以通过字体文件生成字体集合。它属于System.Drawing.Text命名空间。

PrivateFontCollection常用属性:

  1. Families:关联的FontFamily对象的数组。

PrivateFontCollection常用方法:

  1. AddFontFile:将字体从字体文件添加到PrivateFontCollection中。它的参数是要添加的字体文件的路径。

注意:Windows 窗体应用程序支持TrueType字体,对OpenType字体提供有限的支持。如果尝试使用不受支持的字体,将出现异常。

使用字体文件时,通常使用PrivateFontCollection的AddFontFile方法将字体加入到集合中,从而获得Families属性中关联的FontFamily对象,使用Font的构造函数的重载版本即可实例化一个Font对象。

【例 17.27【项目:code17-027】使用字体文件。

        private void button1_Click(object sender, EventArgs e)

        {

            OpenFileDialog ofd = new OpenFileDialog();

            ofd.Filter = "字体文件|*.ttf";

            if (ofd.ShowDialog() != DialogResult.OK)

                return;

            string fontFile;

            fontFile = ofd.FileName;

            Graphics g = this.CreateGraphics();

            g.Clear(this.BackColor);

            //实例化一个PrivateFontCollection对象

            PrivateFontCollection pfonts =new PrivateFontCollection();

            //使用AddFontFile方法添加字体文件

            pfonts.AddFontFile(fontFile);

            //检查是否支持常规字体,可以换成Bold(粗体)、Italic(斜体)等进行检测。

            if( pfonts.Families[0].IsStyleAvailable(FontStyle.Regular))

            {

                Font f =new Font(pfonts.Families[0], 20, FontStyle.Regular);

                string fontname = f.Name;

                SolidBrush b =new SolidBrush(Color.Red);

                g.DrawString(fontname, f, b, 0, 10);

            }

        }

运行结果如下图所示:

图17-30 使用字体文件

注意:开发商用项目的时候必须注意字体的版权。

 

学习更多vb.net知识,请参看vb.net 教程 目录

学习更多C#知识,请参看C#教程 目录


http://www.kler.cn/a/529331.html

相关文章:

  • Ubuntu 下 nginx-1.24.0 源码分析 - ngx_strerror_init()函数
  • G. XOUR
  • 十分钟快速上手 markdown
  • TensorFlow 示例摄氏度到华氏度的转换(一)
  • solidity高阶 -- Eth支付
  • JavaScript中的数组方法总结+详解
  • ES的机架感知-Rack Awareness
  • kimi,天工,gpt,deepseek效果对比
  • 【Arxiv 大模型最新进展】TOOLGEN:探索Agent工具调用新范式
  • python 从知网的期刊导航页面抓取与农业科技相关的数据
  • 网络测试工具
  • 前端学习-事件委托(三十)
  • 简单易懂的倒排索引详解
  • 仿真设计|基于51单片机的温湿度、一氧化碳、甲醛检测报警系统
  • AI 计算的未来:去中心化浪潮与全球竞争格局重塑
  • 迪杰斯特拉(Dijkstra)算法
  • “新月之智”智能战术头盔系统(CITHS)
  • 抖♬♬__ac_signature 算法逆向分析
  • mybatis辅助配置
  • 计算机组成原理——存储系统(一)
  • 42. PWM背光实验
  • HAL库W25Qxx系列芯片驱动
  • C++STL之stack和queue容器(详细+通俗易懂)
  • 课设:【ID0022】火车票售票管理系统(前端)
  • Qt 5.14.2 学习记录 —— 이십이 QSS
  • 【AI文章解读】《No, DeepSeek Is Not A ‘Sputnik Moment’》