简体   繁体   English

计算字符串在C#中的显示宽度?

[英]Calculate the display width of a string in C#?

A Java version of this question was just answered, and, well, I don't know how to do this in .net. 刚刚回答了该问题的Java版本,而且,我不知道如何在.net中执行此操作。

So how do you calculate the display width of a string in C# / .net? 那么,如何计算C#/ .net中字符串的显示宽度?

An alternative for Windows Forms is the static TextRenderer.MeasureText method. Windows窗体的替代方法是静态TextRenderer.MeasureText方法。

Although restricted to integer sizes, this (in tandem with TextRenderer.DrawText) renders more accurate and much higher quality ClearType text than the Graphics.MeasureString/DrawString duo. 尽管限于整数大小,但此方法(与TextRenderer.DrawText配合使用)比Graphics.MeasureString / DrawString二重奏呈现更准确,质量更高的ClearType文本。

You've got the same problem in this question as was present in the Java question - not enough information! 您在此问题中遇到的问题与Java问题中存在的问题相同-信息不足! It will differ between WinForms and WPF. WinForms和WPF之间会有所不同。

For WinForms: Graphics.MeasureString 对于WinForms: Graphics.MeasureString

For WPF I'm not sure, but I suspect it will depend on the exact way you're drawing the text... 对于WPF,我不确定,但是我怀疑这将取决于您绘制文本的确切方式...

在WPF中,您将使用FormattedText

Graphics.MeasureString but its a bit crappy, as is explained and improved upon; Graphics.MeasureString但是有点烂,正如已经解释和改进的; here 这里

You would use Graphics.MeasureString. 您将使用Graphics.MeasureString。

http://msdn.microsoft.com/en-us/library/6xe5hazb.aspx http://msdn.microsoft.com/zh-CN/library/6xe5hazb.aspx

Graphics.MeasureString([text to measure],[font being used to measure text]); Graphics.MeasureString([用于测量的文本],[用于测量文本的字体]);

The resulting object will provide the following: 结果对象将提供以下内容:

可用属性

Other overloads of MeasureString also available. 也可以使用MeasureString的其他重载。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM