Use BeforeDraw and AfterDraw events to add custom graphics to the tree:
procedure TCustomDrawForm.Tree1BeforeDraw(Sender: TObject);var t: Integer;begin // Draw custom background graphics before nodes are rendered with Tree1.Canvas do begin Pen.Style := psSolid; Pen.Color := clRed; Pen.Width := 2; // Draw random lines from bottom-right corner for t := 1 to 10 do LineWithZ(Tree1.Width, Tree1.Height, Tree1.Width - (t * Random(40)), Tree1.Height - (t * Random(30)), 0); end;end;procedure TCustomDrawForm.Tree1AfterDraw(Sender: TObject);var t: Integer;begin // Draw custom graphics after nodes are rendered with Tree1.Canvas do begin Pen.Style := psSolid; Pen.Color := clBlack; Pen.Width := 2; // Draw random lines from top-left corner for t := 1 to 10 do LineWithZ(0, 0, t * Random(40), t * Random(30), 0); end;end;
Create completely custom node shapes by inheriting from TTreeNodeShape:
type // Global formatting options for custom shapes TSpecialShapeFormat = class public MaxLevel: Integer; VertOffset: Integer; Brush: TTeeBrush; Border: TTeePen; constructor Create; destructor Destroy; override; end; // Custom shape with special drawing behavior TSpecialShape = class(TTreeNodeShape) public constructor Create(AOwner: TComponent); override; procedure Draw; override; class function Format: TSpecialShapeFormat; end;constructor TSpecialShape.Create(AOwner: TComponent);begin inherited; Transparent := True; Border.Visible := False;end;// Custom drawing logicprocedure TSpecialShape.Draw;var R: TRect;begin // Only draw for nodes at specified level if Level <= Format.MaxLevel then with Tree.Canvas do begin AssignBrush(Format.Brush); AssignVisiblePen(Format.Border); R := Bounds; // Draw a full-width rectangle behind the node Rectangle(0, R.Top, Tree.ClientWidth, R.Bottom + Format.VertOffset); end; // Draw node text, image, etc. as usual inherited;end;
procedure TCustomShapeForm.FormCreate(Sender: TObject);begin // Set custom shape as default for new nodes Tree1.GlobalFormat.NodeClass := TSpecialShape; // Add sample nodes (will use TSpecialShape) Tree1.Add('Africa').Add('Kenya'); Tree1.Add('America').Add('USA').Add('Los Angeles'); Tree1.Add('Asia').Add('Japan').Add('Tokyo'); // Explicitly create a node with custom shape class Tree1.AddShapeClass(0, 0, 'Universe', nil, TSpecialShape);end;
uses TeeEdiGrad;procedure TNodeGradientForm.FormCreate(Sender: TObject);begin // Show selected nodes with their original gradient Tree1.Selected.Color := clNone;end;procedure TNodeGradientForm.Button1Click(Sender: TObject);begin // Edit gradient for selected node if Tree1.Selected.Count > 0 then TTeeGradientEditor.Edit(Self, Tree1.Selected.First.Gradient);end;procedure TNodeGradientForm.CheckBox1Click(Sender: TObject);var t: Integer;begin // Enable/disable gradient clipping for all nodes for t := 0 to Tree1.Shapes.Count - 1 do Tree1[t].GradientClip := CheckBox1.Checked;end;
procedure TSpecialShape.Draw;begin // Different appearance for different tree levels case Level of 0: Format.Brush.Color := clLime; 1: Format.Brush.Color := clAqua; 2: Format.Brush.Color := clYellow; end; inherited;end;