如何查找字符串在C#中的宽度(以像素为单位) [英] How to find string's width in C# (in pixels)
本文介绍了如何查找字符串在C#中的宽度(以像素为单位)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想实现成帧文本(使用Windows窗体),例如:
I'm trying to achieve framed texts (using Windows Forms), e.g.:
身高始终是相同的,因为我的字符串是少于20个字符。怎么样的宽度?有什么办法可以自动得到它?
Height is always the same, because my strings are less than 20 chars. What about width? Is there any way to get it automatically?
推荐答案
使用 Graphics.MeasureString()
从MSDN: HTTP:/ /msdn.microsoft.com/en-us/library/6xe5hazb.aspx
private void MeasureStringMin(PaintEventArgs e)
{
// Set up string.
string measureString = "Measure String";
Font stringFont = new Font("Arial", 16);
// Measure string.
SizeF stringSize = new SizeF();
stringSize = e.Graphics.MeasureString(measureString, stringFont);
// 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, stringFont, Brushes.Black, new PointF(0, 0));
}
这篇关于如何查找字符串在C#中的宽度(以像素为单位)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文