如何创建一个行为类似于 Tpanel 的 TCustomControl?例如 MyCustomComponent,我可以将组件放入标签、图像等中。
问问题
1613 次
1 回答
8
诀窍是 TCustomPanel 中的这段代码:
constructor TCustomPanel.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
ControlStyle := [csAcceptsControls {, ... } ];
//...
end;
csAcceptsControls
您可以从其ControlStyle
属性中继承更多的 VCL 控件。
如果您想在自己的控件中执行此操作,但不要从这样的 VCL 控件继承,那么您应该执行以下操作:
- 覆盖 Create 构造函数
- 添加
csAcceptsControls
到ControlStyle
属性
像这个示例代码:
//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 回答