我想在面板上绘制以下文字:
这是一个多色文本。
我找到了这篇关于绘制彩色文本的文章。
我用单词替换了字符,但不起作用。
(我使用FillPath/DrawPath来绘制文本)
我的代码:
private void Form1_Paint(object sender, PaintEventArgs e)
{
const string txt = "C# Helper! Draw some text with each letter in a random color.";
// Make the font.
using (Font the_font = new Font("Times New Roman", 40,
FontStyle.Bold | FontStyle.Italic))
{
// Make a StringFormat object to use for text layout.
using (StringFormat string_format = new StringFormat())
{
// Center the text.
string_format.Alignment = StringAlignment.Center;
string_format.LineAlignment = StringAlignment.Center;
string_format.FormatFlags = StringFormatFlags.NoClip;
// Make CharacterRanges to indicate which
// ranges we want to measure.
MatchCollection mc = Regex.Matches(txt, @"[^\s]+");
CharacterRange[] ranges = new CharacterRange[mc.Count];
int g = 0;
foreach (Match m in mc)
{
ranges[g] = new CharacterRange(m.Index, m.Length);
g++;
}
string_format.SetMeasurableCharacterRanges(ranges);
// Measure the text to see where each character range goes.
Region[] regions =
e.Graphics.MeasureCharacterRanges(
txt, the_font, this.ClientRectangle,
string_format);
// Draw the characters one at a time.
for (int i = 0; i < ranges.Length; i++)
{
// See where this character would be drawn.
RectangleF rectf = regions[i].GetBounds(e.Graphics);
Rectangle rect = new Rectangle(
(int)rectf.X, (int)rectf.Y,
(int)rectf.Width, (int)rectf.Height);
// Make a brush with a random color.
using (Brush the_brush = new SolidBrush(RandomColor()))
{
// Draw the character.
string txts = txt.Substring(ranges[i].First, ranges[i].Length);
e.Graphics.DrawString(txts,
the_font, the_brush, rectf, string_format);
}
}
}
}
}
有什么问题吗?
这(在某种程度上)是经典。
MeasureCharacterRanges 执行的相当精确的测量与
Graphics.DrawString
执行的实际字符串绘制之间存在差异。
RectagleF
返回的 Region.GetBounds()
按原样考虑文本的度量。Graphics.DrawString
在计算给定边界内的文本配置时执行某种网格拟合。
我不会在这里解释,这是一个相当广泛的问题,但我已经写了一些相关内容:
在位图上绘制长字符串会导致绘图问题
如果您有兴趣,可以在此上下文中找到有关
Graphics
对象行为的一些详细信息。
总而言之,文本测量正确,但是
Graphics.DrawString
执行的调整导致文本不完全适合测量的边界:绘制的文本稍微溢出。
您可以使用几个 StringFormat
标志来纠正此
问题:
[StringFormat].Trimming = StringTrimming.None
应用此设置后,您可以立即看到问题所在:最后一个字符(或几个字符)被换行到新行,弄乱了绘图。
StringFormatFlags.NoWrap
添加到 StringFormatFlags.NoClip
。我向您建议另一种方法,使用 TextRenderer.DrawText 来渲染字符串。
请注意,
TextRenderer
实际上是WinForms
控件(好吧,不是全部)使用的类,用于将文本渲染到屏幕上。
这是使用以下方法的结果:
示例代码,使用原始代码并进行一些修改:
TextFormatFlags flags = TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter |
TextFormatFlags.NoPadding | TextFormatFlags.NoClipping;
const string txt = "C# Helper! Draw some text with each word in a random color.";
private void panel1_Paint(object sender, PaintEventArgs e) {
Control control = sender as Control;
using (var format = new StringFormat())
using (var font = new Font("Times New Roman", 40, FontStyle.Regular, GraphicsUnit.Point)) {
format.Alignment = StringAlignment.Center;
format.LineAlignment = StringAlignment.Center;
MatchCollection mc = Regex.Matches(txt, @"[^\s]+");
CharacterRange[] ranges = mc.Cast<Match>().Select(m => new CharacterRange(m.Index, m.Length)).ToArray();
format.SetMeasurableCharacterRanges(ranges);
Region[] regions = e.Graphics.MeasureCharacterRanges(txt, font, control.ClientRectangle, format);
for (int i = 0; i < ranges.Length; i++) {
Rectangle WordBounds = Rectangle.Round(regions[i].GetBounds(e.Graphics));
string word = txt.Substring(ranges[i].First, ranges[i].Length);
TextRenderer.DrawText(e.Graphics, word, font, WordBounds, RandomColor(), flags);
}
}
}
private Random rand = new Random();
private Color[] colors =
{
Color.Red,
Color.Green,
Color.Blue,
Color.Lime,
Color.Orange,
Color.Fuchsia,
};
private Color RandomColor()
{
return colors[rand.Next(0, colors.Length)];
}