2013-04-27 46 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

相关问题