TreeView node highlight colour in C#
Using Windows Forms, when I click on a TreeView node, the highlight colour is blue. Is there a way to change this?开发者_JS百科
It is possible if you create your own TreeView class and override the OnDrawNode method. For example, this one will highlight selected node with red color:
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);
}
}
}
精彩评论