unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls, ExtCtrls,
BGRABitmap, BGRABitmapTypes;
type
{ TForm1 }
TForm1 = class(TForm)
Button1: TButton;
Panel1: TPanel;
procedure Button1Click(Sender: TObject);
procedure FormMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
procedure FormMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
procedure FormPaint(Sender: TObject);
private
{ private declarations }
public
ssbmp: TBGRABitmap;
DS: TPoint;
DR: TRect;
MD: boolean;
procedure CalculateDR(X, Y: integer);
end;
var
Form1: TForm1;
implementation
{$R *.lfm}
{ TForm1 }
procedure TForm1.Button1Click(Sender: TObject);
begin
ssbmp.Free;
ssbmp := TBGRABitmap.Create(Monitor.Width, Monitor.Height);
ssbmp.TakeScreenshotOfPrimaryMonitor;
Invalidate;
end;
procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
MD := True;
with DR do
begin
Left := X;
Top := Y;
Right := Left;
Bottom := Top;
end;
DS.X := DR.Left;
DS.Y := DR.Top;
end;
procedure TForm1.FormMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
begin
if not MD then
Exit;
CalculateDR(X, Y);
Invalidate;
end;
procedure TForm1.FormMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
MD := False;
Invalidate;
end;
procedure TForm1.FormPaint(Sender: TObject);
var
bmp: TBGRABitmap;
regionbmp:TBGRABitmap;
begin
if not Assigned(ssbmp) then
Exit;
bmp := TBGRABitmap.Create(Width, Height);
bmp.PutImage(0, 0, ssbmp, dmSet);
if MD then
begin
bmp.Rectangle(DR.Left, DR.Top, DR.Right, DR.Bottom, BGRA(255, 0, 0, 200), dmDrawWithTransparency);
regionbmp:=bmp.GetPart(DR) as TBGRABitmap;
if Assigned(regionbmp) then
regionbmp.Draw(Panel1.Canvas,0,0);
//Do what you want here, if you want just save it after selection is done do this stuff in mouseup
regionbmp.Free;
end;
bmp.Draw(Canvas, 0, 0);
bmp.Free;
end;
procedure TForm1.CalculateDR(X, Y: integer);
begin
if x >= DS.x then
begin
DR.Left := DS.x;
DR.Right := x;
end
else
begin
DR.Right := DS.x;
DR.Left := x;
end;
if y >= DS.y then
begin
DR.Top := DS.y;
DR.Bottom := y;
end
else
begin
DR.Bottom := DS.y;
DR.Top := y;
end;
end;
end.