Delphi visual component with Canvas

  

I have always written non-visual components; their creation is pretty easy and they work equally well under VCL and FMX. So far so good, but now I’m facing a problem.

I used to inherit from TComponent but now I cannot anymore because my component called TRedistPreview really needs a procedure like this:

procedure drawPreview(area: TCanvas);

This procedure has to draw something (shapes, lines and colours) somewhere, for example in a TRectangle. I have seen online that TComponent doesn’t have the ability to draw, so I should inherit from something else. I have found TWinControl (but that is VCL-only) and TCustomControl (VCL only, too).

Could you please tell me what I should inherit from to get a canvas? I mean, I want to replace:

TRedistPreview = class(TComponent)
end;

With:

TRedistPreview = class(TSomeClassThatHasCanvas)
end;

Where I can call procedure drawPreview(area: TCanvas); and draw on a surface (like a TRectangle).

I am looking for the lowest possible class in the hierarchy with a Canvas.

Since this component is very useful to me under Windows and Android, I am looking for a Firemonkey implementation. From my research, I have seen that I could inherit from TRectangle, which is inside FMX.Objects, but I don’t know if this is the right choice.

What should I do?

Also, if I needed this component to be in VCL, do I have to write another component that inherits from another class?

As I’ve said, this is my first time with visual component writing, so I’d like someone to show me the right way!

Comments are closed.