Azad
Azad

Reputation: 5274

how to change the treenode rect size?

i want my tree view shown as folows.enter image description here

this is the code i am using.

procedure TForm1.FormShow(Sender: TObject);
begin
  TreeView1.FullExpand
end;

procedure TForm1.TreeView1CustomDrawItem(Sender: TCustomTreeView;
  Node: TTreeNode; State: TCustomDrawState; var DefaultDraw: Boolean);

const
  _Right = 100;
  _Left = 50;

var
  R : TRect;

begin

 if Node.Level = 0 then
 begin

  R := Node.DisplayRect(true);

  R.Right := R.Right + _Right;
  R.Left := R.Left + _Left;

  Sender.Canvas.Brush.Color := clRed;
  Sender.Canvas.FillRect(R);

 end;

end;

the problem is i can extend the R.right but cannot extend the R.left...

Upvotes: 2

Views: 1517

Answers (1)

LU RD
LU RD

Reputation: 34909

If you do the drawing in theOnAdvancedCustomDrawItem event, there are more options.

This does it for me (filtering out the cdPostPaint Stage):

procedure TForm1.TreeView1AdvancedCustomDrawItem(Sender: TCustomTreeView;
  Node: TTreeNode; State: TCustomDrawState; Stage: TCustomDrawStage;
  var PaintImages, DefaultDraw: Boolean);
const
  _Right = 100;
  _Left = 50;
var
  R : TRect;
begin
 if (Node.Level = 0) and (stage = cdPostPaint) then
 begin

  R := Node.DisplayRect(true);
  R.Right := R.Right + 2; 
  Sender.Canvas.FillRect(R); // Just clear default text area

  R.Right := R.Right + _Right;
  R.Left := R.Left + _Left;
  Sender.Canvas.Brush.Color := clRed;
  Sender.Canvas.FillRect(R);
  {- Make sure writing text with transparent background }
  SetBkMode( Sender.Canvas.Handle, TRANSPARENT );
  Sender.Canvas.TextOut(R.Left,R.Top,Node.Text);
  PaintImages := true;
  DefaultDraw := false;
 end;    
end;

enter image description here

Upvotes: 2

Related Questions