1
votes

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!

1
Absolutely fine to drive from TComponent. No reason at all that you can't implement a method to draw your component on a canvas. Of course, that design might be wrong. Maybe you need a visual control. Perhaps you are asking for the wrong thing.David Heffernan
Yes, VCL and FMX are not in any way compatible with each other - not in the sense of writing a single control which works on both. It's essentially like trying to install a diesel engine from a semi truck into mini cooper.Jerry Dodge
As a jump start, you can take a look at something pre-written, such as my code in this question: stackoverflow.com/questions/31767346/… Study it and implement your own thing.Jerry Dodge
Actually, that control of mine inherited from a TShape.Jerry Dodge

1 Answers

2
votes

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.