Winform中Treeview控件失去焦点,如何将选择的节点还设置为高亮显示?

2024-11-15 21:59:27
推荐回答(1个)
回答1:

这个你可以重绘节点的背景 代码如下 private void Form1_Load(object sender, EventArgs e)
{
treeView1.HideSelection = false;
treeView1.DrawMode = TreeViewDrawMode.OwnerDrawText;
}
private void treeView1_DrawNode(object sender, DrawTreeNodeEventArgs e)
{
e.Graphics.FillRectangle(Brushes.White, e.Node.Bounds);
if (e.State == TreeNodeStates.Selected)//选中的失去焦点的节点
{
e.Graphics.FillRectangle(Brushes.Black, new Rectangle(e.Node.Bounds.Left - 1, e.Node.Bounds.Top, e.Node.Bounds.Width - 2, e.Node.Bounds.Height)); //黑色背景 e.Graphics.DrawString(e.Node.Text, treeView1.Font, Brushes.White, e.Bounds);//白字 }
else
{
e.DrawDefault = true;
}
}