Selection Rect

Download project source here: selectionrect

unit Unit1;

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
  FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs,
  FMX.Controls.Presentation, FMX.StdCtrls;

type
  TForm1 = class(TForm)
    Panel1: TPanel;
    procedure Panel1MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Single);
    procedure Panel1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Single);
    procedure Panel1Paint(Sender: TObject; Canvas: TCanvas; const ARect: TRectF);
    procedure Panel1MouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Single);
    procedure Panel1MouseLeave(Sender: TObject);
  private
    Drawing: boolean;
    AnchorX: single;
    AnchorY: single;
    TargetX: single;
    TargetY: single;
    procedure Invalidate;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.fmx}

procedure TForm1.Panel1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Single);
begin
  Drawing := True;
  AnchorX := X;
  AnchorY := Y;
end;

procedure TForm1.Invalidate;
begin
  Panel1.InvalidateRect(Panel1.ClipRect);
end;

procedure TForm1.Panel1MouseLeave(Sender: TObject);
begin
  if Drawing then begin
    Drawing := False;
    Invalidate;
  end;
end;

procedure TForm1.Panel1MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Single);
var
  myRect : TRectF;
  thePoints: TPointF;
begin
  if not Drawing then begin
    exit;
  end;
  TargetX := X;
  TargetY := Y;
  Invalidate;
end;

procedure TForm1.Panel1MouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Single);
begin
  Drawing := False;
  Invalidate;
end;

procedure TForm1.Panel1Paint(Sender: TObject; Canvas: TCanvas; const ARect: TRectF);
var
  RenderRect: TRectF;
begin
  if not Drawing then begin
    exit;
  end;
  RenderRect.TopLeft := Panel1.LocalToAbsolute(PointF(AnchorX,AnchorY));
  RenderRect.BottomRight := Panel1.LocalToAbsolute(PointF(TargetX,TargetY));
  RenderRect.TopLeft := PointF(AnchorX,AnchorY);
  RenderRect.BottomRight := PointF(TargetX,TargetY);
  panel1.Canvas.BeginScene();
  panel1.Canvas.DrawDashRect(RenderRect,0,0,allcorners,1,talphacolors.Black);
  panel1.Canvas.EndScene;
end;

end.
Print Friendly, PDF & Email