Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens to the DC when I free a canvas?

Tags:

canvas

delphi

gdi

In this code I would expect a call to ReleaseDC to happen in MyCanvas.Free, but this code runs fine, how come?
And what happens to TCanvas' own DC?

var GlobalDC: THandle;

procedure TForm1.FormCreate(Sender: TObject);
begin
   GlobalDC:= GetDC(0);
end;

function LoopMeOften(const X, Y: Integer): TColor;
var
  MyCanvas: TCanvas;
begin
  MyCanvas := TCanvas.Create;
  MyCanvas.Handle := GlobalDC;
  Result := GetPixel(MyCanvas.Handle, X, Y);
  MyCanvas.Free;
end;
like image 330
Johan Avatar asked Feb 23 '23 20:02

Johan


1 Answers

TCanvas does not have its own DC, it neither acquires nor releases a DC. Its descendant TControlCanvas does.

From TCanvas.Handle Property

TCanvas does not own the HDC. Applications must create an HDC and set the Handle property. Applications must release the HDC when the canvas no longer needs it.

like image 196
Sertac Akyuz Avatar answered Mar 05 '23 02:03

Sertac Akyuz