繁体   English   中英

如何使ListViewItem文本的一部分变为粗体?

[英]How to make part of ListViewItem text to be bold?

我在“详细信息”模式下有一个ListView,我想将其某些项目的文本加粗(突出显示在项目文本中找到子字符串的位置)。

所以像这样:

Somefilename / boldText / etc

有什么办法吗?

好的,这将很困难,因为您必须首先通过将该属性设置为trueOwnerDraw ListView 现在,您已经需要实现DrawColumnHeader并将此行放入其中:

e.DrawDefault = true;

接下来,您需要根据DrawSubItem在的区域在DrawItemDrawSubItem实现以下代码。 请记住,我提供给您的代码并不完整,因为它没有解析字符串或任何东西,因此您现在仍然需要实现绘制所选项目的功能,因为您可以自己绘制文本。

var boldFont = new Font(this.Font, FontStyle.Bold);
var location = new PointF(e.Bounds.Location.X, e.Bounds.Location.Y);

e.Graphics.DrawString("Somefilename/", this.Font, Brushes.Black, location);
var size = e.Graphics.MeasureString("Somefilename/", this.Font);

location.X += size.Width;
e.Graphics.DrawString("boldText", boldFont, Brushes.Black, location);
size = e.Graphics.MeasureString("boldText", boldFont);

location.X += size.Width;
e.Graphics.DrawString("/etc", this.Font, Brushes.Black, location);

还要注意的另一件事是,您必须稍微处理一下偏移量,因为某些字体有边距,而加粗的字体会占用更多空间。

如果Item不是SubItem则只需实现e.DrawDefault = true; 对于DrawSubItem事件-反之亦然,如果该项是SubItem ,则为DrawItem实现该行。

您必须重写OnPaint。 试试这些链接:

将标签文本的一部分设置为粗体

在Winforms中自定义ListView?

但是,.net框架不会绘制listview,仅支持它。 因此,即使您覆盖OnPaint,也可能不会调用它。 因此,您可能还必须重写listview控件。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM