在工程文件的begin...end.之间啊! begin Application.Initialize; Application.Title := '应用程序标题'; FrmUserLogin:=TFrmUserLogin.Create(nil); FrmUserLogin:=TFrmUserLogin.Create(nil);//密码验证窗体 try FrmUserlogin.ShowModal ; if FrmUserlogin.LoginValue = True then//验证成功 begin Application.CreateForm(TFrmMainUnit, FrmMainUnit); Application.Run; end; finally FrmUserlogin.Free; end; end.
也可以在 Mainform onshow过程中启动 登陆窗体
public Passed;Boolean;{A flag to determine user passed login or not} end; var Form1:TForm1;
implementprocedure TForm1.FormCreate(Sender:TObject); begin Passed:=False; end;procedure TForm1.FormShow(Sender:TObject); begin if Not Passed then begin LoginForm:=TLoginForm.Create(Self); LoginForm.ShowModal; LoginForm.Free; end; end;以下是LoginForm的登陆窗口中点击确定按钮的事件: uses unit1;procedure TLoginForm.Button1Click(Sender:TObject) begin {check the ID and Password of a user} if 通过 then Form1.Passed:=True else Form1.Passed:=False; end;
begin
Application.Initialize;
Application.Title := '应用程序标题';
FrmUserLogin:=TFrmUserLogin.Create(nil);
FrmUserLogin:=TFrmUserLogin.Create(nil);//密码验证窗体
try
FrmUserlogin.ShowModal ;
if FrmUserlogin.LoginValue = True then//验证成功
begin
Application.CreateForm(TFrmMainUnit, FrmMainUnit);
Application.Run;
end;
finally
FrmUserlogin.Free;
end;
end.
Passed;Boolean;{A flag to determine user passed login or not}
end;
var
Form1:TForm1;
implementprocedure TForm1.FormCreate(Sender:TObject);
begin
Passed:=False;
end;procedure TForm1.FormShow(Sender:TObject);
begin
if Not Passed then
begin
LoginForm:=TLoginForm.Create(Self);
LoginForm.ShowModal;
LoginForm.Free;
end;
end;以下是LoginForm的登陆窗口中点击确定按钮的事件:
uses unit1;procedure TLoginForm.Button1Click(Sender:TObject)
begin
{check the ID and Password of a user}
if 通过 then
Form1.Passed:=True
else
Form1.Passed:=False;
end;
formflash:=Tformflash.Create(application);
formflash.showmodal;
formflash.Update;
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, Buttons, DB, DBTables, wwdbdatetimepicker;type
Tlogin = class(TForm)
GroupBox1: TGroupBox;
edtUser: TEdit;
edtPswd: TEdit;
lblUser: TLabel;
lblPassword: TLabel;
bbtnOK: TBitBtn;
bbtnCancel: TBitBtn;
procedure bbtnCancelClick(Sender: TObject);
procedure bbtnOKClick(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
{ Private declarations }
public
{ Public declarations }
end;var
login: Tlogin;implementationuses MainForm, MastData;{$R *.DFM}procedure Tlogin.bbtnCancelClick(Sender: TObject);
begin
Application.Terminate;
end;procedure Tlogin.bbtnOKClick(Sender: TObject);
begin
with MastData.ADOData do
begin
try
T_user.Filter:='ID='''+edtUser.Text+'''';
T_user.Filtered:=True;
T_user.Open;
except
Application.MessageBox('请与系统管理员联系。','数据库连接错误!',MB_OK);
Application.Terminate;
end;
if T_user.RecordCount=1 then
begin
if T_user['PASSWD']=edtPswd.Text then
begin
login.Hide;
login.Free;
if not Assigned(Main) then
Main:=TMain.Create(Application);
Main.Show;
end
else begin
Application.MessageBox('请重新输入密码,注意大小写!','密码不正确',MB_OK);
end;
end
else
Application.MessageBox('请确认用户名是否正确!','无此用户',MB_OK);
end;
end;procedure Tlogin.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Action :=caFree;
end;end.
并且showmodal之前一定要application.showmainform:=false;
除非适用halt非正常退出,否则你的主窗体会显示出来人后再结束程序。
一定要用application.terminate,close不可以
GZ