警告:この回答は、質問を完全にするためのものであり、実験目的でのみ使用されます。実際のシナリオでは使用しないでください。
1つのソースコードファイルに対して2つの別々のフォーム定義ファイルが必要です。
CreateNew
重要なのは、コンストラクターを利用することです。そのドキュメントを引用するには:
関連付けられた.DFMファイルを使用してフォームを初期化せずにフォームを作成するには、Createの代わりにCreateNewを使用します。
まず、高度なフォームを作成します。
unit Advanced;
interface
uses
Classes, Controls, Forms, StdCtrls;
type
TAdvancedForm = class(TForm)
StandardGroupBox: TGroupBox;
StandardButton: TButton;
AdvancedGroupBox: TGroupBox;
AdvancedButton: TButton;
procedure StandardButtonClick(Sender: TObject);
procedure AdvancedButtonClick(Sender: TObject);
end;
implementation
{$R *.dfm}
procedure TAdvancedForm.StandardButtonClick(Sender: TObject);
begin
Caption := Caption + ' Button1Click';
end;
procedure TAdvancedForm.AdvancedButtonClick(Sender: TObject);
begin
Caption := Caption + ' Button2Click';
end;
end.
アプリを作成し、にコピーAdvanced.dfm
しStandard.dfm
ます。
テキストエディタで開きStandard.dfm
、高度なコンポーネント(この場合はボタンを含む高度なグループボックス)を削除し、フォームとフォームタイプの名前を次のように変更します(T)StandardForm
。
object StandardForm: TStandardForm
...
object StandardGroupBox: TGroupBox
...
object StandardButton: TButton
...
end
end
end
標準フォームのリソースを次の場所に追加しAdvanced.pas
ます。
{$R *.dfm}
{$R Standard.dfm}
そして今、次のコードで、同じソースファイルの両方のフォーム定義を開くことができます。
uses
Advanced;
procedure TForm1.OpenAdvancedFormClick(Sender: TObject);
var
Form: TAdvancedForm;
begin
Form := TAdvancedForm.Create(Application);
Form.Show;
end;
procedure TForm1.OpenStandardFormClick(Sender: TObject);
var
{
Form: TAdvancedForm; // This is tricky! The form we are about to create has
// no AdvancedGroupBox nor AdvancedButton, so make sure
// you are not calling it with code completion.
Form: TStandardForm; // Compiler has no knowledge of TStandardForm!
}
Form: TForm; // So declare your form as TForm!
begin
// But create it as TAdvancedForm, otherwise components will not be found!
Form := TAdvancedForm.CreateNew(Application);
ReadComponentRes('TStandardForm', Form);
Form.Show;
end;