gion 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;
}
}
}
}
生成后拖动控件到界面中,实际效果如下
|