效果图:

新建一个继承自TreeView的控件类,代码如下:

using System;
using System.Windows.Forms;
using System.Drawing;
using System.Runtime.InteropServices; namespace SenseTreeView
{
public class SenseTreeView : TreeView
{
#region 控件属性 //显示字体
private Font _NodeFont;
public Font NodeFont
{
get
{
return _NodeFont;
}
set
{
_NodeFont = value;
}
} //选择TreeView TreeNode时的背景色
private Brush _BackgrountBrush;
public Brush BackgroundBrush
{
get
{
return _BackgrountBrush;
}
set
{
_BackgrountBrush = value;
}
} //选择TreeView TreeNode时背景色的边框画笔
private Pen _BackgroundPen;
public Pen BackgroundPen
{
get
{
return _BackgroundPen;
}
set
{
_BackgroundPen = value;
}
} //TreeView中TreeNode展开时的节点显示图标,
private Image _NodeExpandedImage;
public Image NodeExpandedImage
{
get
{
return _NodeExpandedImage;
}
set
{
_NodeExpandedImage = value;
}
}
//TreeView中TreeNode合拢时的节点显示图标
private Image _NodeCollapseImage;
public Image NodeCollapseImage
{
get
{
return _NodeCollapseImage;
}
set
{
_NodeCollapseImage = value;
}
}
//TreeView中TreeNode的节点显示图标的大小
private Size _NodeImageSize;
public Size NodeImageSize
{
get
{
return _NodeImageSize;
}
set
{
_NodeImageSize = value;
}
} //节点显示图标离左边界的位置
private int _NodeOffset;
public int NodeOffset
{
get
{
return _NodeOffset;
}
set
{
_NodeOffset = value;
}
} #endregion #region 构造函数 public SenseTreeView()
{
//设置窗体Style
//this.SetStyle(ControlStyles.UserPaint, true); //支持用户重绘窗体
//this.SetStyle(ControlStyles.AllPaintingInWmPaint, true); //在内存中先绘制界面
//this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true); //双缓冲,防止绘制时抖动
//this.SetStyle(ControlStyles.DoubleBuffer, true); //双缓冲,防止绘制时抖动
//this.UpdateStyles(); //不显示树形节点显示连接线
this.ShowLines = false; //设置绘制TreeNode的模式
this.DrawMode = TreeViewDrawMode.OwnerDrawAll; //不显示TreeNode前的“+”和“-”按钮
this.ShowPlusMinus = false; //不支持CheckedBox
this.CheckBoxes = false; //设置TreeNode的行高
SendMessage(this.Handle, TVM_SETITEMHEIGHT, , ); //设置默认BackgroundBrush
BackgroundBrush = new SolidBrush(Color.FromArgb(, Color.FromArgb(, , ))); //设置默认BackgroundPen
BackgroundPen = new Pen(Color.FromArgb(, , ), ); //设置默认NodeFont
NodeFont = new Font("宋体", , FontStyle.Regular); //设置默认节点显示图标及Size
NodeExpandedImage = null;
NodeCollapseImage = null;
NodeImageSize = new Size(, ); //设置默认节点显示图标便宜位置
NodeOffset = ;
} #endregion #region 节点绘制函数 //绘制TreeView树中TreeNode
protected override void OnDrawNode(DrawTreeNodeEventArgs e)
{
TreeNode tn = e.Node as TreeNode;
if (tn == null)
{
return;
} //设置Image绘制Rectangle
Point pt = new Point(tn.Bounds.X + NodeOffset, tn.Bounds.Y);
Rectangle rt = new Rectangle(pt, NodeImageSize); if ((e.State & TreeNodeStates.Selected) != )
{
//绘制TreeNode选择后的背景框
e.Graphics.FillRectangle(BackgroundBrush, , tn.Bounds.Y, this.Width - , tn.Bounds.Height - ); //绘制TreeNode选择后的边框线条
e.Graphics.DrawRectangle(BackgroundPen, , tn.Bounds.Y, this.Width - , tn.Bounds.Height - );
} //绘制节点图片
if (NodeExpandedImage != null && NodeCollapseImage != null)
{
if (tn.Nodes.Count != )
{
if (tn.IsExpanded == true)
{
e.Graphics.DrawImage(NodeExpandedImage, rt);
}
else
{
e.Graphics.DrawImage(NodeCollapseImage, rt);
}
} rt.X += ;
} //绘制节点自身图片
if (e.Node.SelectedImageIndex != - && this.ImageList != null)
{
rt.X += ;
e.Graphics.DrawImage(this.ImageList.Images[e.Node.SelectedImageIndex], rt);
} //绘制节点的文本
rt.X += ;
rt.Y += ;
rt.Width = this.Width - rt.X;
e.Graphics.DrawString(e.Node.Text, NodeFont, Brushes.Black, rt);
} #endregion #region 鼠标消息响应函数 //响应鼠标按下消息
protected override void OnMouseDown(MouseEventArgs e)
{
TreeNode clickedNode = this.GetNodeAt(e.X, e.Y); if (clickedNode != null && NodeBounds(clickedNode).Contains(e.X, e.Y))
{
this.SelectedNode = clickedNode;
}
} //响应鼠标双击消息
protected override void OnMouseDoubleClick(MouseEventArgs e)
{
TreeNode clickedNode = this.GetNodeAt(e.X, e.Y); if (clickedNode != null && NodeBounds(clickedNode).Contains(e.X, e.Y))
{
this.SelectedNode = clickedNode; //判断节点的状态
if (clickedNode.Nodes.Count != )
{
if (clickedNode.IsExpanded)
{
clickedNode.Collapse();
}
else
{
clickedNode.Expand();
}
}
}
} #endregion #region 私有函数 //返回TreeView中TreeNode的整行区域
private Rectangle NodeBounds(TreeNode node)
{
// Set the return value to the normal node bounds.
Rectangle bounds = node.Bounds; //if (node.Tag != null)
//{
// // Retrieve a Graphics object from the TreeView handle
// // and use it to calculate the display width of the tag.
// Graphics g = this.CreateGraphics();
// int tagWidth = (int)g.MeasureString(node.Tag.ToString(), NodeFont).Width + 6; // // Adjust the node bounds using the calculated value.
// bounds.Offset(tagWidth / 2, 0);
// bounds = Rectangle.Inflate(bounds, tagWidth / 2, 0);
// g.Dispose();
//} bounds.Width = this.Width; return bounds;
} #endregion #region 引用函数 const int TV_FRIST = 0x1100;
const int TVM_SETITEMHEIGHT = TV_FRIST + ; [DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern IntPtr SendMessage(IntPtr hWnd, int nMsg, int wParam, int Param); #endregion
}
}

在窗体里应用:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using SenseTreeView.Properties; namespace SenseTreeView
{
public partial class Form1 : Form
{
private SenseTreeView stv; public Form1()
{
InitializeComponent(); // Create and initialize the TreeView control.
stv = new SenseTreeView();
stv.Dock = DockStyle.Left;
stv.BackColor = Color.White;
//stv.CheckBoxes = true;
stv.Width = ; ImageList imagelist = new ImageList();
imagelist.Images.Add(Resources._1264);
imagelist.Images.Add(Resources._1265);
imagelist.Images.Add(Resources._1268);
stv.ImageList = imagelist; stv.NodeExpandedImage = Resources.UnSelect;
stv.NodeCollapseImage = Resources.IsSelect; // Add nodes to the TreeView control.
for (int x = ; x < ; ++x)
{
// Add a root node to the TreeView control.
TreeNode node = new TreeNode("中华人民共和国");
node.SelectedImageIndex = ;
for (int y = ; y < ; ++y)
{
// Add a child node to the root node.
TreeNode tn = new TreeNode("Subtask");
tn.SelectedImageIndex = ;
node.Nodes.Add(tn);
}
stv.Nodes.Add(node);
}
stv.ExpandAll(); // Initialize the form and add the TreeView control to it.
this.Controls.Add(stv);
}
}
}

最新文章

  1. 0037 Java学习笔记-多线程-同步代码块、同步方法、同步锁
  2. PHP反射API
  3. unity 播放音乐
  4. string.Format格式化
  5. YAML 语言语法
  6. QT 环境下开发socketCan接口程序
  7. 嵌入式 Linux下curl库API简单介绍
  8. 编写一个递归函数,输出vector对象的内容
  9. Linq- ExcuteQuery用法
  10. node.js 入门(一)安装
  11. 第一次作业 orm环境构建(hibernate)及基本的demo
  12. DevExpress ASP.NET Core Controls 2019发展蓝图(No.4)
  13. asterisk todo
  14. win2003无线网卡驱动无法安装解决方法
  15. QThreadPool线程池的开发使用
  16. HDU 4920 Matrix multiplication(矩阵相乘)
  17. android:shape的使用(+圆角ListView)(转)
  18. 在MyEclipse中修改类不重启tomcat
  19. Linux 系统默认运行级别设定
  20. js布尔值转化

热门文章

  1. L1-064 估值一亿的AI核心代码 (20 分)
  2. Codeforces 1240C. Paint the Tree
  3. web框架链接
  4. B-Tree和 B+Tree的数据存储结构
  5. 经典算法,yuv与rgb互转,查表法,让你的软件飞起来
  6. Vue学习笔记(一) 利用idea 搭建 vue 项目
  7. linux递归查找目录下所有文件夹以及文件
  8. MySQL踩坑及MySQL解压版安装
  9. 1、Bash Shell
  10. 超简单!教你如何修改源列表(sources.list)来提高软件访问速度