如何在C#中将FullRowSelect为true时更改TreeView中所选节点的背景颜色

时间:2014-03-28 16:00:24

标签: c# treeview

标题几乎总结了一下:我正在寻找一种方法来在FullRowSelect属性打开时更改突出显示的节点的颜色。我尝试了以下代码:

using System;
using System.Drawing;
using System.Windows.Forms;

class ExtendedTreeView : TreeView
{
public ExtendedTreeView()
{
    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.FromArgb(254, 128, 62)), e.Bounds);
        ControlPaint.DrawFocusRectangle(e.Graphics, e.Bounds, fore, Color.FromArgb(254, 128, 62));
        TextRenderer.DrawText(e.Graphics, e.Node.Text, font, e.Bounds, fore, TextFormatFlags.GlyphOverhangPadding);
    }
    else
    {
        e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(64,64,64)), e.Bounds);
        TextRenderer.DrawText(e.Graphics, e.Node.Text, font, e.Bounds, fore, TextFormatFlags.GlyphOverhangPadding);
    }
}
}

不幸的是,当FullRowSelect属性关闭时,这只会更改选区的背景颜色。因此,只要项目的文本是,我就会用自己的颜色突出显示该项目,但该行的其余部分仍然是蓝色而不是更改为我自己的颜色。

提前感谢您的帮助!

0 个答案:

没有答案