使用
Windows Forms,当我单击TreeView节点时,突出显示颜色为蓝色.有没有办法改变这个?
解决方法
如果您创建自己的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,Color.Red,TextFormatFlags.GlyphOverhangPadding); } else { e.Graphics.FillRectangle(SystemBrushes.Window,e.Bounds); TextRenderer.DrawText(e.Graphics,TextFormatFlags.GlyphOverhangPadding); } } }