form1 中显示 form2 窗口,有以下关键点:
1、在 Form1 所在单元中,引入 Form2 所在单元。如:
unit Unit1;
implementation
uses Unit2;//引入 Form2所在单元。
2、查看工程文件中,Form2 是否自动建立:
program Project1;
uses
Vcl.Forms,
Unit1 in 'Unit1.pas' {Form1},
Unit2 in 'Unit2.pas' {Form2};// Form2 单元文件
{$R *.res}
begin
Application.Initialize;
Application.MainFormOnTaskbar := True;
Application.CreateForm(TForm1, Form1);
Application.CreateForm(TForm2, Form2);//建立 Form2 的实例
Application.Run;
end.
1、新建DELPHI工程2、在窗体FROM1中添加按钮(Button1)3、新建窗体(FROM2);4、在单元文件UNIT1中引用单元文件(UNIT2),即书写:uses unit2;unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;
implementation
{$R *.dfm}
uses unit2;
procedure TForm1.Button1Click(Sender: TObject);
begin
form2.Show;
end;
end.
if form2=nil then form2:=Tform2.create(self);form2.show;