效果图:

新建一个继承自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. 19牛客暑期多校 round2 F dfs

    题目传送门//res tp nowcoder dfs 先将所有人都归于一队,之后从一队中取出人放置到另一个队. #include<iostream> #include<cstdio& ...

  2. 剑指offer6:旋转数组的最小数字

    1. 题目描述 把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转.输入一个非递减排序的数组的一个旋转,输出旋转数组的最小元素.例如数组{3,4,5,1,2}为{1,2,3,4,5}的一 ...

  3. python中的类变量和对象变量,以及传值传引用的探究

    http://www.cnblogs.com/gtarcoder/p/5005897.html http://www.cnblogs.com/mexh/p/9967811.html

  4. Django入门:操作数据库(Model)

    Django-Model操作数据库(增删改查.连表结构) 一.数据库操作 1.创建model表         基本结构 1 2 3 4 5 6 from django.db import model ...

  5. Codeforces 1238G. Adilbek and the Watering System

    传送门 最关键的想法就是每个位置一定用的是当前能用的最便宜的水,因为到后面可能有更便宜的 然后其他还没用上的水我们也留着,假装此时已经买了,但是如果发现后面有更优的再反悔也不迟 每相邻两个朋友之间我们 ...

  6. C#选择文件保存路劲

    private void button8_Click(object sender, EventArgs e) { FolderBrowserDialog dialog = new FolderBrow ...

  7. gperftools cpp wrapper

    gperftools cpp wrapper // Compile command : ${CXX} -o test_profiler.elf -DUSE_GPERFTOOLS -DDEBUG -D_ ...

  8. react的嵌套组件

    react没有vue插槽的概念,但是有嵌套组件,可以用嵌套组件实现类似插槽的功能.下例中,color,name,btn相当于具名插槽,children相当于匿名插槽. import React fro ...

  9. JS基础_条件分支语句:switch语句

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  10. 微信小程序常用事件

    bind bind事件绑定不会阻止冒泡事件向上冒泡,catch事件绑定可以阻止冒泡事件向上冒泡. bindtap  跳转页面 bindchange  .value 改变时触发 change 事件 bi ...