0

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

4

1 回答 1

3

您不应该使用控件来绘制和动画。相反,您应该使用普通的 GDI 或其他一些 API 手动绘制。有关示例,请参阅此示例您的问题之一中的此示例

无论如何,对您的问题的简单回答:TTimer在您的表单上放置 a 并将其设置Interval250,然后编写:

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.
于 2013-04-27T10:41:36.487 回答