Du musst ein neues erzeugen und das Bitmap-
Handle dort selektieren, damit in das Bitmap gezeichnet wird
Delphi-Quellcode:
type
TSurfaceCanvas =
class(TCanvas)
private
FBmp: TBitmap;
FOldBitmapHandle: HBITMAP;
FOldPaletteHandle: HPALETTE;
protected
procedure CreateHandle;
override;
public
constructor Create(AWidth, AHeight: Integer);
destructor Destroy;
override;
property Bitmap: TBitmap
read FBmp;
end;
{ TSurfaceCanvas }
constructor TSurfaceCanvas.Create(AWidth, AHeight: Integer);
begin
Assert(AWidth * AHeight > 0);
inherited Create;
FBmp := TBitmap.Create;
FBmp.Width := AWidth;
FBmp.Height := AHeight;
end;
procedure TSurfaceCanvas.CreateHandle;
begin
Handle := CreateCompatibleDC(0);
FOldBitmapHandle := SelectObject(
Handle, FBmp.Handle);
if not FBmp.IgnorePalette
then
begin
FOldPaletteHandle := SelectObject(
Handle, FBmp.Palette);
// RealizePalette(FBmp.Palette); FALSCH
RealizePalette(
Handle);
// RICHTIG
end;
end;
destructor TSurfaceCanvas.Destroy;
var
DC: HDC;
begin
if Handle <> 0
then
begin
SelectObject(
Handle, FOldBitmapHandle);
if FOldPaletteHandle <> 0
then
SelectObject(
Handle, FOldPaletteHandle);
DC :=
Handle;
Handle := 0;
DeleteDC(
DC);
end;
FBmp.Free;
inherited Destroy;
end;