Treeview绘制故障

pha*_*unk 7 c# treeview colors winforms

我为每个TreeView节点实现了一个多色系统.但每次我展开一个子节点时,它都会消耗,但也会在我的rootNode上绘制节点(图像2和3).代码来自我之前的问题,这就是bug的样子

在此输入图像描述

如果我决定关闭每个节点并重新扩展毛刺消失了.(图4)

问题似乎与Bounds这就是为什么平局不在正确的地方.知道为什么吗?


  private void treeView1_DrawNode(object sender, DrawTreeNodeEventArgs e)
  {
    string[] texts = e.Node.Text.Split();
    using (Font font = new Font(this.Font, FontStyle.Regular))
    {
        using (Brush brush = new SolidBrush(Color.Red))
        {
            e.Graphics.DrawString(texts[0], font, brush, e.Bounds.Left, e.Bounds.Top);
        }

        using (Brush brush = new SolidBrush(Color.Blue))
        {
            SizeF s = e.Graphics.MeasureString(texts[0], font);
            e.Graphics.DrawString(texts[1], font, brush, e.Bounds.Left + (int)s.Width, e.Bounds.Top);
        }
    }
  }
Run Code Online (Sandbox Code Playgroud)

Lar*_*ech 5

绘图故障似乎是一个准确的描述。

您可以通过订阅AfterExpand事件来尝试解决此问题:

void treeView1_AfterExpand(object sender, TreeViewEventArgs e) {
  treeView1.Invalidate();
}
Run Code Online (Sandbox Code Playgroud)