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

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

[复制链接]
跳转到指定楼层
楼主
DD2013 发表于 2015-1-5 13:24:48 | 只看该作者 |只看大图 回帖奖励 |倒序浏览 |阅读模式
查看: 26800|回复: 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 转播转播
回复 论坛版权

使用道具 举报

沙发
Blue_Pen 发表于 2015-1-5 14:11:14 | 只看该作者
本帖最后由 Blue_Pen 于 2015-1-5 14:12 编辑

注意排版规范,下次按照这种排版发帖,最好附上源码案例
板凳
 楼主| DD2013 发表于 2015-1-5 14:55:38 | 只看该作者
OK,好的。
回复

使用道具 举报

地板
 楼主| DD2013 发表于 2015-1-5 14:56:05 | 只看该作者
Blue_Pen 发表于 2015-1-5 14:11
注意排版规范,下次按照这种排版发帖,最好附上源码案例

ok,知道了
5#
乔克斯 发表于 2015-1-5 16:59:57 | 只看该作者
快上传源码案例~康忙~康忙
6#
 楼主| DD2013 发表于 2015-1-6 11:03:21 | 只看该作者
乔克斯 发表于 2015-1-5 16:59
快上传源码案例~康忙~康忙

在搞日期控件,我看了论坛的一片文章,日历是个窗体,这样不方便。自己做的,先上效果,暂时未美化。

   日期控件类型                                                                        日期窗体类型
7#
乔克斯 发表于 2015-1-6 11:39:54 | 只看该作者
DD2013 发表于 2015-1-6 11:03
在搞日期控件,我看了论坛的一片文章,日历是个窗体,这样不方便。自己做的,先上效果,暂时未美化。

   ...

还不错,重绘日历控件,做好后分享哦。
8#
sa110011sa 发表于 2015-1-6 14:04:26 | 只看该作者
效果很赞哦!
9#
work_liufei 发表于 2015-1-6 21:11:38 | 只看该作者
刚好需要这个东西  真的很值
10#
work_liufei 发表于 2015-1-6 21:12:06 | 只看该作者
感觉无比强大
您需要登录后才可以回帖 登录 | 加入CSkin博客

本版积分规则

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

Powered by Discuz! X3.2  © 2001-2013 Comsenz Inc.  Designed by ARTERY.cn
GMT+8, 2024-4-25 09:28, Processed in 0.585961 second(s), 32 queries , Gzip On.

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