C# 字符串测量

标签 c# winforms string units-of-measurement

我想将字符串的长度测量为像素单位。我在网上搜索了两天但没有成功。最后,我几分钟前从博客中找到了一些代码片段,并对其进行了一些修改。这是我的功能:

private void cmdMeasure_Click(object sender, EventArgs e)
{
    Font fntStyle = new Font("Arial", 16, FontStyle.Regular, GraphicsUnit.Pixel);
    Size textSize = TextRenderer.MeasureText(str2measure.Text, fntStyle);
    MessageBox.Show(textSize.ToString());
}

问题:

16的单位是什么?是em、pt还是GraphicsUnit enum的单位?我并没有真正得到 c# 定义的描述“单位参数指定的单位中的新字体的 em 大小。”

TextRenderer.MeasureText 方法在其测量中是否包含字符之间的空格?

最佳答案

What is the unit of 16? Is it em, pt, or the unit of GraphicsUnit enum? I don't really get the description defined by c# "the em-size of the new font in the units specified by the unit parameter."

正如其他人所说,自从您传递 GraphicsUnit.Pixel 以来,它就是像素。

Does the TextRenderer.MeasureText method include the spaces between the characters in its measurement?

是的。它为您提供整个文本 block 周围的边界框的尺寸。

关于C# 字符串测量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10202476/

相关文章:

c# - 无法在仅反射上下文中获取 MethodInfo 实例

c# - Mono.Addin 实现不检索插件

c# - Winform拖放到excel

C# 缩短重复代码的更好方法

excel - 使用IF语句检查值

c# - AXWindowsMediaPlayer找不到嵌入式资源MP3

C# - 如何在列表框(Windows 窗体)内上下移动多个项目

c# - 在 c# winform 中更改的文本上启用禁用按钮

Java:以数字方式对字符串数组进行排序

java - 使用逗号分隔值构建字符串