更改 TreeNode.BackColor 会导致 TreeView 完全重绘

发布于 2024-09-10 21:18:45 字数 289 浏览 5 评论 0原文

我在 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怪我鬧 2024-09-17 21:18:45

看来你无法阻止这种情况的发生。我查看了 TreeNode.BackColor 设置器的代码:

[SRDescription("TreeNodeBackColorDescr"), SRCategory("CatAppearance")]
public Color BackColor
{
    get
    {
        if (this.propBag == null)
        {
            return Color.Empty;
        }
        return this.propBag.BackColor;
    }
    set
    {
        Color backColor = this.BackColor;
        if (value.IsEmpty)
        {
            if (this.propBag != null)
            {
                this.propBag.BackColor = Color.Empty;
                this.RemovePropBagIfEmpty();
            }
            if (!backColor.IsEmpty)
            {
                this.InvalidateHostTree();
            }
        }
        else
        {
            if (this.propBag == null)
            {
                this.propBag = new OwnerDrawPropertyBag();
            }
            this.propBag.BackColor = value;
            if (!value.Equals(backColor))
            {
                this.InvalidateHostTree();
            }
        }
    }
}

每当 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:

[SRDescription("TreeNodeBackColorDescr"), SRCategory("CatAppearance")]
public Color BackColor
{
    get
    {
        if (this.propBag == null)
        {
            return Color.Empty;
        }
        return this.propBag.BackColor;
    }
    set
    {
        Color backColor = this.BackColor;
        if (value.IsEmpty)
        {
            if (this.propBag != null)
            {
                this.propBag.BackColor = Color.Empty;
                this.RemovePropBagIfEmpty();
            }
            if (!backColor.IsEmpty)
            {
                this.InvalidateHostTree();
            }
        }
        else
        {
            if (this.propBag == null)
            {
                this.propBag = new OwnerDrawPropertyBag();
            }
            this.propBag.BackColor = value;
            if (!value.Equals(backColor))
            {
                this.InvalidateHostTree();
            }
        }
    }
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文