效果图:

新建一个继承自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);
}
}
}

【WinForm-TreeView】实现Win7 Areo效果的更多相关文章

  1. WinForm的TreeView实现Win7 Areo效果

    新建一个继承自TreeView的控件类,代码如下: using System; using System.Windows.Forms; using System.Drawing; using Syst ...

  2. 关于WinForm TreeView的分享~

    最近在写个测试demo的时候使用到WinForm TreeView,已经好久没接触了,有些生疏,所以还是记录一下遇到的一些问题. 1.如果动态绑定TreeView,这个功能一般会在数据量不确定,需要去 ...

  3. Windows 7个性化配置,关闭Win7动画效果,设置窗口背景为“ 豆绿色”

    减少眼睛疲劳配色(豆绿色): RGB:, , ,颜色名称:#C7EDCC 1.任务栏设置 2.关闭Win7动画效果 控制面板 -> 轻松访问 -> 优化视频显示 3.去掉窗口阴影 右键单击 ...

  4. winform treeView 数据绑定

    转载:http://www.jetwu.cn/archives/737 winform treeView 数据绑定 private void Form1_Load(object sender, Eve ...

  5. winform实现图片的滑动效果

    使用winform实现图片的滑动效果(类似网站首页图片滑动切换效果),结果实现了,但是效果其实不是很理想.也许有更好的方法.         Timer timerSlide = null; //当前 ...

  6. Winform TreeView 查找下一个节点

    转载:http://www.cnblogs.com/Ruiky/archive/2013/02/01/2888674.html public static class TreeViewHelper { ...

  7. Windows Server 2008 R2 开启Win7主题效果Aero

    1.打开 开始---管理工具----服务器管理器--功能 2.点击 “添加功能”,选择“桌面体验”,这样就会安装上win7 主题和Windows media player 3.重启电脑后,在“服务”里 ...

  8. winform Treeview控件使用

    做角色菜单权限时用到treeview控件做树状显示菜单,简单总结了一下用法: 1.在winform窗体中拖入treeview控件,注意修改属性CheckBoxes属性为true,即在节点旁显示复选框 ...

  9. winform TreeView的一些用法以及异步加载

    今天,主要弄了一下对于树型控件的一些方法,以及异步加载.参考: http://www.cnblogs.com/greatverve/archive/2012/03/23/winform-treevie ...

随机推荐

  1. ubuntu 设置静态ip,但显示scope global secondary ens33

    设置静态ip 修改 /etc/network/interfaces 文件 # The loopback network interface auto lo iface lo inet loopback ...

  2. 阿里云ECS云服务器CentOS部署个人网站

    ping了一下coding pages和阿里云服务器的速度,意外感觉coding的速度反而更快.不过为了折腾,还是把博客迁移到阿里云,跌跌撞撞遇到很多坑,大多是由于对指令不熟悉以及部分教程省略了部分步 ...

  3. pt工具

    percona-toolkit简介percona-toolkit是一组高级命令行工具的集合,用来执行各种通过手工执行非常复杂和麻烦的mysql任务和系统任务,这些任务包括: 检查master和slav ...

  4. Good Triple CodeForces - 1169D (等差子序列)

    大意: 给定01字符串, 求有多少个区间$[l,r]$, 使得存在正整数$x,k$满足$1\le x,k\le n,l\le x<x+2k\le r,s_x=s_{x+k}=s_{x+2k}$. ...

  5. C# List.sort排序(多权重,升序降序)

    很多人可能喜欢Linq的orderBy排序,可惜U3D里面linq在Ios上会报错,所以就必须使用list的排序. 其实理解了并不难 升序降序比较 sort有三种结果 1,-1,0分别是大,小,相等. ...

  6. 【原创】大数据基础之Kudu(6)kudu tserver内存占用统计分析

    kudu tserver占用内存过高后会拒绝部分写请求,日志如下: 19/06/01 13:34:12 INFO AsyncKuduClient: Invalidating location 34b1 ...

  7. luogu题解 P3709 【大爷的字符串题】

    题目链接: https://www.luogu.org/problemnew/show/P3709 思路: 首先我是没读懂题目的,浏览了讨论区的dalao发现才知道就是求区间众数的出现次数. 然后肯定 ...

  8. 林大妈的CSS知识清单(二)可见格式化模型(内含margin塌陷与浮动闭合的解决方案)

    盒模型.浮动和定位是CSS中最重要的三个概念.它们共同决定了一个元素在页面中以怎样的形式进行排布与显示. 一.盒模型 1. 定义 盒模型是CSS的核心概念.一个页面中,所有的元素(不管他最终显示是圆形 ...

  9. Win10系统如何利用蓝牙设置动态锁?

    很多小伙伴都会有这样的经历,出门之后没走多远,却已然忘记是否锁门,有强迫症的人就会重新返回查看,以确保门是否反锁. 我们在使用电脑时也是这样,遇到事情要临时离开,却忘记是否锁屏,再返回来就耽误时间了. ...

  10. 单节点FastDFS与Nginx部署

    一.安装基本组件 1.安装编译需要的组件,必安装组件. yum install gcc-c++ 2.安装libevent函数库.pcre-devel zlib-devel必安装组件.     yum ...