是否可以通过编程方式创建TShape控件?

时间:2013-04-27 10:27:39

标签: delphi canvas

我想知道是否有某种方法可以在运行时以编程方式创建TShape控件。例如,放置100个形状,隐藏它们,当程序运行时,显示它们,可以创建100个形状一段时间(5个形状在5秒内创建,10个在10秒内,15个在15秒内,依此类推)

1 个答案:

答案 0 :(得分:3)

您应该使用控件绘制和制作动画。相反,您应该使用普通GDI或其他API手动绘制。有关示例,请参阅this examplethis example from one of your questions

无论如何,对您的问题做出简单回答:在表单上放置TTimer并将其Interval设置为250,然后写一下:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls;

type
  TForm1 = class(TForm)
    Timer1: TTimer;
    procedure Timer1Timer(Sender: TObject);
  private
    { Private declarations }
    FShapes: array of TShape;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Timer1Timer(Sender: TObject);
begin
  SetLength(FShapes, Length(FShapes) + 1); // Ugly!
  FShapes[high(FShapes)] := TShape.Create(Self);
  FShapes[high(FShapes)].Parent := Self;
  FShapes[high(FShapes)].Width := Random(100);
  FShapes[high(FShapes)].Height := Random(100);
  FShapes[high(FShapes)].Left := Random(Width - FShapes[high(FShapes)].Width);
  FShapes[high(FShapes)].Top := Random(Height - FShapes[high(FShapes)].Height);
  FShapes[high(FShapes)].Brush.Color := RGB(Random(255), Random(255), Random(255));
  FShapes[high(FShapes)].Shape := TShapeType(random(ord(high(TShapeType))))
end;

end.