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