是否可以在没有源文件的情况下创建和显示TForm? 我想在运行时创建我的表单,并且空的* .dfm和* .pas文件对我来说似乎没用。
谢谢
答案 0 :(得分:10)
你的意思是这样吗?
procedure TForm1.Button1Click(Sender: TObject);
var
Form: TForm;
Lbl: TLabel;
Btn: TButton;
begin
Form := TForm.Create(nil);
try
Form.BorderStyle := bsDialog;
Form.Caption := 'My Dynamic Form!';
Form.Position := poScreenCenter;
Form.ClientWidth := 400;
Form.ClientHeight := 200;
Lbl := TLabel.Create(Form);
Lbl.Parent := Form;
Lbl.Caption := 'Hello World!';
Lbl.Top := 10;
Lbl.Left := 10;
Lbl.Font.Size := 24;
Btn := TButton.Create(Form);
Btn.Parent := Form;
Btn.Caption := 'Close';
Btn.ModalResult := mrClose;
Btn.Left := Form.ClientWidth - Btn.Width - 16;
Btn.Top := Form.ClientHeight - Btn.Height - 16;
Form.ShowModal;
finally
Form.Free;
end;
end;
答案 1 :(得分:3)
是的,有可能:
procedure TForm1.Button1Click(Sender: TObject);
var
Form: TForm;
begin
Form:= TForm.Create(Self);
try
Form.ShowModal;
finally
Form.Free;
end;
end;