給你來個詳細的.(好像TShape沒有onclick事件啊)!
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;
type
TForm1 = class(TForm)
Draw: TButton;
Clear: TButton;
create: TButton;
procedure DrawClick(Sender: TObject);
procedure ClearClick(Sender: TObject);
procedure createClick(Sender: TObject);
procedure ShapeMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.DrawClick(Sender: TObject);
begin
self.Canvas.MoveTo(20,10);
self.Canvas.LineTo(50,40);
end;
procedure TForm1.ClearClick(Sender: TObject);
var
T:TRect;
TL:TPoint;
BR:TPoint;
begin
TL.X:=20;
TL.Y:=10;
BR.X:=50;
BR.Y:=40;
T.TopLeft:=TL;
T.BottomRight:=BR;
self.Canvas.FillRect(T);
end;
procedure TForm1.createClick(Sender: TObject);
var
S:TShape;
begin
S:=TShape.Create(Self);
S.Top:=60;
S.Left:=40;
S.Tag:=1;
S.Parent:=Self;
S.OnMouseDown:=ShapeMouseDown;
S:=TShape.Create(Self);
S.Top:=80;
S.Left:=150;
S.Tag:=2;
S.Parent:=Self;
S.OnMouseDown:=ShapeMouseDown;
end;
procedure TForm1.ShapeMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
case (Sender as TShape).Tag of
1:showmessage('This is the First Shape!');
2:showmessage('This is the Secend Shape!');
end;
end;
end.