Treeview绘制故障

我为每个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); } } } 

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

您可以通过订阅AfterExpand事件来尝试这项工作:

 void treeView1_AfterExpand(object sender, TreeViewEventArgs e) { treeView1.Invalidate(); } 

只要确保使用:“ BeginUpdate() ”和“ EndUpdate() ”每次填充TreeView

EndUpdate() ”< - 是摆脱这种小故障的最重要的事情!

祝好运! 🙂

干杯,J