代码之家  ›  专栏  ›  技术社区  ›  XBasic3000

如何创建行为类似于Tpanel的TCustomControl?

  •  1
  • XBasic3000  · 技术社区  · 16 年前

    1 回复  |  直到 14 年前
        1
  •  8
  •   Jeroen Wiert Pluimers    16 年前

    诀窍在于TCustomPanel中的这段代码:

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

    有更多的VCL控制,你可以下降,从有 csAcceptsControls ControlStyle 财产。

    如果您想在自己的控件中执行此操作,但不要从这样的VCL控件降级,则应执行以下操作:

    1. 重写创建构造函数
    2. CSAcceptsControl控件 到 财产

    如以下示例代码:

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

    --杰罗恩