我为我的每个 TreeView
节点实现了一个多色系统。但是每次我扩展一个子节点时,它都会扩展但也会在我的 rootNode 上绘制节点(图像 2 和 3)。代码来自my previous question这就是错误的样子
如果我决定关闭每个节点并重新展开,故障就会消失。(图 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();
}
关于c# - TreeView 绘制故障,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13825167/