热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

winform自定义控件(gdi+)——测量文本大小(MeasureString)

1、MeasureString(String,Font)代码:stringmeasureStringMeasureString;FontstringFont1ne

1、MeasureString(String, Font)
代码:

   string measureString = "Measure String";

            Font stringFont1 = new Font("Arial", 60);
            SizeF layoutSize = new SizeF(200.0F, 50.0F);

            // Measure string.
            SizeF stringSize = new SizeF();
            stringSize = e.Graphics.MeasureString(measureString, stringFont1);
            // Draw rectangle representing size of string.
            e.Graphics.DrawRectangle(new Pen(Color.Red, 1), 0.0F, 0.0F, stringSize.Width, stringSize.Height);
            // Draw string to screen.
            e.Graphics.DrawString(measureString, stringFont1, Brushes.Black, new PointF(0, 0));

输出:

说明:
测量的大小随着字体的增大而增大。

2、MeasureString(String, Font, SizeF)
代码:

  // Set up string.
            string measureString = "Measure String";

            Font stringFont1 = new Font("Arial", 6);
            SizeF layoutSize = new SizeF(200.0F, 50.0F);

            // Measure string.
            SizeF stringSize = new SizeF();
            stringSize = e.Graphics.MeasureString(measureString, stringFont1, layoutSize);
            // Draw rectangle representing size of string.
            e.Graphics.DrawRectangle(new Pen(Color.Red, 1), 0.0F, 0.0F, stringSize.Width, stringSize.Height);
            // Draw string to screen.
            e.Graphics.DrawString(measureString, stringFont1, Brushes.Black, new PointF(0, 0));


            Font stringFont2 = new Font("Arial", 16);
            stringSize = e.Graphics.MeasureString(measureString, stringFont2, layoutSize);
            // Draw rectangle representing size of string.
            e.Graphics.DrawRectangle(new Pen(Color.Red, 1), 0, 100F, stringSize.Width, stringSize.Height);
            // Draw string to screen.
            e.Graphics.DrawString(measureString, stringFont2, Brushes.Black, new PointF(0, 100));

            Font stringFont3 = new Font("Arial", 56);
            // Set maximum layout size.
            stringSize = e.Graphics.MeasureString(measureString, stringFont3, layoutSize);
            // Draw rectangle representing size of string.
            e.Graphics.DrawRectangle(new Pen(Color.Red, 1), 0.0F, 300F, stringSize.Width, stringSize.Height);
            // Draw string to screen.
            e.Graphics.DrawString(measureString, stringFont3, Brushes.Black, new PointF(0, 30


输出:

说明:
最后一个size指定了文本的最大区域,当字体超出最大区域的时候,矩形就不能包含字体了。

3、MeasureString(String, Font, Int32)
最后一个参数限制宽度,功能和MeasureString(String, Font, SizeF)相同。

4、MeasureString(String, Font, PointF, StringFormat)
代码:

 // Set up string.
            string measureString = "Measure String";
            Font stringFont = new Font("Arial", 16);

            // Set point for upper-left corner of string.
            float x = 50.0F;
            float y = 50.0F;
            PointF ulCorner = new PointF(x, y);

            // Set string format.
            StringFormat newStringFormat = new StringFormat();
            newStringFormat.FormatFlags = StringFormatFlags.DirectionVertical;

            // Measure string.
            SizeF stringSize = new SizeF();
            stringSize = e.Graphics.MeasureString(measureString, stringFont, ulCorner, newStringFormat);

            // Draw rectangle representing size of string.
            e.Graphics.DrawRectangle(new Pen(Color.Red, 1), x, y, stringSize.Width, stringSize.Height);

            // Draw string to screen.
            e.Graphics.DrawString(measureString, stringFont, Brushes.Black, ulCorner, newStringFormat);

输出

说明:可以指定文本格式
————————————————
版权声明:本文为CSDN博主「上位机软件」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_34059233/article/details/119911808


推荐阅读
author-avatar
老鼠扛着刀找猫_592
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有