Reputation: 247
i am writing my own component that has its canvas. I made a procedure to recreate the control's canvas :
procedure ClearCanvas;
begin
FreeAndNil(FCanvas);
FCanvas := TControlCanvas.Create;
TControlCanvas(FCanvas).Control := Self;
end;
the newly created canvas works well , but the old canvas is still showing its contents !
any errors in the code ?! Thanks
Upvotes: 1
Views: 294
Reputation: 54772
There are no errors with the code you display, it successfully creates a control canvas that attaches to the control and gets rid of the old one.
The content you see does not get cleared when you free the canvas because it doesn't belong to the canvas. A VCL canvas is a class that ease working with graphic functions of the underlying OS. What gets drawn is ultimately on a device context retrieved for a window. If it's absolutely necessary to make an analogy with an actual canvas in your case, you have to get rid of the window of your control (or the window of its parent if it is a graphic control) to start with a new canvas.
What you have to actually do is to erase the contents. Erasing is not literal, it is in fact painting over with whatever background is considered to be the initial state.
Upvotes: 2