C#中的TreeView节点高亮颜色 [英] TreeView node highlight colour in C#
本文介绍了C#中的TreeView节点高亮颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用 Windows 窗体,当我单击 TreeView 节点时,突出显示颜色为蓝色.有没有办法改变这种情况?
Using Windows Forms, when I click on a TreeView node, the highlight colour is blue. Is there a way to change this?
推荐答案
如果您创建自己的 TreeView 类并覆盖 OnDrawNode 方法,这是可能的.例如,这将用红色突出显示选定的节点:
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);
}
}
}
这篇关于C#中的TreeView节点高亮颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文