gpt4 book ai didi

c# - 如何使用 TreeViewDrawMode.OwnerDrawAll 更改 TreeView 中选定节点的背景颜色?

转载 作者:太空狗 更新时间:2023-10-29 23:24:45 26 4
gpt4 key购买 nike

我需要更改所选节点的背景颜色,当节点被选中并具有焦点时 - 背景颜色变为绿色,当被选中但没有焦点时 - 红色。我无法区分关注 TreeView 和不关注 TreeView 的选定节点。位于 TabPage 对象中的 TreeView 。

//...
this.myTreeView.HideSelection = false;
//...
private void myTreeView_drawNode(object sender, DrawTreeNodeEventArgs e)
{
Color backColorSelected = System.Drawing.Color.Green;
Color backColor = System.Drawing.Color.Red;
// node selected and has focus
if (((e.State & TreeNodeStates.Selected) != 0)
&& (this.myTabControl.Focused)) // this doesn't work, node is always red
{
using (SolidBrush brush = new SolidBrush(backColorSelected))
{
e.Graphics.FillRectangle(brush, e.Bounds);
}
}
// node selected but doesn't have focus
else if ((e.State & TreeNodeStates.Selected) != 0)
{
using (SolidBrush brush = new SolidBrush(backColor))
{
e.Graphics.FillRectangle(brush, e.Bounds);
}
}
// not selected at all
else
{
e.Graphics.FillRectangle(Brushes.White, e.Bounds);
}

e.Graphics.DrawRectangle(SystemPens.Control, e.Bounds);

TextRenderer.DrawText(e.Graphics,
e.Node.Text,
e.Node.TreeView.Font,
e.Node.Bounds,
e.Node.ForeColor);
}

最佳答案

只需检查节点的属性,它就可以工作(已测试)。此外,我建议缓存您制作的任何自定义画笔,如下所示。(当然,您也可以使用 Brushes.Red 和 Brushes.Green)

SolidBrush greenBrush = new SolidBrush(Color.Green);
SolidBrush redBrush = new SolidBrush(Color.Red);

private void myTreeView_drawNode(object sender, DrawTreeNodeEventArgs e)
{
if (e.Node.IsSelected)
{
if (treeView1.Focused)
e.Graphics.FillRectangle(greenBrush, e.Bounds);
else
e.Graphics.FillRectangle(redBrush, e.Bounds);
}
else
e.Graphics.FillRectangle(Brushes.White, e.Bounds);

e.Graphics.DrawRectangle(SystemPens.Control, e.Bounds);

TextRenderer.DrawText(e.Graphics,
e.Node.Text,
e.Node.TreeView.Font,
e.Node.Bounds,
e.Node.ForeColor);
}

附言您可能需要渲染您单击以展开节点等的内容。

关于c# - 如何使用 TreeViewDrawMode.OwnerDrawAll 更改 TreeView 中选定节点的背景颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12886402/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com