设为首页 加入收藏

TOP

windform 重绘Treeview "+-"号图标(一)
2019-09-17 16:51:22 】 浏览:33
Tags:windform 重绘 Treeview " -" 图标

模仿wind系统界面,重绘Treeview + - 号图标

一,首先需要图片 ,用于替换原有的 +-号

 

二、新建Tree扩展类 TreeViewEx继承TreeView

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

/******************************************************************* 
* Copyright (C)  版权所有
* 文件名称:TreeViewEx
* 命名空间:TestRecentMenu
* 创建时间:2018/12/18 16:49:08
* 作    者: wangyonglai
* 描    述:
* 修改记录:
* 修改人:
* 版 本 号:v1.0.0
**********************************************************************/
namespace TestRecentMenu
{
    public class TreeViewEx : TreeView
    {
        private bool ArrowKeyUp = false;
        private bool ArrowKeyDown = false;
        private System.Windows.Forms.ImageList arrowImageList1;

        /*1节点被选中 ,TreeView有焦点*/
        private SolidBrush brush1 = new SolidBrush(Color.FromArgb(209, 232, 255));//填充颜色
        private Pen pen1 = new Pen(Color.FromArgb(102, 167, 232), 1);//边框颜色

        /*2节点被选中 ,TreeView没有焦点*/
        private SolidBrush brush2 = new SolidBrush(Color.FromArgb(247, 247, 247));
        private Pen pen2 = new Pen(Color.FromArgb(222, 222, 222), 1);

        /*3 MouseMove的时候 画光标所在的节点的背景*/
        private SolidBrush brush3 = new SolidBrush(Color.FromArgb(229, 243, 251));
        private Pen pen3 = new Pen(Color.FromArgb(112, 192, 231), 1);

        public const int WM_PRINTCLIENT = 0x0318;
        public const int PRF_CLIENT = 0x00000004;


        //替换+-号图标的imagelist
        public ImageList arrowImageList
        {
            get
            {
                return arrowImageList1;
            }
            set
            {
                arrowImageList1 = value;
            }
        }

        public TreeViewEx()
        {
            //双缓存防止屏幕抖动
            //this.SetStyle(ControlStyles.UserPaint, true);
            this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint | ControlStyles.DoubleBuffer, true);
            this.UpdateStyles();
            this.DrawMode = TreeViewDrawMode.OwnerDrawAll;
            this.FullRowSelect = true;
            this.HotTracking = true;
            this.HideSelection = false;
            //this.ShowLines = true;
            this.ItemHeight = 20;

        }


        protected override void OnDrawNode(DrawTreeNodeEventArgs e)
        {
            base.OnDrawNode(e);

            #region 1     选中的节点背景=========================================
            Rectangle nodeRect = new Rectangle(1, e.Bounds.Top, e.Bounds.Width - 3, e.Bounds.Height - 1);

            if (e.Node.IsSelected)
            {
                if (this.Focused)
                {
                    e.Graphics.FillRectangle(brush1, nodeRect);
                    e.Graphics.DrawRectangle(pen1, nodeRect);
                }
                else
                {
                    e.Graphics.FillRectangle(brush2, nodeRect);
                    e.Graphics.DrawRectangle(pen2, nodeRect);
                }

            }
            else if ((e.State & TreeNodeStates.Hot) != 0 && e.Node.Text != "")//|| currentMouseMoveNode == e.Node)
            {
                e.Graphics.FillRectangle(brush3, nodeRect);
                e.Graphics.DrawRectangle(pen3, nodeRect);
            }
            else
            {
                e.Graphics.FillRectangle(Brushes.White, e.Bounds);
            }

            #endregion

            #region 2     +-号绘制=========================================
            Rectangle plusRect = new Rectangle(e.Node.Bounds.Left - 32, nodeRect.Top + 6, 9, 9); // +-号的大小 是9 * 9

            if (e.Node.IsExpanded)
                e.Graphics.DrawImage(arrowImageList.Images[1], plusRect);
            else if (e.Node.IsExpanded == false && e.Node.Nodes.Count > 0)
                e.Graphics.DrawImage(arrowImageList.Images[0], plusRect);


            /*测试用 画出+-号出现的矩形*/
            //if (e.Node.Nodes.Count > 0)
            //    e.Graphics.DrawRectangle(new Pen(Color.Red), plusRect);
            #endregion

            #re
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇《C#并发编程经典实例》学习笔记.. 下一篇Asp.Net Core 轻松学-在.Net Core..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目