1

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

19
  • 4
    I can't make sense of this procedure. Simply calling Invalidate (or Repaint) method requests the control for repainting. The rendering itself is then done in the Paint method overridde. Commented May 27, 2017 at 16:11
  • Not an answer, but why on earth are you using FreeAndNil? Commented May 27, 2017 at 16:11
  • 2
    That's not how painting works. You only get a repaint when requested. The canvas does not retain any state. Repainting happens in paint cycle, in response to WM_PAINT. Almost certainly that code should be removed. It must be wrong. Commented May 27, 2017 at 17:12
  • 2
    Recreating the canvas won't help you, it's not directly attached to device contexts, which are not directly attached to display surfaces anyway. You're on the wrong track. Commented May 28, 2017 at 15:06
  • 1
    Your approach is wrong. That's the key point. Recreating the canvas does nothing useful. As you discovered. This was explained to you repeatedly. Eventually you accepted this. Commented May 29, 2017 at 6:40

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for the detailed answer , I then should check Windows api reference to see if I can clear the device context, if not possible then I will be obliged to recreate it .
@DrWael - You're welcome. There's no API that clears a device context, what is generally used is filling with a solid color. Releasing, retrieving a device context won't help either, what gets drawn is to the window that the DC belongs to.
I was thinking that ResetDC might do the job , but no problem I may restructure my component guided by the behavior of the Clear button in the standard tpicture property editor .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.