在你的mainform的FormCreate中添加 try begin Frmlogin := TFrmLogin.Create(self); FrmLogin.ShowModal; bValidUser := frmLogin.bValid end finally FrmLogin.Free; end; if not bValidUser then Application.Terminate;去掉project source中的 Application.CreateForm(TFrmlogin, Frmlogin)或在Project->Option中的AutoCreate项中去掉frmLogin。
to: zhourongbiao(Edward) 此方法可以,但是不管點擊登錄窗口上的任何按鈕,都會將主窗口直接顯示出來,比如沒通過驗証也會顯示主窗口,這又是何原因呢?
直接修改工程文件不好吗? var MutexHandle: THandle; hPrevInst: Boolean; ErrorRecord: TErrorRec; FileName: string; begin MutexHandle := CreateMutex(nil, True, ''); FileName := ExtractFilePath(Application.ExeName) + 'ErrorLog.Ini'; try hPrevInst := (MutexHandle <> 0) and (GetLastError = ERROR_ALREADY_EXISTS); if not hPrevInst then begin Application.Initialize; Application.Title := ''; Application.CreateForm(TDMFrm, DMFrm); if DMFrm.Connected=True then begin ErrorRecord := TErrorRec.Create(FileName);//这个是全局的错误对象 ErrorRecord.Active := True; SplashForm := TSplashForm.Create(nil);//登陆窗口 try Splashform.Show; splashFormStepIt; SplashFormStepIt; Application.ProcessMessages; RightObject := TRightObject.Create(nil);//全局的权限对象 SplashFormStepIt; SplashFormStepIt; SplashFormStepIt; SplashFormStepIt; SplashFormStepIt; if SplashForm.Login then//验证 begin RightObject.UserName := Trim(SplashForm.userCBox.Text); SplashFormStepIt; RightObject.Password := Trim(SplashForm.passEdit.Text); SplashFormStepIt; RightObject.LoginDate := Now; SplashFormStepIt; SplashFormStepIt; SplashFormStepIt; SplashFormStepIt; SplashFormStepIt; FreeAndNil(SplashForm); Application.ProcessMessages; Application.CreateForm(TMainFrm, MainFrm);//主窗口 Application.Run; end; except if Assigned(SplashForm) then FreeAndNil(SplashForm); end; end end; finally CloseHandle(MutexHandle); if RightObject <> nil then FreeAndNil(RightObject); if ErrorRecord <> nil then FreeAndNil(ErrorRecord); end; //end; end.
修改工程文件 Application.Initialize; frmpass:=Tfrmpass.Create(application); if frmpass.ShowModal = 1 then begin frmpass.Visible:=false; .................//显示主窗体 end else begin application.terminated; exit; end; end. 在frmpass的确定按钮中,判断密码,如果正确则,modalresult:=mb_Ok;否则modalresult=mb_cancel
to: chw_csdn_chw(chw) 我希望如果一次輸入錯誤可允許多次輸入,按你的方法只能一次,通過則繼續運行,否則即中止運行.
with TfrmLogin.Create(nil) do try ShowModal; while not (Application.Terminated or Finished) do Application.HandleMessage; if Application.Terminated then Exit; Application.CreateForm(TfrmMain, frmMain); finally Free; end; Application.Run; 在LoginForm的取消按钮或关闭过程加上 if Application.MainForm = nil then Application.Terminate
呵呵 还是在主窗体的onshow里麻烦少一点
这是我一直用的方法,供大家参考 ----------------------------------------------------------------------- procedure TFmMain.FormShow(Sender: TObject); begin fmlogin.ShowModal ; //显示登录确认界面 end; ------------------------------------------------------------------------- procedure TfmLogin.BitBtn2Click(Sender: TObject); begin application.Terminate ; end;procedure TfmLogin.FormCreate(Sender: TObject); begin //得到所有的操作员名称填充列表选项框 if dsusers.DataSet.Active = false then dsusers.DataSet.Active := true; combobox1.Items := getusername();//得到所有操作员名称填充ComboBox EnterCount := 0 end;procedure TfmLogin.BitBtn1Click(Sender: TObject); begin if userlogin(combobox1.Text,edit1.Text) then //如果通过身份认证,则关闭登录窗体 close else begin msg('密码不正确请重新输入','密码错误'); entercount := entercount + 1; if entercount >= 3 then application.Terminate end; end;procedure TfmLogin.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState); begin if key = 13 then keybd_event(vk_tab,0,0,0); end;end.
to cnj79 () 你可以在frmpass窗体的 ''确定'按钮中,加一个循环,如果n次未通过认证,mordalresult:=false; 某次通过认证 mordalresult:=true;to zhusongdong(东) 这是我一直用的方法,供大家参考 ----------------------------------------------------------------------- procedure TFmMain.FormShow(Sender: TObject); begin fmlogin.ShowModal ; //显示登录确认界面 end;最好是先验证密码再创建主窗体,否则如果主窗体需要初始化很多东西,声明很多变量,但用户没通过认证,岂不是很浪费资源,又占时间.即使主窗体不需要初始化很多东西,把它载入内存也是费时,费空间的.
try
begin
Frmlogin := TFrmLogin.Create(self);
FrmLogin.ShowModal;
bValidUser := frmLogin.bValid
end
finally
FrmLogin.Free;
end; if not bValidUser then
Application.Terminate;去掉project source中的 Application.CreateForm(TFrmlogin, Frmlogin)或在Project->Option中的AutoCreate项中去掉frmLogin。
此方法可以,但是不管點擊登錄窗口上的任何按鈕,都會將主窗口直接顯示出來,比如沒通過驗証也會顯示主窗口,這又是何原因呢?
var
MutexHandle: THandle;
hPrevInst: Boolean;
ErrorRecord: TErrorRec;
FileName: string;
begin
MutexHandle := CreateMutex(nil, True, '');
FileName := ExtractFilePath(Application.ExeName) + 'ErrorLog.Ini';
try
hPrevInst := (MutexHandle <> 0) and (GetLastError = ERROR_ALREADY_EXISTS);
if not hPrevInst then
begin
Application.Initialize;
Application.Title := '';
Application.CreateForm(TDMFrm, DMFrm);
if DMFrm.Connected=True then
begin
ErrorRecord := TErrorRec.Create(FileName);//这个是全局的错误对象
ErrorRecord.Active := True;
SplashForm := TSplashForm.Create(nil);//登陆窗口
try
Splashform.Show;
splashFormStepIt;
SplashFormStepIt;
Application.ProcessMessages;
RightObject := TRightObject.Create(nil);//全局的权限对象
SplashFormStepIt;
SplashFormStepIt;
SplashFormStepIt;
SplashFormStepIt;
SplashFormStepIt;
if SplashForm.Login then//验证
begin
RightObject.UserName := Trim(SplashForm.userCBox.Text);
SplashFormStepIt;
RightObject.Password := Trim(SplashForm.passEdit.Text);
SplashFormStepIt;
RightObject.LoginDate := Now;
SplashFormStepIt;
SplashFormStepIt;
SplashFormStepIt;
SplashFormStepIt;
SplashFormStepIt;
FreeAndNil(SplashForm);
Application.ProcessMessages;
Application.CreateForm(TMainFrm, MainFrm);//主窗口
Application.Run;
end;
except
if Assigned(SplashForm) then
FreeAndNil(SplashForm);
end;
end
end;
finally
CloseHandle(MutexHandle);
if RightObject <> nil then
FreeAndNil(RightObject);
if ErrorRecord <> nil then
FreeAndNil(ErrorRecord);
end;
//end;
end.
Application.Initialize;
frmpass:=Tfrmpass.Create(application);
if frmpass.ShowModal = 1 then begin
frmpass.Visible:=false;
.................//显示主窗体
end else begin
application.terminated;
exit;
end;
end.
在frmpass的确定按钮中,判断密码,如果正确则,modalresult:=mb_Ok;否则modalresult=mb_cancel
我希望如果一次輸入錯誤可允許多次輸入,按你的方法只能一次,通過則繼續運行,否則即中止運行.
try
ShowModal;
while not (Application.Terminated or Finished) do
Application.HandleMessage;
if Application.Terminated then Exit;
Application.CreateForm(TfrmMain, frmMain);
finally
Free;
end;
Application.Run;
在LoginForm的取消按钮或关闭过程加上
if Application.MainForm = nil then
Application.Terminate
-----------------------------------------------------------------------
procedure TFmMain.FormShow(Sender: TObject);
begin
fmlogin.ShowModal ; //显示登录确认界面
end;
-------------------------------------------------------------------------
procedure TfmLogin.BitBtn2Click(Sender: TObject);
begin
application.Terminate ;
end;procedure TfmLogin.FormCreate(Sender: TObject);
begin
//得到所有的操作员名称填充列表选项框
if dsusers.DataSet.Active = false then
dsusers.DataSet.Active := true;
combobox1.Items := getusername();//得到所有操作员名称填充ComboBox
EnterCount := 0
end;procedure TfmLogin.BitBtn1Click(Sender: TObject);
begin
if userlogin(combobox1.Text,edit1.Text) then //如果通过身份认证,则关闭登录窗体
close
else
begin
msg('密码不正确请重新输入','密码错误');
entercount := entercount + 1;
if entercount >= 3 then application.Terminate
end;
end;procedure TfmLogin.FormKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
if key = 13 then
keybd_event(vk_tab,0,0,0);
end;end.
你可以在frmpass窗体的 ''确定'按钮中,加一个循环,如果n次未通过认证,mordalresult:=false;
某次通过认证
mordalresult:=true;to zhusongdong(东)
这是我一直用的方法,供大家参考
-----------------------------------------------------------------------
procedure TFmMain.FormShow(Sender: TObject);
begin
fmlogin.ShowModal ; //显示登录确认界面
end;最好是先验证密码再创建主窗体,否则如果主窗体需要初始化很多东西,声明很多变量,但用户没通过认证,岂不是很浪费资源,又占时间.即使主窗体不需要初始化很多东西,把它载入内存也是费时,费空间的.