procedure Tfrom1.Button1Click(Sender: TObject);
begin
form2:=TfrmMessage.Create(self);
form2.id:=id;
form2.Show;
etForegroundWindow(form2.Handle);
end;每按一次就会创建一个Tform1窗体的实例并show出来
现在我要根据我赋予form2.id的值,找出该窗体,如果没有,则创建并from2.show,如果有,则呼唤到前,应该怎么做啊
begin
form2:=TfrmMessage.Create(self);
form2.id:=id;
form2.Show;
etForegroundWindow(form2.Handle);
end;每按一次就会创建一个Tform1窗体的实例并show出来
现在我要根据我赋予form2.id的值,找出该窗体,如果没有,则创建并from2.show,如果有,则呼唤到前,应该怎么做啊
=================================================function TFrmMain.sysOpenForm(refClass:TClass;var frmName;aOwner:TComponent):Boolean;
var
i:Integer;
tmp:TForm;
begin
result:=False; try
begin /////////////////////////////////
for i:=0 to Screen.FormCount-1 do
begin
if Screen.Forms[i].ClassType=refClass then
begin
tmp:=Screen.Forms[i];
if tmp.WindowState=wsMinimized then
ShowWindow(tmp.Handle,SW_SHOWNORMAL)
else
showWindow(tmp.Handle,SW_SHOWNA);
if not tmp.Visible then tmp.Visible:=True;
tmp.BringToFront;
tmp.SetFocus; TForm(frmName):=tmp;
result:=True;
exit;
end;
end; /////////////////////////////////
tmp:=TForm(refClass.NewInstance);
TForm(frmName):=Tmp;
Tmp.Create(aOwner);
Tmp.Show;
result:=true;
end;
except
begin
result:=false;
exit;
end;
end;
end;
program MP3Player;uses
Forms,
Windows,
mp3 in 'mp3.pas' {Form1};
var
Hwnd : THandle;
{$R *.res}{begin
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.Run;
end.}begin
Hwnd:=FindWindow('TForm1','MP3Player'); //查找是否已有窗体MainForm
if Hwnd<>0 then
begin
SetForegroundWindow(Hwnd); //激活已運行的程序實例,但程序最小化??效
Application.Terminate; //中止本次實例
end
else
begin
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.Run;//運行本次實例
end;
end.
uses unit2
unit2是form2的单元。
var
form2:=Tfrom;
begin
try
from2:=Tfrom2.create(application);
from2.show;
except
showmessage('not create');
end;
end;用于父子窗体模式;
var
Form1 :TForm1;
MyForm:TForm1;
procedure DynamicCreate(id:integer;NewForm:TForm1);
···········implementation
·······
procedure DynamicCreate(id:integer;NewForm:TForm1);
var
i:integer;
begin
for i:=0 to screen.FormCount-1 do
begin
if screen.Forms[i].Tag=id then
begin
screen.Forms[i].BringToFront;
exit;
end
end; NewForm:=Tform1.create(application);
Newform.Tag:=id;
NewForm.BringToFront;
NewForm.SetFocus;
end;procedure TForm1.Button1Click(Sender: TObject);
begin
DynamicCreate(1,MyForm);
end;