5

ノードが選択されていてフォーカスがある場合は、選択したノードの色を元に戻す必要があります-バックカラーは緑になり、選択されているがフォーカスがない場合は赤になります。選択したノードがツリー ビューにフォーカスされている場合とフォーカスされていない場合の違いがわかりません。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);
}   
4

1 に答える 1

4

ノードのプロパティを確認するだけで動作します(テスト済み)。また、次のように作成したカスタム ブラシをキャッシュすることをお勧めします (もちろん、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);
    }

PSおそらく、ノードなどを展開するためにクリックするものをレンダリングする必要があります.

于 2012-10-15T09:25:26.893 に答える