TreeView节点在C#中突出显示颜色

时间:2010-11-18 10:25:47

标签: c# winforms treeview

使用Windows Forms,当我单击TreeView节点时,突出显示颜色为蓝色。有没有办法改变这个?

1 个答案:

答案 0 :(得分:7)

如果您创建自己的TreeView类并覆盖OnDrawNode方法,则可以。例如,这个将突出显示选定的红色节点:

class ClassMyTreeView:TreeView
{
    public ClassMyTreeView()
    {
        this.DrawMode = TreeViewDrawMode.OwnerDrawText;
    }

    protected override void OnDrawNode(DrawTreeNodeEventArgs e)
    {
        TreeNodeStates state = e.State;
        Font font = e.Node.NodeFont ?? e.Node.TreeView.Font;
        Color fore = e.Node.ForeColor;
        if (fore == Color.Empty) 
            fore = e.Node.TreeView.ForeColor;
        if (e.Node == e.Node.TreeView.SelectedNode)
        {
            fore = SystemColors.HighlightText;
            e.Graphics.FillRectangle(new SolidBrush(Color.Red), e.Bounds);
            ControlPaint.DrawFocusRectangle(e.Graphics, e.Bounds, fore, Color.Red);
            TextRenderer.DrawText(e.Graphics, e.Node.Text, font, e.Bounds, fore, Color.Red, TextFormatFlags.GlyphOverhangPadding);
        }
        else
        {
            e.Graphics.FillRectangle(SystemBrushes.Window, e.Bounds);
            TextRenderer.DrawText(e.Graphics, e.Node.Text, font, e.Bounds, fore, TextFormatFlags.GlyphOverhangPadding);
        }
    }
}