日期:2014-05-18 浏览次数:21546 次
public class Form1 : Form
{
public Form1()
{
InitializeComponent();
//自已绘制
this.treeView1.DrawMode = TreeViewDrawMode.OwnerDrawText;
this.treeView1.DrawNode+=new DrawTreeNodeEventHandler(treeView1_DrawNode);
}
//在绘制节点事件中,按自已想的绘制
private void treeView1_DrawNode(object sender, DrawTreeNodeEventArgs e)
{
if ((e.State & TreeNodeStates.Selected) != 0)
{
//演示为绿底白字
e.Graphics.FillRectangle(Brushes.Green,e.Node.Bounds);
Font nodeFont = e.Node.NodeFont;
if (nodeFont == null) nodeFont = ((TreeView)sender).Font;
e.Graphics.DrawString(e.Node.Text, nodeFont, Brushes.White,
Rectangle.Inflate(e.Bounds, 2, 0));
}
else
{
e.DrawDefault = true;
}
if ((e.State & TreeNodeStates.Focused) != 0)
{
using (Pen focusPen = new Pen(Color.Black))
{
focusPen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;
Rectangle focusBounds = e.Node.Bounds ;
focusBounds.Size = new Size(focusBounds.Width - 1,
focusBounds.Height - 1);
e.Graphics.DrawRectangle(focusPen, focusBounds);
}
}
}
}
------解决方案--------------------
给你个遮盖的方法把
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
TreeView _TreeView = (TreeView)sender;
Control _SetLabel = _TreeView.Controls["SelectLab"];
if (_SetLabel == null)
{
Label _Label = new Label();
_Label.Name = "SelectLab";
_Label.AutoSize = false;
_Label.BackColor = Color.Yellow;
_Label.ForeColor = Color.Red;
_Label.Font = _TreeView.Font;
_TreeView.Controls.Add(_Label);
_SetLabel = _Label;
}
_SetLabel.Size = new Size(e.Node.Bounds.Width + 5, e.Node.Bounds.Height);
_SetLabel.Location = new Point(e.Node.Bounds.X, e.Node.Bounds.Y);
_SetLabel.Text = e.Node.Text;
}