2

Tpanel のように動作する TCustomControl を作成するにはどうすればよいですか? たとえば、ラベルや画像などのコンポーネントをドロップできる MyCustomComponent などです。

4

1 に答える 1

8

秘訣は、TCustomPanel の次のコードです。

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

派生できる VCL コントロールは他にもたくさんありcsAcceptsControls、それらのControlStyleプロパティに含まれています。

独自のコントロールでこれを行いたいが、そのような VCL コントロールから派生したものではない場合は、次のようにする必要があります。

  1. Create コンストラクターをオーバーライドする
  2. プロパティ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.

--jeroen

于 2010-07-19T06:54:18.943 に答える