unit UPaintboxBuffer;
interface
uses
ExtCtrls, Graphics;
type
TPaintboxBuffer =
class
private
FPaintBox : TPaintBox;
FBitmapA,
FBitmapB : TBitmap;
FWorkBmp : TBitmap;
// either FBitmapA or FBitmapB
procedure CreateBitmap(
var Bmp : TBitmap; W, H : Integer);
procedure OnPaintHandler(Sender: TObject);
public
constructor Create(APaintBox : TPaintBox);
destructor Destroy;
procedure SwitchBitmap;
property Bitmap : TBitmap
read FWorkBmp;
end;
implementation
constructor TPaintboxBuffer.Create(APaintBox : TPaintBox);
begin
FPaintBox := APaintBox;
CreateBitmap(FBitmapA, FPaintBox.Width, FPaintBox.Height);
CreateBitmap(FBitmapB, FPaintBox.Width, FPaintBox.Height);
FWorkBmp := FBitmapA;
FPaintBox.OnPaint := OnPaintHandler;
end;
procedure TPaintboxBuffer.CreateBitmap(
var Bmp : TBitmap; W, H : Integer);
begin
Bmp := TBitmap.Create;
Bmp.Width := W;
Bmp.Height := H;
end;
destructor TPaintboxBuffer.Destroy;
begin
FPaintBox.OnPaint :=
NIL;
FBitmapA.Free;
FBitmapB.Free;
end;
procedure TPaintboxBuffer.OnPaintHandler(Sender: TObject);
begin
FPaintBox.Canvas.StretchDraw(FPaintBox.Canvas.ClipRect, FWorkBmp);
end;
procedure TPaintboxBuffer.SwitchBitmap;
begin
if FWorkBmp = FBitMapA
then
FWorkBmp := FBitMapB
else
FWorkBmp := FBitMapA;
FPaintBox.Invalidate;
end;
end.