如何确定一个字符串的大小给定的字体 [英] How to determine the size of a string given a font
问题描述
我有一个小的形式,显示了一定的进展信息。
非常罕见的我一定要表现出相当长的消息,我希望能够调整必要的,这样这个消息的形式适合当这种形式。
I have a small form that displays some progress information.
Very rarely I have to show a rather long message and I want to be able to resize this form when needed so that this message fits in the form.
那么,如何才能知道有多宽字符串取值
将在字体呈现的 F
?
So how do I find out how wide string S
will be rendered in font F
?
推荐答案
这取决于所使用的渲染引擎。您可以GDI和GDI +的基本切换。切换可以通过设置 <一做href="http://msdn.microsoft.com/en-us/library/microsoft.visualbasic.applicationservices.windowsformsapplicationbase.usecompatibletextrendering.aspx">UseCompatibleTextRendering$c$c>属性进行相应的
It depends on the rendering engine being used. You can basically switch between GDI and GDI+. Switching can be done by setting the UseCompatibleTextRendering
property accordingly
在使用GDI +你应该使用 MeasureString
:
When using GDI+ you should use MeasureString
:
string s = "A sample string";
SizeF size = e.Graphics.MeasureString(s, new Font("Arial", 24));
在使用GDI(即本机Win32渲染),你应该使用 TextRenderer
类:
When using GDI (i.e. the native Win32 rendering) you should use the TextRenderer
class:
SizeF size = TextRenderer.MeasureText(s, new Font("Arial", 24));
请参阅这篇文章:文本渲染:构建全球通用的应用程序使用复杂的脚本,在Windows窗体控件一>
这篇关于如何确定一个字符串的大小给定的字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!