0

I wonder if there is some way to create TShape controls programmatically during runtime. For example, insteed of putting 100 shapes, hide them and when the program runs, show them, 100 shapes could be created over some time (5 shapes created in 5 seconds, 10 in 10 seconds, 15 in 15 seconds, and so on).

3
  • Something like: procedure TForm.Timer1Timer(Sender: TObject); begin With Tshape.Create(self) do begin Parent := self; Left := xxx end; end; ?? Commented Apr 27, 2013 at 10:38
  • Yes, something like that Commented Apr 27, 2013 at 11:35
  • GExperts and CnWizards have button to convert any visual component into code. Perhaps such questions "how make VCL components a code" are all be considered duplicate... Commented Apr 29, 2013 at 13:28

1 Answer 1

3

You should not draw and animate by using controls. Instead, you should draw manually using plain GDI or some other API. For an example, see this example or this example from one of your questions.

Anyhow, a simple answer to your question: Put a TTimer on your form and set its Interval to 250, and write:

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.
Sign up to request clarification or add additional context in comments.

1 Comment

@user2296565: It's all there. It's a private field of the form class.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.