作者: DD2013
查看: 26966|回复: 34
打印 上一主题 下一主题

[教程] 【C#TreeView控件】TreeView的扩展

[复制链接]
跳转到指定楼层
#
DD2013 发表于 2015-1-5 13:24:48 | 只看该作者 |只看大图 回帖奖励 |正序浏览 |阅读模式
查看: 26966|回复: 34
Treeview控件扩展
说明:
重写了treeview。还是和以前一样,先晒效果。(加了鼠标的跟随的效果,点击区域变大,节点的图标绘制)

效果图:


以下是控件源码:
[C#] 纯文本查看 复制代码
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(81, 81, 81);

        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));
            }
        }
    }
}


评分

参与人数 1金钱 +3 收起 理由
乔克斯 + 3 感谢分享,LZ辛苦了~

查看全部评分

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏18 转播转播
回复 论坛版权

使用道具 举报

34#
岔路ko 发表于 2020-8-19 22:00:24 | 只看该作者
好评,快上传原始码案例〜康忙〜康忙
33#
cyuehua0278 发表于 2020-5-13 08:44:31 | 只看该作者
非常感谢楼主,很厉害了!
32#
ql_quincy 发表于 2020-1-8 00:22:39 | 只看该作者
123123123123123
31#
青衫 发表于 2019-10-18 16:03:59 | 只看该作者
大佬可以发下AssemblyHelper类给我看看吗
30#
鹏飞千里13 发表于 2019-3-16 20:50:03 | 只看该作者
这个相当牛,膜拜
29#
maker316 发表于 2019-3-15 17:08:43 | 只看该作者
mark一下 以备不时之需
28#
 楼主| DD2013 发表于 2017-11-24 15:27:46 | 只看该作者
1715577326 发表于 2016-5-5 11:59
楼主有没有源码实例?求分享一份呗。谢谢

已经线上使用了。
27#
gavin2016 发表于 2017-9-22 11:00:01 | 只看该作者
感谢分享,学习一下。
26#
gts 发表于 2017-4-24 02:46:30 | 只看该作者
受益匪浅,非常感谢。
25#
wangcaiok 发表于 2017-4-4 23:21:47 | 只看该作者
加载treeview时,第一个node会有重影!!
您需要登录后才可以回帖 登录 | 加入CSkin博客

本版积分规则

QQ|申请友链|小黑屋|手机版|Archiver|CSkin ( 粤ICP备13070794号

Powered by Discuz! X3.2  © 2001-2013 Comsenz Inc.  Designed by ARTERY.cn
GMT+8, 2024-5-21 04:05, Processed in 0.658391 second(s), 33 queries , Gzip On.

快速回复 返回顶部 返回列表