void treeView1_DrawNode(object sender, DrawTreeNodeEventArgs e) { if (e.Node.Parent == null) { Color backColor, foreColor; if ((e.State & TreeNodeStates.Selected) == TreeNodeStates.Selected) { backColor = SystemColors.Highlight; foreColor = SystemColors.HighlightText; } else if ((e.State & TreeNodeStates.Hot) == TreeNodeStates.Hot) { backColor = SystemColors.HotTrack; foreColor = SystemColors.HighlightText; } else { backColor = e.Node.BackColor; foreColor = e.Node.ForeColor; } if (this.treeView1.ShowPlusMinus) { #region 画一个“加号”表示未展开的 Pen pen = new Pen(Brushes.Black); Rectangle plusBound = new Rectangle(new Point(0, e.Bounds.Top), new Size(this.treeView1.Width, 18)); e.Graphics.DrawRectangle(pen, plusBound.X + 7, plusBound.Y + 2, 10, 10); e.Graphics.DrawLine(pen, plusBound.X + 9, plusBound.Top + 7, plusBound.Left + 15, plusBound.Top + 7); if (!e.Node.IsExpanded) { //如果节点未展开,则在减号中添加一条线,变成加号 e.Graphics.DrawLine(pen, plusBound.X + 12, plusBound.Top + 4, plusBound.Left + 12, plusBound.Top + 10); } #endregion } Rectangle newBounds = e.Node.Bounds; newBounds.X = 20; using (SolidBrush brush = new SolidBrush(backColor)) { e.Graphics.FillRectangle(brush, newBounds); } TextRenderer.DrawText(e.Graphics, e.Node.Text, this.treeView1.Font, newBounds, foreColor, backColor); if ((e.State & TreeNodeStates.Focused) == TreeNodeStates.Focused) { ControlPaint.DrawFocusRectangle(e.Graphics, newBounds, foreColor, backColor); } e.DrawDefault = false; } else { e.DrawDefault = true; } }