更改 TreeNode.BackColor 会导致 TreeView 完全重绘
我在 TreeView 中设置了特定 TreeNode 的 BackColor,以提示用户在使用应用程序时该节点发生了一些有趣的事情。但是,当我设置 BackColor 时,它会导致整个父 TreeView 控件重新绘制,而不仅仅是已更改的特定 TreeNode 的标签区域。我在任何时候都不会调用“刷新”或“更新”——只是在 TreeNode 上设置 BackColor。看起来,TreeView 不仅刷新了已更改的 TreeNode 的边界,还刷新了其整个区域。这会导致控件出现烦人的快速闪烁。
知道为什么会发生这种情况以及是否可以轻松阻止吗?
I set the BackColor of specific TreeNodes in a TreeView as a hint to the user that something interesting has happened to the node while they are using the application. However, when I set BackColor, it causes the entire parent TreeView control to redraw rather than just the label area of the specific TreeNode that has been changed. I am not calling Refresh or Update at any point -- just setting BackColor on the TreeNode. It seems that rather than just invalidating the bounds of the TreeNode that has been changed, the TreeView is refreshing its entire area. This results in an annoying quick flash of the control.
Any idea why this happening and if it can be easily stopped?
看来你无法阻止这种情况的发生。我查看了 TreeNode.BackColor 设置器的代码:
每当 BackColor 更改时,就会在包含该节点的树上强制执行无效操作。再次查看 InvalidateHostTree 函数,您无法设置任何标志来阻止刷新发生。
It doesn't look like you can stop this from occuring. I took a look at the code for the TreeNode.BackColor setter:
Whenever the BackColor changes, an invalidate is forced on the tree that contains the node. Again, looking at the InvalidateHostTree function, there are no flags you can set to stop the refresh from occurring.