获取文字长度

wpf

//获取文字长度
private double MeasureTextWidth(string text, double fontSize, string fontFamily)
{
FormattedText formattedText = new FormattedText(
text,
System.Globalization.CultureInfo.InvariantCulture,
FlowDirection.LeftToRight,
new Typeface(fontFamily.ToString()),
fontSize,
Brushes.Black
);
return formattedText.WidthIncludingTrailingWhitespace;
}

 

winform

    System.Drawing.Size fontSize = System.Windows.Forms.TextRenderer.MeasureText(value, new System.Drawing.Font("Arial", (float)btnDisplay.FontSize));

posted @ 2014-08-27 16:36  metoer  阅读(212)  评论(0编辑  收藏  举报