TreeView节点自定义绘制样式

TreeView节点自定义绘制需要设置属性DrawMode属性为OwnerDrawAll;

 public LTreeNew()
        {
            // This call is required by the Windows.Forms Form Designer.
            ApplyNewUI.SetNewUI(this, InitializeComponent);
            
            // TODO: Add any initialization after the InitForm call
            //自已绘制  
            this.tree.DrawMode = TreeViewDrawMode.OwnerDrawAll;
            this.tree.DrawNode += new DrawTreeNodeEventHandler(tree1_DrawNode);
        }
        //在绘制节点事件中,按自已想的绘制  
        private void tree1_DrawNode(object sender, DrawTreeNodeEventArgs e)
        {
            TreeViewZJStyle.SetTreeNodeDraw(sender, e, imageCollectionTree, this.Width, 0, 0, 1);

        }  
/// <summary>
        /// 调整TreeView 样式
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// <param name="imageList1">节点图片组件</param>
        /// <param name="Width">总宽度</param>
        /// <param name="ExpandImageIndex">展开图</param>
        /// <param name="CollapseImageIndex">收缩图</param>
        /// <param name="LeafImageIndex">叶子图</param>
        public static void SetTreeNodeDraw(object sender, DrawTreeNodeEventArgs e, ImageCollection imageList1, int Width, int ExpandImageIndex, int CollapseImageIndex, int LeafImageIndex)
        {
            TreeNode tn = e.Node as TreeNode;
            if (tn == null)
            {
                return;
            }
            Point ptbigimage = new Point(tn.Bounds.X - 5, tn.Bounds.Y + 5);
            Point ptsmallimage = new Point(tn.Bounds.X-5, tn.Bounds.Y + 5);
            Size szbig = new Size(16, 16);//打开收缩图片
            Size szsmall = new Size(16, 16);//叶子图片
            //Rectangle bcimage = new Rectangle(ptimage, new Size(33, 23));
            //根据节点增加图片
            if (tn.IsExpanded)
            {
                e.Graphics.DrawImage(imageList1.Images[ExpandImageIndex], new Rectangle(ptbigimage, szbig));
            }
            else if (tn.Nodes.Count > 0)
            {
                e.Graphics.DrawImage(imageList1.Images[CollapseImageIndex], new Rectangle(ptbigimage, szbig));
            }
            else
            {
                e.Graphics.DrawImage(imageList1.Images[LeafImageIndex], new Rectangle(ptsmallimage, szsmall));
            }
            //增加背景颜色
            Point pt = new Point(tn.Bounds.X + 11, tn.Bounds.Y);
            Brush bccolor = new SolidBrush(System.Drawing.Color.FromArgb(((int)(((byte)(224)))), ((int)(((byte)(229)))), ((int)(((byte)(242))))));
            Brush ybccolor = new SolidBrush(System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(255)))), ((int)(((byte)(255))))));
            Rectangle bcrt = new Rectangle(pt, new Size(Width - e.Bounds.X, e.Bounds.Height));//使用Width充满整行
            if ((e.State & TreeNodeStates.Focused) != 0)
            {
                e.Graphics.FillRectangle(bccolor, bcrt);
            }
            else
            {
                e.Graphics.FillRectangle(ybccolor, bcrt);
            }
            //增加格线
            using (Pen focusPen = new Pen(System.Drawing.Color.FromArgb(((int)(((byte)(192)))), ((int)(((byte)(192)))), ((int)(((byte)(192)))))))
            {
                focusPen.DashStyle = System.Drawing.Drawing2D.DashStyle.Solid;
                e.Graphics.DrawRectangle(focusPen, bcrt);
            }
            //设置文本颜色和字体
            Point ptft = new Point(tn.Bounds.X + 16, tn.Bounds.Y + 7);
            Rectangle rt = new Rectangle(ptft, new Size(e.Bounds.Width, e.Bounds.Height));
            Brush bfcolor = new SolidBrush(System.Drawing.Color.FromArgb(((int)(((byte)(59)))), ((int)(((byte)(59)))), ((int)(((byte)(59))))));
            Font nf = tn.NodeFont == null ? ((TreeView)sender).Font : tn.NodeFont;
            e.Graphics.DrawString(e.Node.Text, nf, bfcolor, Rectangle.Inflate(rt, 1, 4));
        }

另外,ItemHeight属性调整节点显示高度;FullRowSelect和ShowLines属性设置选中整行。 

原文地址:https://www.cnblogs.com/zhutao1015/p/7016355.html