tags:

views:

98

answers:

4

I want to implement Parent/Child relationship with TControl and I mean to be able to have a TControl that can serve as a container for another TControl. So far as I could see you can only do to that with TWinControl, why is that? and how I can make it to work for TControl?

+3  A: 

You need TWinControl to provide a drawing context (handle) and consequently to draw a control. That is how Windows works.

VCL allows controls that have no handle; these controls takes a handle from their parent. That is how VCL designed.

Theoretically you can design a library (VCL replacement or enhancement) that does not require a TControl to be parented by TWinControl, but you still need to obtain a handle somehow (from some TWinControl) to draw TControls without handle. I don't see any benefits in such an approach, it just makes things more complicated.

Serg
Indeed I have a base container that is TWinControl's descendant and contains my controls (TControl's descendants) that mimic controls like label, edit, button, checkbox, etc.The base container (TWinControl) draws the background and then the controls inside it, simulate focus events and keystrokes.It works pretty well, UI looks fantastic (alpha blending, anti-aliased curved borders, etc), it's fast and most important flicker free though there is a lot of functionality missing at it's current state.I could make something from scratch but I will have to implement anchors support, etc.
pani
@pani - I don't think that reimplementing VCL functionality from scratch is a good idea. Even if you succeed, you will finally obtain a headache - for example, updating your component to work with newer VCL versions will (possibly) be a problem. Note that VCL itself wraps standard Windows controls whenever possible rather than designing controls from scratch.
Serg
Thank you Serg, your remarks made me realise that using TControl (or TWinControl) for what I had in mind could lead to future problems as you mentioned.
pani
A: 

Do you mean that you want to be able to place one control inside of another on a form, like putting a button on a panel or a group box? If so, there's a reason why you can only do it with TWinControl descendants. The parenting is handled by Windows, not by the VCL, and in order to do it you need a control that has a HWND window handle. This is the distinguishing characteristic of TWinControl: it has a window handle, whereas other TControl descendants don't.

Mason Wheeler
The VCL, as far as I saw, can work with controls that have a window handle and controls without one. I believe by design you can only have as TControl's parent only a TWinControl. I want to overcome this limitation.
pani
A: 

To host other controls, your container needs to have a window handle. That's mainly because every windowed control needs to have a parent window. To avoid that restriction, you might be able to hack something together so that your container can only accept TGraphicControl descendants as children, but I think the IDE already "knows" that only TWinControls can hold other controls, so you'd be fighting against that. In particular, the IDE will want to assign the child's Parent property, but there won't be anything to assign to it.

If, on the other hand, you give a TControl a window handle, you may as well just have a TWinControl.

Rob Kennedy
I can always create the controls at runtime. The question is how to do that hack...
pani
A: 

TWinControl have it's own piece of screen to draw on (given by OS). Non-Win TControl doesn't, it paints on parent's context. Thus only TWinControl can be a parent, use it instead of TControl.

adf88