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

时间:2016-02-02 13:45:26

标签: c# winforms colors treeview treenode

我有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;
                }
             }
          }
      }
   }
}

3 个答案:

答案 0 :(得分:5)

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

类似的东西:

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);
}

答案 1 :(得分:2)

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

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

答案 2 :(得分:0)

好吧,如果你想让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);