unit Unit1;
{$mode objfpc}{$H+}
interface
uses
{$IFDEF MSWindows}Windows,{$ENDIF} Classes, Forms, Graphics, StdCtrls, ExtCtrls;
type
{ TForm1 }
TForm1 = class(TForm)
Button1: TButton;
Image1: TImage;
procedure Button1Click(Sender: TObject);
private
public
end;
var
Form1: TForm1;
implementation
{$R *.lfm}
{ TForm1 }
procedure TForm1.Button1Click(Sender: TObject);
{$IFDEF MSWindows}
const
CAPTUREBLT = $40000000;
cmCaptureBlt = CAPTUREBLT; // fast snapshots
{$ENDIF}
var
{$IFDEF MSWindows}
LDC: HDC; // device context
LPal: PLogPalette; // backward compatible palette
{$ENDIF}
LCanvas: TCanvas; // internal drawing surface
LOrgBmp, // the bitmap used as source
LNewBmp: TBitmap; // destination bitmap
begin
{$IFDEF MSWindows}
// open device context
LDC := GetDCEx(GetDesktopWindow, 0, DCX_WINDOW or DCX_PARENTCLIP or DCX_CLIPSIBLINGS or DCX_CLIPCHILDREN);
try
{$ENDIF}
// create a canvas
LCanvas := TCanvas.Create;
try
// create a bitmap
LOrgBmp := TBitmap.Create;
try
{$IFDEF MSWindows}
// link surface with device context
LCanvas.Handle := LDC;
// setup bitmap basics
LOrgBmp.PixelFormat := TPixelFormat.pf24bit;
LOrgBmp.Width := Screen.Monitors[0].Width - Screen.Monitors[0].Left;
LOrgBmp.Height := Screen.Monitors[0].Height - Screen.Monitors[0].Top;
// if needed, get and add a palette
if (GetDeviceCaps(LDC, RASTERCAPS) and RC_PALETTE = RC_PALETTE) then
begin
GetMem(LPal, SizeOf(TLOGPALETTE) + (255 * SizeOf(TPALETTEENTRY)));
FillChar(LPal^, SizeOf(TLOGPALETTE) + (255 * SizeOf(TPALETTEENTRY)), #0);
LPal^.palVersion := $300;
LPal^.palNumEntries := GetSystemPaletteEntries(LDC, 0, 256, LPal^.palPalEntry);
if (LPal^.palNumEntries <> 0) then
LOrgBmp.Palette := CreatePalette(LPal^);
FreeMem(LPal, SizeOf(TLOGPALETTE) + (255 * SizeOf(TPALETTEENTRY)));
end;
// prepare to copy
LOrgBmp.Canvas.CopyMode := cmSrcCopy or cmCaptureBlt;
// copy surface to bitmap
LOrgBmp.Canvas.CopyRect(
Rect(0,
0,
LOrgBmp.Width,
LOrgBmp.Height),
LCanvas,
Rect(Screen.Monitors[0].Left,
Screen.Monitors[0].Top,
Screen.Monitors[0].Width,
Screen.Monitors[0].Height));
// LOrgBmp has now a snapshot of monitor[0] (primary)
{$ELSE}
LOrgBmp.LoadFromFile('enter here path and filename of a bitmap');
{$ENDIF}
// create second bitmap
LNewBmp := TBitmap.Create;
try
// link surface with first bitmap
LCanvas.Handle := LOrgBmp.Canvas.Handle;
// setup basics
LNewBmp.PixelFormat := LOrgBmp.PixelFormat;
LNewBmp.Width := LOrgBmp.Width;
LNewBmp.Height := LOrgBmp.Height;
LNewBmp.Canvas.CopyMode := cmSrcCopy;
// get the right side of LOrgBmp and copy it to the left side of LNewBmp
LNewBmp.Canvas.CopyRect(
Rect(0,
0,
LNewBmp.Width div 2,
LNewBmp.Height),
LCanvas,
Rect(0 + (LOrgBmp.Width div 2),
0,
LOrgBmp.Width,
LOrgBmp.Height));
// get the left side of LOrgBmp and copy it to the right side of LNewBmp
LNewBmp.Canvas.CopyRect(
Rect(0 + (LNewBmp.Width div 2),
0,
LNewBmp.Width,
LNewBmp.Height),
LCanvas,
Rect(0,
0,
LOrgBmp.Width div 2,
LOrgBmp.Height));
// put it on screen
Image1.Picture.Assign(LNewBmp);
// demo is finished, free/release everything
finally
LNewBmp.Free;
end;
finally
LOrgBmp.Free;
end;
finally
LCanvas.Free;
end;
{$IFDEF MSWindows}
finally
ReleaseDC(GetDesktopWindow, LDC);
end;
{$ENDIF}
end;
end.