运行本程序前先在FORM上放两个SHAPE。
unit usndth;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ExtCtrls, StdCtrls, Buttons;
type
TBallThread = class(TThread)
private
shap : Tshape;
x,y : integer;
maxx,maxy : integer;
procedure moveball;
// property
protected
procedure execute;override;
public
constructor create(s:tshape;w,h,i,j:integer);
end;
TfMain = class(TForm)
Shape1: TShape;
Shape2: TShape;
BitBtn1: TBitBtn;
BitBtn2: TBitBtn;
procedure BitBtn1Click(Sender: TObject);
procedure BitBtn2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
fMain: TfMain;
b1,b2 : TballThread;
implementation
{$R *.DFM}
constructor TballThread.create(s:tshape;w,h,i,j:integer);
begin
Inherited create(false);
shap := s;
x := i;
y := j;
maxx :=w;
maxy :=h;
FreeOnterminate := True;
end;
procedure TballThread.moveball;
begin
with shapdo
begin
if (left <=0) or (Width+left > maxx) then
x := x * -1;
if (top <=0) or (top + height > maxy) then
y := y * -1;
left := left + x;
top := top + y;
end;
end;
procedure TballThread.execute ;
begin
while not terminateddo
Synchronize(moveball);
end;
procedure TfMain.BitBtn1Click(Sender: TObject);
begin
b1 := TballThread.create(shape1,fmain.Width ,fmain.Height ,1,1);
end;
procedure TfMain.BitBtn2Click(Sender: TObject);
begin
b2 := TballThread.create(shape2,fmain.width,height,2,2);
end;
end.