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

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

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

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/36225.html

(0)
编程小号编程小号

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注