如何使用表单的全局变量将其放入面板中?
How can I use a form's global variable to put it in a panel?
这是我的问题的延续:
我现在想使用表单全局变量将其嵌入到面板中以显示它,但它只创建要嵌入的表单,没有按钮。
在可执行文件的代码中,我首先创建要嵌入的表单,然后再创建要嵌入的表单,如下所示:
program Project1;
uses
System.StartUpCopy,
FMX.Forms,
Unit1 in 'Unit1.pas' {Form1},
Unit2 in 'Unit2.pas' {Form2};
{$R *.res}
begin
Application.Initialize;
Application.CreateForm(TForm2, Form2);
Application.CreateForm(TForm1, Form1);
Application.Run;
end.
主窗体代码为:
unit Unit1;
interface
uses
System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.StdCtrls;
type
TForm1 = class(TForm)
Label1: TLabel;
Panel1: TPanel;
procedure EmbedForm(ArgParent : TControl; ArgForm : TCustomForm);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.fmx}
uses Unit2;
procedure TForm1.FormCreate(Sender: TObject);
begin
EmbedForm(Panel1, Form2);
end;
procedure TForm1.EmbedForm(ArgParent: TControl; ArgForm: TCustomForm);
begin
while ArgForm.ChildrenCount>0 do
begin
ArgForm.Children[0].Parent:= ArgParent;
end;
end;
end.
要嵌入的表单代码是:
unit Unit2;
interface
uses
System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.StdCtrls;
type
TForm2 = class(TForm)
Button2: TButton;
Button1: TButton;
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form2: TForm2;
implementation
{$R *.fmx}
end.
我之前为避免必须遍历 ArgForm 的所有子项而执行此操作的方法是在 ArgForm 上设置一个 "master container" 类型,其中包含您需要的所有子项。
我的设置方式是
- 首先放置一个 TLayout,与 ArgForm 上的 Client 对齐
- 接下来,我将所有子控件添加到 ArgForm 的 TLayout(按钮等)
- 接下来将面板添加到我们希望嵌入的表单中
- 设置该表单后,我将 ArgForm 的布局分配给 ParentForm 的 OnShow 上的父表单面板,而不是 OnCreate (ArgForm.Children[0].Parent:=Self.Panel1;)
项目来源:
program Project1;
uses
System.StartUpCopy,
FMX.Forms,
Unit1 in 'Unit1.pas' {ParentForm},
Unit2 in 'Unit2.pas' {ArgForm};
{$R *.res}
begin
Application.Initialize;
Application.CreateForm(TParentForm, ParentForm);
Application.CreateForm(TArgForm, ArgForm);
Application.Run;
end.
家长表格代码:
unit Unit1;
interface
uses
System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs,unit2,
FMX.StdCtrls;
type
TParentForm = class(TForm)
Panel1: TPanel;
procedure FormShow(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
ParentForm: TParentForm;
implementation
{$R *.fmx}
procedure TParentForm.FormShow(Sender: TObject);
begin
ArgForm.Children[0].Parent:=Self.Panel1;
end;
end.
ArgForm 代码:
unit Unit2;
interface
uses
System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.StdCtrls,
FMX.Layouts;
type
TArgForm = class(TForm)
Layout1: TLayout;
Button1: TButton;
Button2: TButton;
private
{ Private declarations }
public
{ Public declarations }
end;
var
ArgForm: TArgForm;
implementation
{$R *.fmx}
end.
也许其他人可以回答,但在我看来,创建按钮时未显示的原因是当时尚未创建控件吗?
这是我的问题的延续:
我现在想使用表单全局变量将其嵌入到面板中以显示它,但它只创建要嵌入的表单,没有按钮。
在可执行文件的代码中,我首先创建要嵌入的表单,然后再创建要嵌入的表单,如下所示:
program Project1;
uses
System.StartUpCopy,
FMX.Forms,
Unit1 in 'Unit1.pas' {Form1},
Unit2 in 'Unit2.pas' {Form2};
{$R *.res}
begin
Application.Initialize;
Application.CreateForm(TForm2, Form2);
Application.CreateForm(TForm1, Form1);
Application.Run;
end.
主窗体代码为:
unit Unit1;
interface
uses
System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.StdCtrls;
type
TForm1 = class(TForm)
Label1: TLabel;
Panel1: TPanel;
procedure EmbedForm(ArgParent : TControl; ArgForm : TCustomForm);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.fmx}
uses Unit2;
procedure TForm1.FormCreate(Sender: TObject);
begin
EmbedForm(Panel1, Form2);
end;
procedure TForm1.EmbedForm(ArgParent: TControl; ArgForm: TCustomForm);
begin
while ArgForm.ChildrenCount>0 do
begin
ArgForm.Children[0].Parent:= ArgParent;
end;
end;
end.
要嵌入的表单代码是:
unit Unit2;
interface
uses
System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.StdCtrls;
type
TForm2 = class(TForm)
Button2: TButton;
Button1: TButton;
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form2: TForm2;
implementation
{$R *.fmx}
end.
我之前为避免必须遍历 ArgForm 的所有子项而执行此操作的方法是在 ArgForm 上设置一个 "master container" 类型,其中包含您需要的所有子项。 我的设置方式是
- 首先放置一个 TLayout,与 ArgForm 上的 Client 对齐
- 接下来,我将所有子控件添加到 ArgForm 的 TLayout(按钮等)
- 接下来将面板添加到我们希望嵌入的表单中
- 设置该表单后,我将 ArgForm 的布局分配给 ParentForm 的 OnShow 上的父表单面板,而不是 OnCreate (ArgForm.Children[0].Parent:=Self.Panel1;)
项目来源:
program Project1;
uses
System.StartUpCopy,
FMX.Forms,
Unit1 in 'Unit1.pas' {ParentForm},
Unit2 in 'Unit2.pas' {ArgForm};
{$R *.res}
begin
Application.Initialize;
Application.CreateForm(TParentForm, ParentForm);
Application.CreateForm(TArgForm, ArgForm);
Application.Run;
end.
家长表格代码:
unit Unit1;
interface
uses
System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs,unit2,
FMX.StdCtrls;
type
TParentForm = class(TForm)
Panel1: TPanel;
procedure FormShow(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
ParentForm: TParentForm;
implementation
{$R *.fmx}
procedure TParentForm.FormShow(Sender: TObject);
begin
ArgForm.Children[0].Parent:=Self.Panel1;
end;
end.
ArgForm 代码:
unit Unit2;
interface
uses
System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.StdCtrls,
FMX.Layouts;
type
TArgForm = class(TForm)
Layout1: TLayout;
Button1: TButton;
Button2: TButton;
private
{ Private declarations }
public
{ Public declarations }
end;
var
ArgForm: TArgForm;
implementation
{$R *.fmx}
end.
也许其他人可以回答,但在我看来,创建按钮时未显示的原因是当时尚未创建控件吗?