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!
In VCL the first class that supports canvas is TCustomcontrol
which is descendant from TWinControl
.
http://docwiki.embarcadero.com/Libraries/Seattle/en/Vcl.Controls.TCustomControl
In FMX the base class that does allow painting routines is TControl.
But bare in mind that rendering of visual components in FireMonkey is a lot different than in VCL. So if you think about reusing VCL code in FMX or vice versa I'm afraid it probably won't be possible.