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屬性設置選中整行。 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM