首先,要正确解决这是一个相当棘手的问题 - 您最好尝试找到可以为您解决此问题的现有控件(例如 RichTextBox)。
也就是说,如果您确实想这样做,那么这或多或少是解决此问题的正确方法,但是如果您查看MeasureString documentation,您会注意到您看到的行为是故意的
MeasureString 方法设计用于单个字符串
并在字符串前后包含少量额外空格
允许悬垂的字形。此外,DrawString 方法调整
字形点以优化显示质量并可能显示字符串
比 MeasureString 报告的要窄。获取合适的指标
对于布局中的相邻字符串(例如,在实现
格式化文本),使用MeasureCharacterRanges 方法或其中一种
MeasureString 方法采用StringFormat,并通过
GenericTypographic。此外,请确保 TextRenderingHint 用于
Graphics 是 AntiAlias。
所以在我看来,您应该改用 Graphics.MeasureCharacterRanges Method。
这是我准备的一个示例,交易以两种不同的颜色呈现一些文本。要试用,只需将其粘贴到新表单中
protected override void OnPaint(PaintEventArgs e)
{
// This is where we wish to print our string
var region = new RectangleF(50, 50, 200, 50);
// This is the font we wish to use
var font = new Font("Times New Roman", 16.0F);
// Draw a string for comparison
DrawString(e.Graphics, "RedBlack", font, Brushes.Black, new RectangleF(50, 150, 200, 50));
// Draw the first string and keep a track of the Region it was rendered in
var first = DrawString(e.Graphics, "Red", font, Brushes.Red, region);
// Adjust the region we wish to print
region = new RectangleF(region.X + first.GetBounds(e.Graphics).Width, region.Y, region.Width, region.Height);
// Draw the second string
DrawString(e.Graphics, "Black", font, Brushes.Black, region);
base.OnPaint(e);
}
private Region DrawString(Graphics g, string s, Font font, Brush brush, RectangleF layoutRectangle)
{
var format = new StringFormat();
format.SetMeasurableCharacterRanges(new[] { new CharacterRange(0, s.Length) });
g.DrawString(s, font, brush, layoutRectangle, format);
return g.MeasureCharacterRanges(s, font, layoutRectangle, format)[0];
}
这就是它的样子
请注意,您需要小心剪辑 - 默认情况下,GDI 会为您将渲染的文本“换行”到新行,但这将不再起作用,您最终会得到类似这样的结果
此外,如果您尝试打印出具有不同字体/字体大小的文本,那么每种字体的“底部”都不会与您期望的对齐。尝试查看 Formatting text on a common baseline 以获取有关如何处理该问题的一些提示。