unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
ExtCtrls;
type
{ TForm1 }
TForm1 = class(TForm)
Label1: TLabel;
Shape1: TShape;
Shape2: TShape;
procedure Shape1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure Shape1MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer
);
procedure Shape1MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
private
function isInsideTheBox(X, Y: Integer): Boolean;
end;
var
Form1: TForm1;
isLeftClicking: Boolean = False;
orgX, orgY: Integer;
implementation
{$R *.lfm}
{ TForm1 }
procedure TForm1.Shape1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
if (Button <> mbLeft) then Exit;
orgX := X;
orgY := Y;
isLeftClicking := True;
end;
procedure TForm1.Shape1MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
begin
if not(isLeftClicking) then Exit;
Shape1.Left := Shape1.Left + X-orgX;
Shape1.Top := Shape1.Top + Y-orgY;
if isInsideTheBox
(Shape1.Left + Shape1.Width div 2, Shape1.Top + Shape1.Height div 2) then
Shape1.Brush.Color := clGreen
else
Shape1.Brush.Color := clRed;
end;
procedure TForm1.Shape1MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
if (Shape1.Brush.Color = clGreen) then Halt;
Shape1.Brush.Color := clSkyBlue;
isLeftClicking := False;
end;
function TForm1.isInsideTheBox(X, Y: Integer): Boolean;
begin
Result := True;
if (X < Shape2.Left) or (X > Shape2.Left + Shape2.Width) or
(Y < Shape2.Top) or (Y > Shape2.Top + Shape2.Height) then
Result := False;
end;
end.