在工程文件中写 program project1;uses Forms, Controls, SysUtils; begin Application.Initialize; try CurrentPath:=ExtractFilePath(Application.ExeName); ComputName := GetComputName; Application.Title := '************ ---- 测试版'; Application.CreateForm(TDM, DM); 数据模块 Application.CreateForm(TfmMain, fmMain); 主窗口 Application.CreateForm(TfmLogin, fmLogin); 验证窗口 fmLogin.ShowModal; if fmLogin.ModalResult = mrOk then begin fmLogin.Free; fmMain.Show; Application.Run; end else begin fmLogin.Free; fmMain.Free; dm.Free; end; except Application.Terminate; end; end.
登陆窗体动态创建,放在主窗体OnActivate事件即可 procedure TForm1.FormActivate(Sender: TObject); var Fsign:TForm2; begin Fsign:=TForm2.Create(self);//动态创建登录窗体 while (Fsign.Edit1.Text<>'abc')and(Fsign.ModalResult<>mrcancel) do begin Fsign.ShowModal; Fsign.Label1.Caption:='密码错误'; end; if Fsign.Edit1.Text='abc' then Fsign.Close else Application.Terminate; end;
然后在 Form的OnCreate 中写(在OnShow等也可以):PostMessage(Handle,WM_COMMAND,0,Integer(button1.Handle));因为PostMessage是异步的,不会阻碍Form的创建和显示等过程。
program project1;uses
Forms,
Controls,
SysUtils;
begin
Application.Initialize;
try
CurrentPath:=ExtractFilePath(Application.ExeName);
ComputName := GetComputName;
Application.Title := '************ ---- 测试版';
Application.CreateForm(TDM, DM); 数据模块
Application.CreateForm(TfmMain, fmMain); 主窗口
Application.CreateForm(TfmLogin, fmLogin); 验证窗口
fmLogin.ShowModal;
if fmLogin.ModalResult = mrOk then
begin
fmLogin.Free;
fmMain.Show;
Application.Run;
end
else begin
fmLogin.Free;
fmMain.Free;
dm.Free;
end;
except
Application.Terminate;
end;
end.
procedure TForm1.FormActivate(Sender: TObject);
var Fsign:TForm2;
begin
Fsign:=TForm2.Create(self);//动态创建登录窗体
while (Fsign.Edit1.Text<>'abc')and(Fsign.ModalResult<>mrcancel) do
begin
Fsign.ShowModal;
Fsign.Label1.Caption:='密码错误';
end;
if Fsign.Edit1.Text='abc' then
Fsign.Close
else Application.Terminate;
end;
这样的话,最好用一个timer来实现
begin
Timer1.Interval:=1;
Timer1.Enabled:=true;
end;procedure TForm1.Timer1Timer(Sender: TObject);
begin
Timer1.Enabled:=false;
Form2.ShowModal;
end;
……
form2.showModal;
form1.hide
……