How do I create and free a TCanvas when you have a handle?
I want to create TCanvas
to draw easier. First I create a canvas MyCanvas:=TCanvas.Create;
, then I get a handle DC:=GetWindowDC(Handle);
and now what should I do ...? Should I assign a new canvas descriptor directly MyCanvas.Handle:=DC;
or should I destroy the existing one first MyCanvas.Handle
? And after I make the drawings, should I free the handle ReleaseDC(Handle,DC);
, or if I free the canvas MyCanvas.Free
, the handle will be released automatically?
source to share
When you create TCanvas
it has no descriptor. Assign the handle using the DC returned GetWindowDC
. When you destroy the canvas, the handle is not destroyed. You need to explicitly call ReleaseDC
.
From the docs :
TCanvas does not own HDC. Applications must create an HDC and set the Handle property. Apps should release HDC when the canvas no longer needs it. Setting the Handle property on a canvas that already has a valid HDC will not automatically issue an initial HDC.
source to share