如何改变treenode rect size?

时间:2014-09-29 11:02:20

标签: delphi

我希望我的树视图显示为folows。enter image description here

这是我正在使用的代码。

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;

问题是我可以扩展R.right但不能扩展R.left ......

1 个答案:

答案 0 :(得分:2)

如果您在OnAdvancedCustomDrawItem事件中进行绘图,则有更多选项。

这对我来说(过滤掉cdPostPaint舞台):

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