1. 程式人生 > >C# Treeview控制元件繪製

C# Treeview控制元件繪製

using System;
using System.Drawing;
using System.Windows.Forms;
using Windows.Resource;
 
namespace Windows.Forms.Controls
{
 
    public partial class TreeViewEx : TreeView
    {
 
        Color drawTextColor = Color.FromArgb(55, 55, 55);
 
        public TreeViewEx()
        {
            InitializeComponent();
 
            this.DrawMode = TreeViewDrawMode.OwnerDrawAll;
            this.FullRowSelect = true;
            this.ItemHeight = 23;
            this.HotTracking = true;
            this.ShowLines = true;
 
        }
 
        protected override void OnDrawNode(DrawTreeNodeEventArgs e)
        {
            base.OnDrawNode(e);
 
            //節點背景繪製
            if (e.Node.IsSelected)
            {
                e.Graphics.DrawImage(AssemblyHelper.GetImage("Resources.tree_Selected.png"), e.Bounds);
            }
            else if ((e.State & TreeNodeStates.Hot) != 0)//|| currentMouseMoveNode == e.Node)
            {
                e.Graphics.DrawImage(AssemblyHelper.GetImage("Resources.tree_Hover.png"), e.Bounds);
            }
            else
            {
                e.Graphics.FillRectangle(Brushes.White, e.Bounds);
            }
 
            //節點頭圖示繪製
            if (e.Node.IsExpanded)
            {
                e.Graphics.DrawImage(AssemblyHelper.GetImage("Resources.tree_NodeExpend.png"), e.Node.Bounds.X - 12, e.Node.Bounds.Y + 6);
            }
            else if (e.Node.IsExpanded == false && e.Node.Nodes.Count > 0)
            {
                e.Graphics.DrawImage(AssemblyHelper.GetImage("Resources.tree_NodeCollaps.png"), e.Node.Bounds.X - 12, e.Node.Bounds.Y + 6);
            }
 
            //文字繪製
            using (Font foreFont = new Font(this.Font, FontStyle.Regular))
            using (Brush drawTextBrush = new SolidBrush(drawTextColor))
            {
                e.Graphics.DrawString(e.Node.Text, foreFont, drawTextBrush, e.Node.Bounds.Left + 5, e.Node.Bounds.Top + 5);
            }
        }
 
        protected override void OnMouseDoubleClick(MouseEventArgs e)
        {
            base.OnMouseDoubleClick(e);
            TreeNode tn = this.GetNodeAt(e.Location);
            //調整【點選測試區域】大小,包括圖示
            Rectangle bounds = new Rectangle(tn.Bounds.Left - 12, tn.Bounds.Y, tn.Bounds.Width - 5, tn.Bounds.Height);
            if (tn != null && bounds.Contains(e.Location) == false)
            {
                if (tn.IsExpanded == false)
                    tn.Expand();
                else
                    tn.Collapse();
            }
        }
 
        protected override void OnMouseClick(MouseEventArgs e)
        {
            base.OnMouseClick(e);
            TreeNode tn = this.GetNodeAt(e.Location);
            this.SelectedNode = tn;
        }
 
        TreeNode currentNode = null;
        protected override void OnMouseMove(MouseEventArgs e)
        {
            base.OnMouseMove(e);
            TreeNode tn = this.GetNodeAt(e.Location);
            Graphics g = this.CreateGraphics();
            if (currentNode != tn)
            {
                //繪製當前節點的hover背景
                if (tn != null)
                    OnDrawNode(new DrawTreeNodeEventArgs(g, tn, new Rectangle(0, tn.Bounds.Y, this.Width, tn.Bounds.Height), TreeNodeStates.Hot));
 
                //取消之前hover的節點背景
                if (currentNode != null)
                    OnDrawNode(new DrawTreeNodeEventArgs(g, currentNode, new Rectangle(0, currentNode.Bounds.Y, this.Width, currentNode.Bounds.Height), TreeNodeStates.Default));
            }
            currentNode = tn;
            g.Dispose();
        }
 
 
        protected override void OnMouseLeave(EventArgs e)
        {
            base.OnMouseLeave(e);
            //移出控制元件時取消Hover背景
            if (currentNode != null)
            {
                Graphics g = this.CreateGraphics();
                OnDrawNode(new DrawTreeNodeEventArgs(g, currentNode, new Rectangle(0, currentNode.Bounds.Y, this.Width, currentNode.Bounds.Height), TreeNodeStates.Default));
            }
        }
    }
}