how to create a TCustomControl that behaves like Tpanel?

会有一股神秘感。 提交于 2019-12-11 07:07:24

问题


how do I create a TCustomControl that will behave like Tpanel? eg MyCustomComponent, that I can drop components in like labels, images etc.


回答1:


The trick is this piece of code in TCustomPanel:

constructor TCustomPanel.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  ControlStyle := [csAcceptsControls {, ... } ];
//...
end;

There are many more VCL controls you can descend from that have csAcceptsControls in their ControlStyle property.

If you want to do this in your own controls, but do not descend from such a VCL control, then you should do something like this:

  1. Override the Create constructor
  2. Add csAcceptsControls to the ControlStyle property

Like this sample code:

//MMWIN:MEMBERSCOPY
unit _MM_Copy_Buffer_;

interface

type
  TMyCustomControl = class(TSomeControl)
  public
    constructor Create(AOwner: TComponent); override;
  end;


implementation

{ TMyCustomControl }

constructor TMyCustomControl.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  ControlStyle := ControlStyle + [csAcceptsControls {, ...} ];
//...
end;


end.

--jeroen



来源:https://stackoverflow.com/questions/3278752/how-to-create-a-tcustomcontrol-that-behaves-like-tpanel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!