新建一个继承自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 ...

  2. C# 使用winForm的TreeView显示中国城镇四级联动

    直接上代码吧,这里 MySql.Data.MySqlClient;需要到mysql官网下载mysql-connector-net-6.9.8-noinstall.zip   访问密码 6073 usi ...

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

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

  4. Winform中Treeview控件失去焦点,将选择的节点设置为高亮显示 (2012-07-16 13:47:07)转载▼

    Winform中Treeview控件失去焦点,将选择的节点设置为高亮显示 (2012-07-16 13:47:07)转载▼标签: winform treeview drawnode Treeview控 ...

  5. WinForm中TreeView控件实现鼠标拖动节点(可实现同级节点位置互换,或拖到目标子节点)

    ;//1:不同级, 不为1:拖同级 private void treeView1_ItemDrag(object sender, ItemDragEventArgs e) { if (e.Button ...

  6. C#Winform中treeView控件使用总结

    1.如何展开结点时改变图标(注意:不是选中时) 要在目录中使用图标首先要加入一个控件ImageList(命名为imageList1),然后可以按图片的index或名称引用图片. 然后需要在TreeVi ...

  7. .NET在WebForm里实现类似WinForm里面TrackBar控件的效果(AJAX Control Toolkit的使用)

    WinForm 里面有一个 TrackBar 控件,表示一个标准的 Windows 跟踪条,是类似于 ScrollBar 控件的可滚动控件.用这个控件可以实现很多可以实时调整的功能,比如最常见的音量调 ...

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

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

  9. C# WinForm实现Windows 7 Aero磨砂玻璃效果

    在Vista系统之后,微软为窗体程序提供了Aero磨砂的效果,如下图.那么用C#如 何来实现这种磨砂效果呢? 代码: using System; using System.Collections.Ge ...

随机推荐

  1. 服务器安装VMware ESXI5.5

    一.VMware ESXI5.5 vSphere5.5 VIMSetup下载http://blog.sina.com.cn/s/blog_61c07ac50101gy64.html(1)VMware安 ...

  2. linux虚拟机centos64位_6.5+VM10安装oracle11g图文详解

    注意: vi基本命令:i--编辑状态  退出编辑并保存时先按ESC键,再按符合“:wq”或者":x"即可注意每个步骤时的当前用户,是root还是oracle 以root用户登录虚机 ...

  3. 使用NSURLCache为NSURLRequest设置缓存

    http://m.blog.csdn.net/blog/u011439689/18734363#

  4. cellForRowAtIndexPath方法不执行的那些坑

    今天用到了uitableview,是xib形式的.不过cellForRowAtIndexPath方法死活不执行,检查了返回的row数量,section的数量,数据源,代理都没问题,不过cellForR ...

  5. shell中的type命令

    type [-aftpP] name [name,...] -a 打印name的所有可能情况,比如type -a ls,会打印ls is aliased to 'ls --color=auto'和ls ...

  6. CF 8D Two Friends 【二分+三分】

    三个地点构成一个三角形. 判断一下两个人能否一起到shop然后回家,如果不能: 两个人一定在三角形内部某一点分开,假设沿着直线走,可以将问题简化. 三分从电影院出来时候的角度,在对应的直线上二分出一个 ...

  7. c#利用HttpWebRequest获取网页源代码

    c#利用HttpWebRequest获取网页源代码,搞了好几天终于解决了,直接获取网站编码进行数据读取,再也不用担心乱码了! 命名空间:Using System.Net private static ...

  8. l连接远程桌面

    参考:http://www.windows7en.com/Win7/17168.html 重要命令:mstsc

  9. Android 多渠道打包,上百渠道,秒打签名

    具体工具参见:https://github.com/hpu-spring87/MultiSignTools

  10. MAC中通过gem命令安装compass

    在MAC中通过gem命令安装compass时会出异常,原因是compass版本更新了,一些运行时所用到的依赖软件的版本没能得到更新,故而出现错误.例如,用以下命令安装compass: $ gem in ...