interface
uses
winprocs,classes,graphics,extctrls;
type
tbouncethread=class(tthread)
private
fshape:tshape;
fxspeed:integer;
fyspeed:integer;
procedure moveshape;
protected
procedure execute;override;
public
constructor create(suspended:boolean;shape:tshape;xspeed,yspeed:integer);
propertyshape:tshapereadfshape;
end;
implementation
procedure tbouad.moveshape;
var
maxheight,maxwidth:integer;
begin
with fshape do
begin
left:=left+fxspeed;
top:=top+fyspeed;
if(left=0) or(left+width=parent.width)then
fxspeed:=fxspeed*-1;
if(top=0)or(top=height+parent.height)then
fyspeed:=fyspeed*-1;
end;
end;
procedure tbouncethread.execute;
begin
while not terminated do
begin
synchronize(moveshape);
end;
end;
constructor tbouncethread.create(suspended:boolean;shape:tshape;
xspeed,yspeed:integer);
begin
inherited create(suspended);
fshape:=shape;
fxspeed:=xspeed;{x軸走向的速度}
fyspeed:=yspeed;{y軸走向的速度}
freeonterminate:=true;
end;
end.
執行時我們可以新建壹個程序,然後在uses部分加入以上的bncthrd單元,再在它的窗體form1上加入兩個shape控件shape1和shape2,shape1可以是壹個矩形而shape2
是壹個圓。加入以下的代碼就可以讓矩形和圓動起來。
unit Unit1;
interface
uses
bncthrd,Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;
type
TForm1 = class(TForm)
Shape1: TShape;
Shape2: TShape;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.Button1Click(Sender: TObject);
begin
tbouncethread.create(false,shape1,1,2);
tbouncethread.create(false,shape2,2,3);
end;
end.