2013-04-27 71 views
0

我不知道是否有某種方法可以在運行時以編程方式創建TShape控件。例如,保證放置100個形狀,隱藏它們並在程序運行時顯示它們,可以在一段時間內創建100個形狀(5個形狀創建5個形狀,10個10秒,15個15秒等等) 。是否可以通過編程創建TShape控件?

+0

是這樣的:程序TForm.Timer1Timer(發件人:TObject的); begin With Tshape.Create(self)do begin Parent:= self; Left:= xxx end; 結束; ?? – bummi 2013-04-27 10:38:18

+0

是的,類似的東西 – user2296565 2013-04-27 11:35:18

+0

GExperts和CnWizards有按鈕可以將任何可視化組件轉換爲代碼。也許這樣的問題「如何使VCL組件成爲代碼」都被認爲是重複的... – 2013-04-29 13:28:01

回答

3

您應該不是通過使用控件繪製和動畫。相反,你應該使用普通的GDI或其他API手動繪製。有關示例,請參見this examplethis example from one of your questions

總之,一個簡單的回答你的問題:你的窗體上放置一個TTimer並設置其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. 
+0

好的,但是什麼是Fshapes? – user2296565 2013-04-27 10:45:48

+0

@ user2296565:全都在那裏。它是表單類的一個私有字段。 – 2013-04-27 10:46:23

+0

好的,謝謝,我明白了 – user2296565 2013-04-27 10:50:49

相關問題