Let components dropped on my control in the IDE be

2019-02-18 18:05发布

问题:

I have a descendant of TWinControl (in fact it is just that for now) and I registered it as a component in the IDE:

type
  TGroupPanel = class(TWinControl);

But when I drop other components on it, they attach to the form instead of to my control. In other words, I want my custom control to behave like a TPanel so that components dropped on it become its children.

If I create the components at runtime and assign them manually to my control, like in the code below, then it works:

  TForm1 = class(TForm)
    Group: TGroupPanel;
    procedure FormCreate(Sender: TObject);
  private
    Panel: TPanel;
  end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Panel := TPanel.Create(Group);
  Panel.Parent := Group;
  Panel.Align := alClient;
end;

So, what should I do to get components dropped on a TWinControl at design time become children of that control?

(What I am trying to do is to make a special control to group other components on, so I can align and position them together. Of course, I can do that with a normal panel, but I want to do this with a lightweight control that does not paint anything, and in TWinControl I found the solution.)

回答1:

Set csAcceptControls flag for ControlStyle.

constructor TGroupPanel.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  ControlStyle := ControlStyle + [csAcceptsControls];
end;