重写C#TreeView控件,提升TreeView控件档次
我改过的TreeView控件的效果如下:
鼠标没有放到树额节点上
鼠标放到树的节点上
数节点的展开
展开树的子节点
鼠标放到数的节点上
下面是画上下箭头 的代码
//父节点的画箭头
private void DrawParentNodeArrow(DrawTreeNodeEventArgs e, int Direction)
{
Graphics g = e.Graphics;
g.SmoothingMode = SmoothingMode.AntiAlias;
GraphicsPath path = new GraphicsPath();
Point[] pt;
if (Direction == ParentNodeArrowDirection.Up)
{
pt = new Point[3] { new Point(e.Bounds.X+e.Bounds.Width-22,(e.Bounds.Height/2)+2+e.Bounds.Y),
new Point(e.Bounds.X+e.Bounds.Width-10, (e.Bounds.Height/2)+2+e.Bounds.Y),
new Point(e.Bounds.X+e.Bounds.Width-16, (e.Bounds.Height/2)-4+e.Bounds.Y)
};
}
else if (Direction == ParentNodeArrowDirection.Down)
{
pt = new Point[3] { new Point(e.Bounds.X+e.Bounds.Width-22,(e.Bounds.Height/2)-2+e.Bounds.Y),
new Point(e.Bounds.X+e.Bounds.Width-10, (e.Bounds.Height/2)-2+e.Bounds.Y),
new Point(e.Bounds.X+e.Bounds.Width-16, (e.Bounds.Height/2)+4+e.Bounds.Y) };
}
else
{
return;
}
path.AddPolygon(pt);
g.FillPath(new SolidBrush(Color.Black), path);
}
子节点的中箭头的画法
//子节点的箭头
private void DrawChildNodeArrow(int i缩进, DrawTreeNodeEventArgs e, int Direction)
{
Graphics g = e.Graphics;
g.SmoothingMode = SmoothingMode.AntiAlias;
GraphicsPath path = new GraphicsPath();
Point[] pt;
if (Direction == ChileNodeArrowDirection.Right)
{
pt = new Point[3] { new Point(e.Bounds.X+i缩进-12,e.Bounds.Y+(e.Bounds.Height)/2-4),
new Point(e.Bounds.X+i缩进-12, e.Bounds.Y+(e.Bounds.Height)/2+4),
new Point(e.Bounds.X+i缩进-6, e.Bounds.Y+(e.Bounds.Height)/2)};
path.AddPolygon(pt);
g.DrawPath(new Pen(Color.Black), path);
}
else if (Direction == ChileNodeArrowDirection.RightDown)
{
pt = new Point[3] { new Point(e.Bounds.X+i缩进-12,e.Bounds.Y+(e.Bounds.Height/2)+4),
new Point(e.Bounds.X+i缩进-4, e.Bounds.Y+(e.Bounds.Height/2)+4),
new Point(e.Bounds.X+i缩进-4, e.Bounds.Y+(e.Bounds.Height/2)-4)};
path.AddPolygon(pt);
g.FillPath(new SolidBrush(Color.Black), path);
}
else
{
return;
}
}
最重要的就是画树的节点了
protected override void OnDrawNode(DrawTreeNodeEventArgs e)
{
Font f = null;
int i缩进 = 0;
if (e.Node.Parent == null)
{
Color gradientBegin = Color.FromArgb(203, 225, 252);
Color gradientEnd = Color.FromArgb(125, 165, 224);
if ((e.State & TreeNodeStates.Hot) != 0)
{
gradientBegin = Color.FromArgb(255, 255, 222);
gradientEnd = Color.FromArgb(255, 203, 136);
f = new Font(FontFamily.GenericSansSerif, 10, FontStyle.Bold);
}
else
{
f = new Font(FontFamily.GenericSansSerif, 10, FontStyle.Regular);
}
using (Brush b = new LinearGradientBrush(e.Bounds, gradientBegin,
gradientEnd, LinearGradientMode.Vertical))
{
e.Graphics.FillRectangle(b, e.Bounds);
}
//画箭头
if (((e.State & TreeNodeStates.Hot) != 0) && e.Node.Nodes.Count > 0)
{
if (e.Node.IsExpanded)
{
DrawParentNodeArrow(e, ParentNodeArrowDirection.Up);
}
else
{
DrawParentNodeArrow(e, ParentNodeArrowDirection.Down);
}
}
i缩进 = 0;
}
else
{
if ((e.State & TreeNodeStates.Hot) != 0)
{
Color gradientBegin = Color.FromArgb(255, 255, 255);
//Color gradientBegin = Color.FromArgb(190, 227, 253);
Color gradientEnd = Color.FromArgb(190, 227, 253);
using (Brush b = new LinearGradientBrush(e.Bounds, gradientBegin,
gradientEnd, LinearGradientMode.Vertical))
{
e.Graphics.FillRectangle(b, e.Bounds);
}
f = new Font(FontFamily.GenericSansSerif, 10, FontStyle.Bold);
}
else
{
e.Graphics.FillRectangle(new SolidBrush(Color.White), e.Bounds);
f = new Font(FontFamily.GenericSansSerif, 10, FontStyle.Regular);
}
i缩进 = Node级次(e.Node) * Indent;
//画箭头
if (e.Node.Nodes.Count > 0)
{
if (e.Node.IsExpanded)
{
DrawChildNodeArrow(i缩进, e, ChileNodeArrowDirection.RightDown);
}
else
{
DrawChildNodeArrow(i缩进, e, ChileNodeArrowDirection.Right);
}
}
}
//画文字
e.Graphics.DrawString(e.Node.Text, f, new SolidBrush(Color.Black), e.Bounds.X + i缩进, e.Bounds.Y + Convert.ToInt32((e.Bounds.Height - f.GetHeight()) / 2 + 1));
}