[英]How to remove default blue background on treeview nodes c# winform
I have a treeview but when I click on a node it displays a blue background.我有一个 treeview 但是当我点击一个节点时它显示蓝色背景。 Do you have a solution to remove this blue background?
你有办法去除这个蓝色背景吗? I am using winform c# Thanks
我正在使用 winform c# 谢谢
To eliminate the blue background color of a node that is selected by mouse click, set the DrawMode
property of the tree view to OwnerDrawText
and handle the node label drawing as shown:要消除通过鼠标单击选中的节点的蓝色背景色,请将树视图的
DrawMode
属性设置为OwnerDrawText
并处理节点 label 绘图,如下所示:
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
treeView.DrawMode = TreeViewDrawMode.OwnerDrawText;
treeView.DrawNode += customDrawNode;
treeView.AfterSelect += setTitleBarText;
}
If the node is selected, custom draw the label. Non-selected nodes draw default.如果选中节点,则自定义绘制label。未选中节点默认绘制。
private void customDrawNode(object? sender, DrawTreeNodeEventArgs e)
{
if(sender is TreeView treeView)
{
if ((e.Node != null) && e.State.HasFlag(TreeNodeStates.Selected))
{
using (var brush = new SolidBrush(treeView.BackColor))
{
e.Graphics.FillRectangle(brush, e.Bounds);
}
using (var brush = new SolidBrush(treeView.ForeColor))
{
e.Graphics.DrawString(e.Node.Text, treeView.Font, brush, e.Bounds);
}
return;
}
}
e.DrawDefault = true;
}
Verify selected node by setting main form text.通过设置主窗体文本来验证所选节点。
private void setTitleBarText(object? sender, TreeViewEventArgs e)
{
Text =
treeView.SelectedNode == null ?
"Main Form" :
$"Main Form: {treeView.SelectedNode.Text}";
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.