Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用TreeViewer.OwnerDrawAll更改TreeView中选定节点的背景色?_C#_.net_Winforms - Fatal编程技术网

C# 如何使用TreeViewer.OwnerDrawAll更改TreeView中选定节点的背景色?

C# 如何使用TreeViewer.OwnerDrawAll更改TreeView中选定节点的背景色?,c#,.net,winforms,C#,.net,Winforms,我需要改变所选节点的背景颜色,当节点被选中并有焦点时,背景颜色变为绿色,当被选中但没有焦点时,背景颜色变为红色。我无法区分聚焦于树状视图的选定节点和不聚焦于树状视图的选定节点。位于TabPage对象中的树视图 //... this.myTreeView.HideSelection = false; //... private void myTreeView_drawNode(object sender, DrawTreeNodeEventArgs e) { Color backCol

我需要改变所选节点的背景颜色,当节点被选中并有焦点时,背景颜色变为绿色,当被选中但没有焦点时,背景颜色变为红色。我无法区分聚焦于树状视图的选定节点和不聚焦于树状视图的选定节点。位于TabPage对象中的树视图

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

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


另外,您可能需要渲染您单击以展开节点等的内容。

使用
myTreeView.Focused
而不是
this.myTabControl.Focused
?@aliassce,这不起作用,我只是想让您知道,您的代码对于我需要做的事情非常有效。谢谢发布。您希望确保控件上的DrawMode也设置为OwnerDrawAll。
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);
    }