如何动态创建窗体(不设FormStyle) ( 积分: 50 )

  • 主题发起人 主题发起人 lijun175165721
  • 开始时间 开始时间
L

lijun175165721

Unregistered / Unconfirmed
GUEST, unregistred user!
function TForm3.CreateForm(AOwner:TComponent;Aclass:TComponentClass):TComponent;
var
Instance:TComponent;
begin
Instance:=TComponent(AClass.NewInstance);
Instance.Create(AOwner);
CreateForm1:=Instance;
end;
我是按上面的方面来动态创建窗体的,不过在后面调用此函数时没有创建成功!
请大家指点!
 
function TForm3.CreateForm(AOwner:TComponent;Aclass:TComponentClass):TComponent;
var
Instance:TComponent;
begin
Instance:=TComponent(AClass.NewInstance);
Instance.Create(AOwner);
CreateForm1:=Instance;
end;
我是按上面的方面来动态创建窗体的,不过在后面调用此函数时没有创建成功!
请大家指点!
 
把APPLICATION做为参数
然后调用
Application.CreateForm(Aclass, your_form);
我想应该可以的
 
我也试过这样用Application,但是还是没有成功的创建窗体。
 
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
function createform(app:Tapplication;cls:TComponentClass):Tform;
end;

var
Form1: TForm1;

implementation

uses Unit2;

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
createform(application,Tform2).Show;
end;
///////////////////////////////////////////////////////////////////////////////
function TForm1.createform(app: Tapplication
cls: TComponentClass): Tform;
var
fm:Tform;
begin
app.CreateForm(cls,fm);
result:=fm;
end;
//////////////////////////////////////////////////////////////////////
end.



unit Unit2;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs;

type
TForm2 = class(TForm)
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form2: TForm2;

implementation

{$R *.dfm}

end.
 
// 调用检查Forms
procedure TTaiShing.FindShowForm(FormClass: TFormClass
var Form);
var
I: Integer;
begin
try
TCustomForm(Form) := nil;
for I := 0 to Screen.FormCount - 1 do
if Screen.Forms.ClassNameIs(FormClass.ClassName) then
begin
TCustomForm(Form) := Screen.Forms;
break;
end;
if TCustomForm(Form) = nil then
begin
Application.CreateForm(FormClass, Form);
end;
if TCustomForm(Form).WindowState = wsMinimized then
ShowWindow(TCustomForm(Form).Handle, SW_RESTORE)
else
TCustomForm(Form).BringToFront;
except
on E: Exception
do raise E.Create(E.Message);
end;
end;
 
// 调用检查Forms
procedure TTaiShing.FindShowForm(FormClass: TFormClass
var Form);
var
I: Integer;
begin
try
TCustomForm(Form) := nil;
for I := 0 to Screen.FormCount - 1 do
if Screen.Forms.ClassNameIs(FormClass.ClassName) then
begin
TCustomForm(Form) := Screen.Forms;
break;
end;
if TCustomForm(Form) = nil then
begin
Application.CreateForm(FormClass, Form);
end;
if TCustomForm(Form).WindowState = wsMinimized then
ShowWindow(TCustomForm(Form).Handle, SW_RESTORE)
else
TCustomForm(Form).BringToFront;
except
on E: Exception
do raise E.Create(E.Message);
end;
end;
 
// 调用检查Forms
procedure TTaiShing.FindShowForm(FormClass: TFormClass
var Form);
var
I: Integer;
begin
try
TCustomForm(Form) := nil;
for I := 0 to Screen.FormCount - 1 do
if Screen.Forms.ClassNameIs(FormClass.ClassName) then
begin
TCustomForm(Form) := Screen.Forms;
break;
end;
if TCustomForm(Form) = nil then
begin
Application.CreateForm(FormClass, Form);
end;
if TCustomForm(Form).WindowState = wsMinimized then
ShowWindow(TCustomForm(Form).Handle, SW_RESTORE)
else
TCustomForm(Form).BringToFront;
except
on E: Exception
do raise E.Create(E.Message);
end;
end;
 
后退
顶部