Treeview控件失去焦点,将选择的节点设置为高亮显示

  1. 修改TreeView的属性
    1 this.treeView1.HideSelection = false;
    2 this.treeView1.DrawMode = TreeViewDrawMode.OwnerDrawText;
  2. 添加treeView1_DrawNode事件
    1 private void treeView1_DrawNode(object sender, DrawTreeNodeEventArgs e)
    2 {
    3   e.DrawDefault = true;//由操作系统绘制节点,默认值为false
    4 }
     1 private void treeView1_DrawNode(object sender, DrawTreeNodeEventArgs e)
     2 {
     3     if ((e.State & TreeNodeStates.Selected) != 0)
     4     {
     5         e.Graphics.FillRectangle(Brushes.DodgerBlue, e.Node.Bounds);
     6 
     7         Font nodeFont = e.Node.NodeFont;
     8         if (nodeFont == null) nodeFont = ((TreeView)sender).Font;
     9         e.Graphics.DrawString(e.Node.Text, nodeFont, Brushes.White, Rectangle.Inflate(e.Bounds, 2, 0));
    10     }
    11     else
    12     {
    13         e.DrawDefault = true;
    14     }
    15 
    16     if ((e.State & TreeNodeStates.Focused) != 0)
    17     {
    18         using (Pen focusPen = new Pen(Color.White))
    19         {
    20             focusPen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;
    21             Rectangle focusBounds = e.Node.Bounds;
    22             focusBounds.Size = new Size(focusBounds.Width - 1,
    23             focusBounds.Height - 1);
    24             e.Graphics.DrawRectangle(focusPen, focusBounds);
    25         }
    26     }
    27 }

     

你可能感兴趣的:(Treeview控件失去焦点,将选择的节点设置为高亮显示)