我需要在类库中构建一个函数,该函数接受一个字符串和该字符串的特定字体,然后获取字符串的宽度
那么我怎样才能得到字符串边界宽度呢?
另一种方法是使用TextRenderer
,并致电its MeasureString http://msdn.microsoft.com/en-us/library/y4xdbe66.aspx方法,传递字符串和字体类型。
MSDN 示例:
private void MeasureText1(PaintEventArgs e)
{
String text1 = "Measure this text";
Font arialBold = new Font("Arial", 12.0F);
Size textSize = TextRenderer.MeasureText(text1, arialBold);
TextRenderer.DrawText(e.Graphics, text1, arialBold,
new Rectangle(new Point(10, 10), textSize), Color.Red);
}
NOTE:这只是 @Neil Barnwell 已经发布的(同样有效)解决方案的替代解决方案
(如果您的项目中已经引用了 System.Windows.Forms,这可能会更方便)。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)