If you use TGraphicControl as ancestor for your objects, you can get them to tell you their name when clicked. You don't need to mess with mouse coordinates or control boundaries at all. Here's a very basic example:
unit mainShapeClick;
{$mode objfpc}{$H+}
interface
uses
Classes, Forms, Controls, Graphics;
type
{ TSquare }
TSquare = class(TGraphicControl)
protected
procedure Paint; override;
procedure MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
override;
public
constructor Create(AOwner: TComponent); override;
end;
{ TCircle }
TCircle = class(TGraphicControl)
protected
procedure Paint; override;
procedure MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
override;
public
constructor Create(AOwner: TComponent); override;
end;
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
private
sq: TSquare;
circ: TCircle;
end;
var
Form1: TForm1;
implementation
{$R *.lfm}
{ TCircle }
procedure TCircle.Paint;
begin
Canvas.Brush.Color:=Color;
Canvas.Ellipse(ClientRect);
inherited Paint;
end;
procedure TCircle.MouseDown(Button: TMouseButton; Shift: TShiftState; X,
Y: Integer);
begin
inherited MouseDown(Button, Shift, X, Y);
(Owner as TForm).Caption:='You clicked on ' + Name;
end;
constructor TCircle.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
SetInitialBounds(70, 70, 50, 50);
end;
{ TSquare }
procedure TSquare.Paint;
begin
Canvas.Brush.Color:=Color;
Canvas.FillRect(ClientRect);
Canvas.Frame(ClientRect);
inherited Paint;
end;
procedure TSquare.MouseDown(Button: TMouseButton; Shift: TShiftState; X,
Y: Integer);
begin
inherited MouseDown(Button, Shift, X, Y);
(Owner as TForm).Caption:='You clicked on ' + Name;
end;
constructor TSquare.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
SetInitialBounds(10, 10, 50, 50);
end;
{ TForm1 }
procedure TForm1.FormCreate(Sender: TObject);
begin
sq:=TSquare.Create(Self);
sq.Color:=clMoneyGreen;
sq.Name:='Square1';
sq.Parent:=Self;
circ:=TCircle.Create(Self);
circ.Color:=clSkyBlue;
circ.Name:='Circle1';
circ.Parent:=Self;
end;
end.