You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WinForms TreeView加载时节点文本异常变粗问题的排查与解决咨询

WinForms TreeView加载时节点文本异常变粗问题的排查与解决咨询

各位开发者好,我在开发WinForms应用时碰到了一个诡异的问题:TreeView控件在加载节点的过程中,节点文本会莫名显示成粗体,但等整个TreeView完全加载完成后,文本又会恢复正常样式。

先给大家说下我的技术实现背景:我给TreeView设置了自定义绘制模式,代码如下:

trvScheduling.DrawMode = TreeViewDrawMode.OwnerDrawText;

同时我重写了TreeView的OnDrawNode事件来实现自定义绘制逻辑,具体代码如下:

protected override void OnDrawNode(DrawTreeNodeEventArgs e)
{
    if (e.Node == null) return;
    if (e.Node.IsVisible == false) return;

    var selected = (e.State & TreeNodeStates.Selected) == TreeNodeStates.Selected;
    //var unfocused = !e.Node.TreeView.Focused;

    if (e.Node.TreeView.CheckBoxes == false)
    {
        // we need to do owner drawing only on a selected node
        // and when the treeview is unfocused, else let the OS do it for us
        if (selected)
        {
            var font = e.Node.NodeFont ?? e.Node.TreeView.Font;
            Brush solidBrush = new SolidBrush(WinFormsUITheme.styleColor);
            e.Graphics.FillRectangle(solidBrush, e.Bounds);
            TextRenderer.DrawText(e.Graphics, e.Node.Text, font, e.Bounds, Color.White, TextFormatFlags.GlyphOverhangPadding);
        }
        else
        {
            e.DrawDefault = true;
        }
    }
    else
    {
        //if (e.Node == null) return;
        if ((e.State & TreeNodeStates.Selected) != 0)
        {
            var font = e.Node.NodeFont ?? e.Node.TreeView.Font;
            Brush solidBrush = new SolidBrush(Color.White);
            e.Graphics.FillRectangle(solidBrush, new Rectangle(e.Bounds.Left, e.Bounds.Top, e.Bounds.Right, 16));
        }
        //Use the default background and node text.
        else
        {
            //e.DrawDefault = true;
        }

        // If a node tag is present, draw its string representation
        // to the right of the label text.
        if (e.Node.ToolTipText != null)
        {
            //e.Node.Text
            int color;
            bool isValidInt = int.TryParse(e.Node.ToolTipText.ToString(), out color);
            if (isValidInt)
            {
                Brush colorBrush = new SolidBrush(Color.FromArgb(color));
                //e.Graphics.FillRectangle(colorBrush, new Rectangle(e.Bounds.Right + 2, e.Bounds.Top, 10, 10));
                e.Graphics.FillRectangle(colorBrush, new Rectangle(e.Bounds.Left + 2, e.Bounds.Top + 2, 10, 10));
                e.Graphics.DrawString(e.Node.Text.ToString(), tagFont,
                Brushes.Black, e.Bounds.Left + 12, e.Bounds.Top);
            }
            else
            {
                e.DrawDefault = true;
            }
        }
    }
}

我怀疑是OnDrawNode事件里的绘制逻辑出了问题,才导致加载时文本变粗的现象。我试过在调用e.Graphics.DrawString(..)前设置e.Graphics.TextRenderingHint = TextRenderingHint.AntiAlias,但这个操作并没有解决问题。

后来我给OnDrawNode加了调试断点,发现一个奇怪的现象:TreeView会对每个节点的文本绘制三次——第一次绘制时文本显示正常,但第二次绘制时因为重复渲染,文本就变成粗体了。

有没有大佬知道这是什么原因导致的?或者有没有办法在OnDrawNode事件里避免这种重复渲染的情况,从而解决加载时文本变粗的问题?

备注:内容来源于stack exchange,提问作者r-m85

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.20 07:54:35