1

我正在开发自定义 C# TreeView,我想做一些自定义绘制来突出显示节点名称中的关键字。

我做了:

DrawMode = TreeViewDrawMode.OwnerDrawText;

在自定义 TreeView 的构造函数中并覆盖 OnDrawNode:

protected override void OnDrawNode(DrawTreeNodeEventArgs e)
    {
        if (!e.Node.IsVisible) { return; }
        if (e.Node.Bounds.IsEmpty) { return; }

        e.DrawDefault = false;

        ...draw calls...

但是在我这样编码之后它的工作很奇怪,感知到的行为包括:

  1. OnDrawNode 正在调用未展开且不可见的子节点
  2. 当 TreeView 的内容更新时,用户会同时看到旧内容和新内容相互重叠。旧内容直到大约半秒或更长时间才会消失。
  3. 渲染速度比原始绘图调用慢得多。

我所做的另一个修改是我在此处找到的用于抑制 TreeView 更新时发生闪烁的代码片段:http: //dev.nomad-net.info/articles/double-buffered-tree-and-list-views 但似乎与问题没有直接关系,因为删除后我仍然可以看到文本重叠。

我想知道是否有人对这个问题有任何想法?

任何想法将不胜感激。谢谢你。

编辑:

OnDrawNode 的内容如下:

string pattern = keyword;
if (!string.IsNullOrWhiteSpace(pattern))
{
    Regex regularExpressionnew = Regex(pattern);
    Match match = regularExpression.Match(e.Node.Text);
    while (match.Success)
    {
        CaptureCollection captureCollection = match.Groups[0].Captures;
        foreach (Capture capture in captureCollection)
        {
            int highlightStartIndex = capture.Index;
            int highlightEndIndex = capture.Index + pattern.Length;
            e.Graphics.FillRectangle(nodeHightLightColor, GetTextBoundsBetweenIndex(e.Graphics, e.Node.Text, highlightStartIndex, highlightEndIndex, e.Bounds));
        }
        match = match.NextMatch();
    }

Brush drawBrush = new SolidBrush(Color.Black);
e.Graphics.DrawString(e.Node.Text, Font, drawBrush, e.Bounds);

GetTextBoundsBetweenIndex 本质上是计算覆盖 highlightStartIndex 和 highlightEndIndex 之间的字符的正方形区域。

但是如果正则表达式被注释掉并且只剩下文本渲染,就会发生滞后和重叠。

4

0 回答 0