<分区>
我在 ObjectListView 中使用 Graphics.DrawString
.我需要突出显示字符串中的一些单词(将它们的背景设为黄色)。这能做到吗?
在使用 Graphics.DrawString
之前,我可以使用 DefaultRenderer
来突出显示。但现在它不起作用。
我正在使用 Graphics.DrawString
作为 Task List
示例 here
<分区>
我在 ObjectListView 中使用 Graphics.DrawString
.我需要突出显示字符串中的一些单词(将它们的背景设为黄色)。这能做到吗?
在使用 Graphics.DrawString
之前,我可以使用 DefaultRenderer
来突出显示。但现在它不起作用。
我正在使用 Graphics.DrawString
作为 Task List
示例 here
最佳答案
如果 ObjectListView 支持(普通的 ListView 肯定支持)),只需使用 TextRenderer
来绘制文本:
private void listView1_DrawItem(object sender, DrawListViewItemEventArgs e)
{
TextFormatFlags flags = TextFormatFlags.Left | TextFormatFlags.VerticalCenter;
TextRenderer.DrawText(e.Graphics, e.Item.Text, Font, e.Bounds,
Color.Blue, Color.LightSteelBlue, flags);
}
像往常一样,每次调用仅支持一种样式。因此,要突出显示某些单词,您将使用多个调用并使用 TextRenderer.MeasureText
方法来查找下一个位置。除非您可以接受固定字体,否则很难获得完美的像素。
这是一个快速而肮脏的例子:
private void listView1_DrawItem(object sender, DrawListViewItemEventArgs e)
{
TextFormatFlags flags = TextFormatFlags.Left;
int leftPadding = 3;
int x = leftPadding;
var words = e.Item.Text.Split('#');
for (int i = 0; i < words.Count(); i++)
{
Point pt = new Point(x, e.Bounds.Top );
Size sz = TextRenderer.MeasureText(words[i], Font);
if (i % 2 == 0 )
TextRenderer.DrawText(e.Graphics, words[i], Font, pt,
Color.Black, flags);
else
TextRenderer.DrawText(e.Graphics, words[i], Font, pt,
Color.Blue, Color.LightSteelBlue, flags);
x += sz.Width;
}
}
如您所见,缝隙后有一些额外的空间。也许使用 E.Graphics.MeasureString
调用 wih StringFormat.GenericTypographic
会更好,因为它被调整为创建没有松弛的尺寸..:
更新:
这看起来更好,让我们同时使用两个渲染器:
Point pt = new Point(x, e.Bounds.Top );
Size sz1 = TextRenderer.MeasureText(words[i], Font);
SizeF sz2 = e.Graphics.MeasureString(words[i],Font, 9999, StringFormat.GenericTypographic);
if (i % 2 == 0 )
TextRenderer.DrawText(e.Graphics, words[i], Font, pt, Color.Black);
else
TextRenderer.DrawText(e.Graphics, words[i], Font, pt,
Color.Blue, Color.LightSteelBlue);
x += (int )(sz1.Width + sz2.Width) / 2;
要绘制环绕 文本,您需要考虑可用空间并在计算中包括 y 坐标。为此,您可以逐字绘制或使用 RenderText
采用 Rectangle
的重载。这会变得相当乏味!
更新 2
这是另一个显示如何处理换行文本的快速方法:
private void listView1_DrawItem(object sender, DrawListViewItemEventArgs e)
{
int maxW = e.Bounds.Width;
int leftPadding = 3;
int leading = 1;
int x = leftPadding;
int y = e.Bounds.Y;
var chunks = e.Item.Text.Split('#');
SizeF s0 = e.Graphics.MeasureString("_|", Font);
Size s1 = e.Bounds.Size;
for (int i = 0; i < chunks.Count(); i++)
{
Point pt = new Point(x, e.Bounds.Top );
var words = chunks[i].Split(' ');
for (int j = 0; j < words.Count(); j++)
{
Size sz1 = TextRenderer.MeasureText(words[j], Font);
SizeF sz2 = e.Graphics.MeasureString(words[j], Font, 9999,
StringFormat.GenericTypographic);
int w = (int)(sz1.Width + sz2.Width) / 2;
if (x + w > maxW)
{
y += sz1.Height + leading;
x = leftPadding;
}
DrawWords(e.Graphics, words[j], Font, new Point( x, y),
Color.Blue, Color.LightSteelBlue, i % 2 != 1);
x += w;
}
}
}
它使用了一个小函数:
void DrawWords(Graphics g, string text, Font font, Point pt,
Color fCol, Color Bcol, bool highlite )
{
if (highlite)
TextRenderer.DrawText(g, text, font, pt, Color.Black);
else
TextRenderer.DrawText(g, text, font, pt, Color.Blue, Color.LightSteelBlue);
}
关于c# - Graphics.DrawString 突出显示特定单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35155487/