使用不同的颜色为树视图节点着色

本文关键字:视图 节点 颜色 | 更新日期: 2023-09-27 18:32:50

我有一个带有一些节点的treeview。在某些情况下,我想为每个节点及其子节点添加不同的颜色。我编写了一个为节点及其子节点着色的函数。

有人会告诉我我是否有可能Color.Green为变量,以至于我不会为每种颜色编写整个函数?我的意思是作为函数中的输入参数。

这是函数:

public void ColorChild(TreeNode nodes, int indx)
{
    foreach (TreeNode node_tmp in nodes.Nodes[indx].Nodes)
    {
       System.Drawing.Color = Green;
       node_tmp.ForeColor = color;
       foreach (TreeNode node_tmp2 in node_tmp.Nodes)
       {
          node_tmp2.ForeColor = Color.Green;
          foreach (TreeNode node_tmp3 in node_tmp2.Nodes)
          {
             node_tmp3.ForeColor = Color.Green;
             foreach (TreeNode node_tmp4 in node_tmp3.Nodes)
             {
                node_tmp4.ForeColor = Color.Green;
                foreach (TreeNode node_tmp5 in node_tmp4.Nodes)
                {
                   node_tmp5.ForeColor = Color.Green;
                }
             }
          }
      }
   }
}

使用不同的颜色为树视图节点着色

您可以使用递归遍历所有节点。

像这样:

void ColorNode(TreeNodeCollection nodes, System.Drawing.Color Color)
{
    foreach (TreeNode child in nodes)
    {
        child.ForeColor= Color;
        if(child.Nodes != null && child.Nodes.Count>0)
          ColorNode(child.Nodes, Color);
    }
}

并从您的方法调用它,例如:

public void ColorChild(TreeNode nodes, int indx)
{
   ColorNode(nodes.Nodes, Color.Green);
}

如果树很大,您的解决方案会非常慢。只需在绘制节点时按需为节点着色即可。为此,您需要设置绘图模式:

treeView1.DrawMode = TreeViewDrawMode.OwnerDrawText;

然后,当节点即将绘制时,按需询问其颜色:

private void tree_DrawNode(object sender, DrawTreeNodeEventArgs e)
{
    TreeNodeStates state = e.State;
    Font font = e.Node.NodeFont ?? e.Node.TreeView.Font;
    Color foreColor;
    Color backColor;
    // node is selected
    // if you want to see the color of a selected node, too,
    // you can use inverted fore/back colors instead of system selection colors 
    if ((state & TreeNodeStates.Selected) == TreeNodeStates.Selected)
    {
        bool isFocused = (state & TreeNodeStates.Focused) == TreeNodeStates.Focused;
        backColor = SystemColors.Highlight;
        foreColor = isFocused ? SystemColors.HighlightText : SystemColors.InactiveCaptionText;
        e.Graphics.FillRectangle(SystemBrushes.Highlight, e.Bounds);
        if (isFocused)
            ControlPaint.DrawFocusRectangle(e.Graphics, e.Bounds, foreColor, backColor);
        TextRenderer.DrawText(e.Graphics, e.Node.Text, font, e.Bounds, foreColor, TextFormatFlags.GlyphOverhangPadding | TextFormatFlags.SingleLine | TextFormatFlags.EndEllipsis | TextFormatFlags.NoPrefix);
    }
    // node is not selected
    else
    {
        backColor = GetBackColor(e.Node); // GetBackColor: return some color by condition
        foreColor = GetForeColor(e.Node); // GetForeColor: return some color by condition
        using (Brush background = new SolidBrush(backColor))
        {
            e.Graphics.FillRectangle(background, e.Bounds);
            TextRenderer.DrawText(e.Graphics, e.Node.Text, font, e.Bounds, foreColor, TextFormatFlags.GlyphOverhangPadding | TextFormatFlags.SingleLine | TextFormatFlags.EndEllipsis);
        }
    }
}

如果条件发生更改,只需使树无效:

treeView1.Invalidate(); // this will re-draw the visible nodes

好吧,如果你想让 Color 作为函数的参数,没有什么能阻止你。

public void ColorChild(TreeNode nodes, int indx, Color color)
{
    foreach (TreeNode node_tmp in nodes.Nodes[indx].Nodes)
    {
       node_tmp.ForeColor = color;
       foreach (TreeNode node_tmp2 in node_tmp.Nodes)
       {
          node_tmp2.ForeColor = color;
          foreach (TreeNode node_tmp3 in node_tmp2.Nodes)
          {
             node_tmp3.ForeColor = color;
             foreach (TreeNode node_tmp4 in node_tmp3.Nodes)
             {
                node_tmp4.ForeColor = color;
                foreach (TreeNode node_tmp5 in node_tmp4.Nodes)
                {
                   node_tmp5.ForeColor = color;
                }
             }
          }
      }
   }
}

然后就这样称呼它

ColorChild(nodes, indx, Color.Green);