2
votes

Hi I'm developing a TControl descendant, lets name it THTMLBaseControl, at runtime that control only generates and output HTML code based on the settings of that control, so all the additional properties of the base TControl class and methods, including Windows Messaging system is really not used at runtime and causes memory overhead.

I need that control to be inherited from TControl so at design time I can use all the IDE designer stuff.

But at runtime almost all of those properties that at desingtime are needed i dont need them.

I also have all my controls inherited from that THTMLBaseControl, so creating a wrapper class per control class is not an option because it will duplicate the code a lot.

So what i need is something that at runtime, before the class is instantiated I can change the parent class so it will be instantiated based on another TControl-like class, maybe named TmyBaseControl inherited from "TComponent" as TControl Does, but that will not have all that TControl memory overhead and will only have the properties and methods needed by my THTMLBaseControl.

I really dont have a GUI at rutime is a web server that will serve only HTML, is some thing that intraweb and Raudus do, but always the issue is that all are based on TControl, so they have to be created at run time and generate a lot memory and process overhead that is not used. and maybe there could a solution so any THTMlBaseControl descendant instantiated at runtime will inherit the all properties and methods from TmyBaseControl and not from TControl.

I have seen there are ways to hack the VMT but maybe there are other solution and have not seen it. I already done changing the NewInstance, ClassParent and TnstanceSize class methods but i have to specify from which class and obviously i have to do the same steps per each inherited THTMLBaseControl class

And for the sake of all: This is just a doubt, I need the components to be controls like TEdit, TPanel, visible and editable by the designer IDE I even could create my own TControl class but I was just thinking if I can reuse the code already existing.

Regards

1
What you are proposing is not the solution to your problem, even if it were possible. Is your main concern that of overhead? How many instances of this object do you have? Do you know that properties of TComponent descendents show up in the designer?David Heffernan
Why would you use TCustomControl, @Lightbulb? TCustomControl is the same as TConrol, except it has more stuff, in particular a window handle and a canvas.Rob Kennedy
That is really really wrong. Not enough detail to say, have you considered a THTMLControlConfigComponent for design time behehaviour and then instantiate your controls based on it's settings?Tony Hopkinson
@RobKennedy, my bad. I was assuming TControl was descended from TCustomControl. I never had to use it before, so I checked now. You are right, TComponent is best way to go here.LightBulb
@Lightbulb I'm not lazy, it was just a question if "it can be done in another way", I could also copy all the code from TControl and create mine, or just have wrapper descending from TComponent and show it in the designer via TControl.Canvas. This was just a humble question about if it can be done or "is there another way", I need the control to be edited, resized as an TEdit for example. Just wanted to reuse code if it could be, if not I will find my way. TBaseHTMLControl was an example to illustrate my Doubt. Thanks for the enlightenmentfduenas

1 Answers

10
votes

You cannot change the class a run time. Once an object is instantiated, its class is fixed. You could hack the object to change its VMT pointer, making it refer to a different class, but that would still not address your main concern, which is memory usage — even if you change the VMT pointer, all the memory for the object has already been allocated; changing the VMT pointer doesn't magically make the object occupy less memory.

The first thing you could do is stop descending from TControl. As you've noted, you don't need any of the things it provides. All you want is something you can drop on a form at design time to set its properties. For that, all you need is TComponent, so make that your base class instead of TControl. Then you'll get something more like TTimer, which has no GUI. Once you've done that, you no longer need TForm, either. Instead, you can put your component on a TDataModule, which is specifically designed for managing non-visual components at design time.