外观
TreeView重绘
效果图
![[Pasted image 20230921172232.png]]
代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace DiyControl
{
public partial class TreeViewDetailEx : TreeView
{
Color fontColor = ColorTranslator.FromHtml("#428EFF");
Dictionary<string, Image> imglist;
public TreeViewDetailEx()
{
InitializeComponent();
this.FullRowSelect = true;
this.ShowPlusMinus = false;
this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
this.DrawMode = TreeViewDrawMode.OwnerDrawAll;
imglist = new Dictionary<string, Image>
{
{"红细胞系统",Properties.Resources.hong},
{"巨核细胞及血小板",Properties.Resources.xuexiaoban},
{"粒细胞系统",Properties.Resources.lixibao },
{"淋巴细胞系统",Properties.Resources.lingba},
{"其它细胞",Properties.Resources.other},
{"骨髓红系分类",Properties.Resources.hong},
{"骨髓粒系分类",Properties.Resources.lixibao},
{"骨髓淋巴及浆细胞系分类",Properties.Resources.lingba},
{"单核细胞系分类",Properties.Resources.danhe},
{"巨核细胞系分类",Properties.Resources.xuexiaoban},
{"其他细胞系分类",Properties.Resources.other},
{"未审核",Properties.Resources.weishenghe},
};
}
int image_left = 0;// 图标之后的位置
int py_left = 10; //左边点
int py_top = 15;
protected override void OnDrawNode(DrawTreeNodeEventArgs e)
{
Debug.WriteLine(e.Node.Bounds + "" + e.Bounds);
string nodeText = e.Node.Text;
using (var g = e.Graphics)
{
if (e.Node.Level == 0)
{
g.FillRectangle(new SolidBrush(ColorTranslator.FromHtml("#EDF4FF")), new Rectangle(e.Bounds.Left, e.Bounds.Top + ((e.Bounds.Height - 54) / 2), e.Bounds.Width, 54));//整个背景的颜色
/***********节点展开收起的图标***************/
Image expanded_iamge = Properties.Resources.expanded;
int e_center_height = e.Node.Bounds.Top + ((e.Bounds.Height - expanded_iamge.Height) / 2);
if (e.Node.IsExpanded)
g.DrawImage(expanded_iamge, py_left, e_center_height);
else if (e.Node.Nodes.Count > 0 && !e.Node.IsExpanded)
g.DrawImage(Properties.Resources.expand, py_left, e_center_height);
/***********节点图标***************/
string ikey = imglist.ContainsKey(e.Node.Tag.ToString()) ? e.Node.Tag.ToString() : "其它细胞";
Image titleimg = imglist[ikey];
image_left = titleimg.Width + py_left;
int center_height = e.Node.Bounds.Top + ((e.Bounds.Height - titleimg.Height) / 2);
g.DrawImage(titleimg, image_left, center_height);
g.DrawString(nodeText, new Font("等线", (float)Model.SysParam.pageScaleWidth * 16, FontStyle.Bold), new SolidBrush(Color.Black), this.Indent + image_left + this.Indent, center_height + 4);
}
else if (e.Node.Level == 1)
{
/***********节点展开收起的图标***************/
//g.DrawLine(new Pen(new SolidBrush(Color.Red), 0.5f), new Point(0, e.Bounds.Top), new Point(e.Bounds.Width, e.Bounds.Top));
Image expanded_iamge = Properties.Resources.expanded;
int e_center_height = e.Node.Bounds.Top + ((e.Bounds.Height - expanded_iamge.Height) / 2);
if (e.Node.IsExpanded)
g.DrawImage(expanded_iamge, image_left + e.Node.Level * this.Indent, e_center_height);
else if (e.Node.Nodes.Count > 0 && !e.Node.IsExpanded)
g.DrawImage(Properties.Resources.expand, image_left + e.Node.Level * this.Indent, e_center_height);
g.DrawString(nodeText,
new Font("等线", (float)Model.SysParam.pageScaleWidth * 15),
new SolidBrush(ColorTranslator.FromHtml("#4E576A")),
image_left + expanded_iamge.Width + py_left + e.Node.Level * this.Indent, e_center_height - 3);//图片太小 -3
}
else if (e.Node.Level == 2)
{
g.DrawString("• 其中 " + nodeText,
new Font("等线", (float)(Model.SysParam.pageScaleWidth * 13.5)),
new SolidBrush(fontColor),
image_left + py_left + e.Node.Level * this.Indent, e.Node.Bounds.Top + ((e.Bounds.Height - 25) / 2));
}
}
}
protected override void OnMouseClick(MouseEventArgs e)
{
TreeNode treeNode = this.GetNodeAt(e.Location);
if (treeNode != null)
{
//Debug.WriteLine(image_left + (treeNode.Level * this.Indent) + "," + e.Location + "," + iamgerect + "," + treeNode.Bounds);
Rectangle iamgerect = treeNode.Bounds;
if (treeNode.Level == 2) return;
else if (treeNode.Level == 0) iamgerect = new Rectangle(10, treeNode.Bounds.Top + (treeNode.Bounds.Height - 12) / 2, 12, 12);
else if (treeNode.Level == 1 && treeNode.Nodes.Count > 0) iamgerect = new Rectangle(image_left + (treeNode.Level * this.Indent), treeNode.Bounds.Top + (treeNode.Bounds.Height - 12) / 2, 12, 12);
if (iamgerect.Contains(e.Location))//判断是否点击到图标
{
if (treeNode.IsExpanded) treeNode.Collapse();
else treeNode.Expand();
}
}
}
protected override void OnMouseDoubleClick(MouseEventArgs e)
{
TreeNode treeNode = this.GetNodeAt(e.Location);
if (treeNode != null)
{
if (!treeNode.IsExpanded) treeNode.Expand();
else treeNode.Collapse();
}
}
protected override CreateParams CreateParams
{//防止展开菜单闪烁
get
{
CreateParams cp = base.CreateParams;
if (!DesignMode) cp.ExStyle |= 0x02000000;
return cp;
}
}
}
}