2

如何创建一个行为类似于 Tpanel 的 TCustomControl?例如 MyCustomComponent,我可以将组件放入标签、图像等中。

4

1 回答 1

8

诀窍是 TCustomPanel 中的这段代码:

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

csAcceptsControls您可以从其ControlStyle属性中继承更多的 VCL 控件。

如果您想在自己的控件中执行此操作,但不要从这样的 VCL 控件继承,那么您应该执行以下操作:

  1. 覆盖 Create 构造函数
  2. 添加csAcceptsControlsControlStyle属性

像这个示例代码:

//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.

——杰伦

于 2010-07-19T06:54:18.943 回答