using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
namespace WindowsApplication12
{
class TreeViewEx : System.Windows.Forms.TreeView
{
const int WM_PAINT = 15;
public TreeViewEx()
{
this.DrawMode = TreeViewDrawMode.OwnerDrawText;
}
private bool enabled = true;
public new bool Enabled
{
get { return enabled; }
set
{
enabled = value;
this.Invalidate();
}
}
protected override void OnBeforeSelect(TreeViewCancelEventArgs e)
{
base.OnBeforeSelect(e);
e.Cancel = !enabled;
}
protected override void OnBeforeExpand(TreeViewCancelEventArgs e)
{
base.OnBeforeExpand(e);
e.Cancel = !enabled;
}
protected override void OnBeforeCollapse(TreeViewCancelEventArgs e)
{
base.OnBeforeCollapse(e);
e.Cancel = !enabled;
}
protected override void OnPaint(PaintEventArgs e)
{
if (!enabled)
{
using (System.Drawing.Drawing2D.HatchBrush hb = new System.Drawing.Drawing2D.HatchBrush(System.Drawing.Drawing2D.HatchStyle.ForwardDiagonal, Color.Black, Color.LightGray))
{
e.Graphics.FillRectangle(hb, e.ClipRectangle);
}
}
else
{
base.OnPaint(e);
}
}
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
if (m.Msg == WM_PAINT && !enabled)
{
using (Graphics g = this.CreateGraphics())
{
using (System.Drawing.Drawing2D.HatchBrush hb = new System.Drawing.Drawing2D.HatchBrush(System.Drawing.Drawing2D.HatchStyle.ForwardDiagonal, Color.DarkGray, Color.Transparent))
{
g.FillRectangle(hb, this.ClientRectangle );
}
}
}
}
protected override void OnDrawNode(DrawTreeNodeEventArgs e)
{
if (e.Node.IsVisible)
{
e.Graphics.FillRectangle(Brushes.White, e.Bounds);
using (Pen p = new Pen(Color.DarkBlue))
{
if ((e.State & TreeNodeStates.Hot) == TreeNodeStates.Hot && enabled)
{
using (SolidBrush br = new SolidBrush(Color.CornflowerBlue))
{
e.Graphics.FillRectangle(br, e.Bounds);
}
e.Graphics.DrawRectangle(p, new Rectangle(e.Bounds.Location, new Size(e.Bounds.Width - 1, e.Bounds.Height - 1)));
}
if (e.Node == e.Node.TreeView.SelectedNode)
{
using (SolidBrush br = new SolidBrush(Color.LightBlue))
{
e.Graphics.FillRectangle(br, e.Bounds);
}
e.Graphics.DrawRectangle(p, new Rectangle(e.Bounds.Location, new Size(e.Bounds.Width, e.Bounds.Height - 1)));
}
}
using (SolidBrush br = new SolidBrush(this.ForeColor))
{
e.Graphics.DrawString(e.Node.Text, this.Font, br, e.Bounds.Left, e.Bounds.Top + 1);
}
}
}
}
}