开发者

Parent/Child relationship with TControl

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 开发者_如何学Cto work for TControl?


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.


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜