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).
Asked
Active
Viewed 3,610 times
0

Andreas Rejbrand
- 105,602
- 8
- 282
- 384

user2296565
- 243
- 1
- 7
- 18
-
Something like: procedure TForm.Timer1Timer(Sender: TObject); begin With Tshape.Create(self) do begin Parent := self; Left := xxx end; end; ?? – bummi Apr 27 '13 at 10:38
-
Yes, something like that – user2296565 Apr 27 '13 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... – Arioch 'The Apr 29 '13 at 13:28
1 Answers
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.

Community
- 1
- 1

Andreas Rejbrand
- 105,602
- 8
- 282
- 384
-
@user2296565: It's all there. It's a private field of the form class. – Andreas Rejbrand Apr 27 '13 at 10:46