如何创建行为类似于Tpanel的TCustomControl? [英] how to create a TCustomControl that behaves like Tpanel?

查看:99
本文介绍了如何创建行为类似于Tpanel的TCustomControl?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何创建行为类似于Tpanel的TCustomControl?例如MyCustomComponent,我可以在其中放置标签,图像等组件。

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

推荐答案

诀窍是TCustomPanel中的这段代码:

The trick is this piece of code in TCustomPanel:

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

具有 csAcceptsControls的VCL控件还有很多code>在其 ControlStyle 属性中。

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

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. 覆盖Create构造函数

  2. csAcceptsControls 添加到 ControlStyle 属性

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

就像下面的示例代码:

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

这篇关于如何创建行为类似于Tpanel的TCustomControl?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆