windform 重绘Treeview "+-"号图标
程序员文章站
2022-03-09 13:46:37
模仿wind系统界面,重绘Treeview + - 号图标 一,首先需要图片 ,用于替换原有的 +-号 二、新建Tree扩展类 TreeViewEx继承TreeView 生成后拖动控件到界面中,实际效果如下 ......
模仿wind系统界面,重绘treeview + - 号图标
一,首先需要图片 ,用于替换原有的 +-号
二、新建tree扩展类 treeviewex继承treeview
using system;
using system.collections.generic;
using system.drawing;
using system.linq;
using system.text;
using system.windows.forms;
/*******************************************************************
* copyright (c) 版权所有
* 文件名称:treeviewex
* 命名空间:testrecentmenu
* 创建时间:2018/12/18 16:49:08
* 作 者: wangyonglai
* 描 述:
* 修改记录:
* 修改人:
* 版 本 号:v1.0.0
**********************************************************************/
namespace testrecentmenu
{
public class treeviewex : treeview
{
private bool arrowkeyup = false;
private bool arrowkeydown = false;
private system.windows.forms.imagelist arrowimagelist1;
/*1节点被选中 ,treeview有焦点*/
private solidbrush brush1 = new solidbrush(color.fromargb(209, 232, 255));//填充颜色
private pen pen1 = new pen(color.fromargb(102, 167, 232), 1);//边框颜色
/*2节点被选中 ,treeview没有焦点*/
private solidbrush brush2 = new solidbrush(color.fromargb(247, 247, 247));
private pen pen2 = new pen(color.fromargb(222, 222, 222), 1);
/*3 mousemove的时候 画光标所在的节点的背景*/
private solidbrush brush3 = new solidbrush(color.fromargb(229, 243, 251));
private pen pen3 = new pen(color.fromargb(112, 192, 231), 1);
public const int wm_printclient = 0x0318;
public const int prf_client = 0x00000004;
//替换+-号图标的imagelist
public imagelist arrowimagelist
{
get
{
return arrowimagelist1;
}
set
{
arrowimagelist1 = value;
}
}
public treeviewex()
{
//双缓存防止屏幕抖动
//this.setstyle(controlstyles.userpaint, true);
this.setstyle(controlstyles.optimizeddoublebuffer | controlstyles.allpaintinginwmpaint | controlstyles.doublebuffer, true);
this.updatestyles();
this.drawmode = treeviewdrawmode.ownerdrawall;
this.fullrowselect = true;
this.hottracking = true;
this.hideselection = false;
//this.showlines = true;
this.itemheight = 20;
}
protected override void ondrawnode(drawtreenodeeventargs e)
{
base.ondrawnode(e);
#region 1 选中的节点背景=========================================
rectangle noderect = new rectangle(1, e.bounds.top, e.bounds.width - 3, e.bounds.height - 1);
if (e.node.isselected)
{
if (this.focused)
{
e.graphics.fillrectangle(brush1, noderect);
e.graphics.drawrectangle(pen1, noderect);
}
else
{
e.graphics.fillrectangle(brush2, noderect);
e.graphics.drawrectangle(pen2, noderect);
}
}
else if ((e.state & treenodestates.hot) != 0 && e.node.text != "")//|| currentmousemovenode == e.node)
{
e.graphics.fillrectangle(brush3, noderect);
e.graphics.drawrectangle(pen3, noderect);
}
else
{
e.graphics.fillrectangle(brushes.white, e.bounds);
}
#endregion
#region 2 +-号绘制=========================================
rectangle plusrect = new rectangle(e.node.bounds.left - 32, noderect.top + 6, 9, 9); // +-号的大小 是9 * 9
if (e.node.isexpanded)
e.graphics.drawimage(arrowimagelist.images[1], plusrect);
else if (e.node.isexpanded == false && e.node.nodes.count > 0)
e.graphics.drawimage(arrowimagelist.images[0], plusrect);
/*测试用 画出+-号出现的矩形*/
//if (e.node.nodes.count > 0)
// e.graphics.drawrectangle(new pen(color.red), plusrect);
#endregion
#region 3 画节点文本=========================================
rectangle nodetextrect = new rectangle(
e.node.bounds.left,
e.node.bounds.top + 4,
e.node.bounds.width + 2,
e.node.bounds.height
);
nodetextrect.width += 4;
nodetextrect.height -= 4;
e.graphics.drawstring(e.node.text,
e.node.treeview.font,
new solidbrush(color.black),
nodetextrect);
//画子节点个数 (111)
if (e.node.getnodecount(true) > 0)
{
e.graphics.drawstring(string.format("({0})", e.node.getnodecount(true)),
new font("arial", 8),
brushes.gray,
nodetextrect.right - 4,
nodetextrect.top -2);
}
///*测试用,画文字出现的矩形*/
//if (e.node.text != "")
// e.graphics.drawrectangle(new pen(color.blue), nodetextrect);
#endregion
#region 4 画iimagelist 中的图标===================================================================
int currt_x = e.node.bounds.x;
if (this.imagelist != null && this.imagelist.images.count > 0)
{
//图标大小16*16
rectangle imagebox = new rectangle(
e.node.bounds.x - 3 - 16,
e.node.bounds.y + 2,
16,//imagelist image width
16);//height
int index = e.node.imageindex;
string imagekey = e.node.imagekey;
if (imagekey != "" && this.imagelist.images.containskey(imagekey))
e.graphics.drawimage(this.imagelist.images[imagekey], imagebox);
else
{
if (e.node.imageindex < 0)
index = 0;
else if (index > this.imagelist.images.count - 1)
index = 0;
e.graphics.drawimage(this.imagelist.images[index], imagebox);
}
currt_x -= 19;
/*测试 画imagelist的矩形*/
//if (e.node.imageindex > 0)
// e.graphics.drawrectangle(new pen(color.black, 1), imagebox);
}
#endregion
}
protected override void onbeforeselect(treeviewcanceleventargs e)
{
base.onbeforeselect(e);
if (e.node != null)
{
//禁止选中空白项
if (e.node.text == "")
{
//响应上下键
if (arrowkeyup)
{
if (e.node.prevnode != null && e.node.prevnode.text != "")
this.selectednode = e.node.prevnode;
}
if (arrowkeydown)
{
if (e.node.nextnode != null && e.node.nextnode.text != "")
this.selectednode = e.node.nextnode;
}
e.cancel = true;
}
}
}
/// <summary>
/// 防止在选择设,treenode闪屏
/// </summary>
protected override createparams createparams
{
get
{
createparams cp = base.createparams;
if (!designmode)
{
cp.exstyle |= 0x02000000;// turn on ws_ex_composited
}
return cp;
}
}
}
}
生成后拖动控件到界面中,实际效果如下
上一篇: 表分区(转载)