模仿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 = false;
       this.ShowNodeToolTips = 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 #region 3 画节点文本=========================================
Rectangle nodeTextRect = new Rectangle(
e.Node.Bounds.Left,
e.Node.Bounds.Top + 4,
e.Node.Bounds.Width + 2,
e.Node.Bounds.Height
);
nodeTextRect.Width += 4;
nodeTextRect.Height -= 4; e.Graphics.DrawString(e.Node.Text,
e.Node.TreeView.Font,
new SolidBrush(Color.Black),
nodeTextRect); //画子节点个数 (111)
if (e.Node.GetNodeCount(true) > 0)
{
e.Graphics.DrawString(string.Format("({0})", e.Node.GetNodeCount(true)),
new Font("Arial", 8),
Brushes.Gray,
nodeTextRect.Right - 4,
nodeTextRect.Top -2);
} ///*测试用,画文字出现的矩形*/
//if (e.Node.Text != "")
// e.Graphics.DrawRectangle(new Pen(Color.Blue), nodeTextRect);
#endregion #region 4 画IImageList 中的图标=================================================================== int currt_X = e.Node.Bounds.X;
if (this.ImageList != null && this.ImageList.Images.Count > 0)
{
//图标大小16*16
Rectangle imagebox = new Rectangle(
e.Node.Bounds.X - 3 - 16,
e.Node.Bounds.Y + 2,
16,//IMAGELIST IMAGE WIDTH
16);//HEIGHT int index = e.Node.ImageIndex;
string imagekey = e.Node.ImageKey;
if (imagekey != "" && this.ImageList.Images.ContainsKey(imagekey))
e.Graphics.DrawImage(this.ImageList.Images[imagekey], imagebox);
else
{
if (e.Node.ImageIndex < 0)
index = 0;
else if (index > this.ImageList.Images.Count - 1)
index = 0;
e.Graphics.DrawImage(this.ImageList.Images[index], imagebox);
}
currt_X -= 19; /*测试 画IMAGELIST的矩形*/
//if (e.Node.ImageIndex > 0)
// e.Graphics.DrawRectangle(new Pen(Color.Black, 1), imagebox);
}
#endregion
} protected override void OnBeforeSelect(TreeViewCancelEventArgs e)
{
base.OnBeforeSelect(e);
if (e.Node != null)
{
//禁止选中空白项
if (e.Node.Text == "")
{
//响应上下键
if (ArrowKeyUp)
{
if (e.Node.PrevNode != null && e.Node.PrevNode.Text != "")
this.SelectedNode = e.Node.PrevNode;
} if (ArrowKeyDown)
{
if (e.Node.NextNode != null && e.Node.NextNode.Text != "")
this.SelectedNode = e.Node.NextNode;
} e.Cancel = true;
}
}
} /// <summary>
/// 防止在选择设,treeNode闪屏
/// </summary>
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
if (!DesignMode)
{
cp.ExStyle |= 0x02000000;// Turn on WS_EX_COMPOSITED
}
return cp; }
}
}
}

  

 生成后拖动控件到界面中,实际效果如下

windform 重绘Treeview "+-"号图标的更多相关文章

  1. WinForm TreeView节点重绘,失去焦点的高亮显示

    当用户焦点离开TreeView时,TreeView选中节点仍然高亮,但是颜色符合主题. 设置TreeView.HideSelection = False;可让选中节点保持高亮. 添加重绘事件 Tree ...

  2. C#重绘TabControl

    C#重绘TabControl的Tabpage标签,添加图片及关闭按钮 Code highlighting produced by Actipro CodeHighlighter (freeware)h ...

  3. 玩转控件:重写/重绘Dev中MessageBox弹窗控件

    很久没有更新博客了,本想着直接发一篇<手撕ERP>系列,从控件重写.重绘,到框架搭建,再到部分模块实现+业务的.但是每次动手的时候,都觉得难以下手.直接从数据库设计开始吧,模块设计还没定下 ...

  4. (转)使用Custom Draw实现ListCtrl的重绘

    使用Custom Draw实现ListCtrl的重绘   common control 4.7版本介绍了一个新的特性叫做Custom Draw,这个名字显得模糊不清,让人有点摸不着头脑,而且MSDN里 ...

  5. 『转载』C# winform 中dataGridView的重绘(进度条,虚线,单元格合并等)

    原文转载自:http://hi.baidu.com/suming/item/81e45b1ab9b4585f2a3e2243 最近比较浅的研究了一下dataGridView的重绘,发现里面还是有很多东 ...

  6. 重绘TabControl

    本文转载自:http://blog.csdn.net/conmajia/article/details/7596718 作者:野比 (conmajia@gmail.com) 时间:May, 2012 ...

  7. 重绘panel控件,实现panel的阴影效果

    最近想在项目中添加一个要有阴影的panel控件,找了好多资料,最后通过采用图片的方式实现了panel的阴影效果,效果图如下: 重绘代码如下: using System; using System.Co ...

  8. HighChart学习-更新数据data Series与重绘

    一:HighChart介绍 基于JQuery的纯JavaScript的图标库,支持各种图表显示,同时还支持Mootools 与Prototype详细版本支持在这里: JQuery 1.3.2 - 1. ...

  9. Windows开发进阶之VC++中如何实现对话框的界面重绘

    技术:Windows 系统+Visual studio 2008   概述 应用程序界面是用户与应用程序之间的交互的桥梁和媒介,用户界面是应用程序中最重要的组成部分,也是最为直观的视觉体现.对用户而言 ...

随机推荐

  1. sql求倒数第二大的数,效率不高,但写法新颖

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  2. js处理img标签加载图片失败,显示默认图片

    1.第一种方法: 如果已经引入了jquery插件,就很好办.没有的话,如果实在需要,可以附上代码: script(type='text/javascript', src="http://aj ...

  3. xml约束的概念

    1 xml 约束的概念 XML 指可扩展标记语言(EXtensible Markup Language) XML 是一种标记语言,很类似 HTML XML 的设计宗旨是传输数据,而非显示数据 XML ...

  4. 20155312 2016-2017-2《Java程序设计》课程总结

    20155312 2016-2017-2<Java程序设计>课程总结 每周作业链接汇总 预备作业1:你期望的师生关系是什么? 预备作业2:做中学learning by doing个人感想 ...

  5. yum 安装报错:Could not retrieve mirrorlist http://mirrorlist.centos.org/?release=7&arch=x86_64&repo=os&infra=stock error was 14: curl#6 - "Could not resolve host: mirrorlist.centos.org; Unknown error"

    [root@venn09 ~]# yum install -y vim Loaded plugins: fastestmirror Could not retrieve mirrorlist http ...

  6. HMM(隐马尔可夫模型)不断学习中

    HMM(隐马尔可夫模型)是用来描述隐含未知参数的统计模型,举一个经典的例子:一个东京的朋友每天根据天气{下雨,天晴}决定当天的活动{公园散步,购物,清理房间}中的一种,我每天只能在twitter上看到 ...

  7. JS将时间戳转化为时间

    //将时间戳转化为时间 function timestampToTime(timestamp) { var date = new Date(timestamp * 1000);//时间戳为10位需*1 ...

  8. 百度地图经纬度和地址互转(Java代码)

    这是基于springmvc+mybatis 的一个controller.如果不是这个框架,可以把方法实体抽到自己写的一个类中,然后再测试 package com.uwitec.controller.s ...

  9. Winform自定义表单(转)

    出处:http://www.newlifex.com/showtopic-167.aspx 好吧,附件真的损坏了,原始代码我也没有了,再提取我也没精力了,不好意思,哪位之前下过可以重发一遍吗?不过即使 ...

  10. 走进JDK(一)------Object

    阅读JDK源码也是一件非常重要的事情,尤其是使用频率最高的一些类,通过源码可以清晰的清楚其内部机制. 如何阅读jdk源码(基于java8)? 首先找到本地电脑中的jdk安装路径,例如我的就是E:\jd ...