var
  Form :TForm;
  Btn :Tbutton;


procedure FormClose(Sender :TObject; Action :TCloseAction);
begin
  Action := CaFree;
  if sender = Form then Form := nil;
end;

procedure ButtonClick(Sender :TObject);
begin
  ShowMessage('Clicked');
end;

begin

 Form := TForm.create(application);
 Form.Width := 200;
 Form.Height:= 200;
 Form.Position := poScreenCenter;
 Form.OnClose := @FormClose;
 Form.FormStyle := fsMdiChild;

 Btn := TButton.create(Form);
 Btn.left := (Form.clientwidth - Btn.Width) div 2;
 Btn.Top := (Form.ClientHeight - Btn.Height) div 2;
 Btn.OnClick := @ButtonClick;
 Btn.Caption := 'Click me';
 Btn.Parent := Form;

 //while Form <> nil do Application.ProcessMessages; // нельзя завершать скрипт, пока активна эта форма.

 ShowMessage('Script ended');

end.

