如何在主Form出现之前,弹出密码验证From,Cancel就退出程序,Ok后密码正确才出现主Form

如何在主Form出现之前,弹出密码验证From,Cancel就退出程序,Ok后密码正确才出现主Form
本文地址 :CodeGo.net/5175478/ 
------------------------------------------------------------------------------------------------------------------------- 
1.我给你一个LOGINFORM的例子:
unit LoginFrm;
interface
uses WinTypes, WinProcs, Classes, Graphics, Forms, Controls, StdCtrls,
Buttons, ExtCtrls;
type
TLoginForm = class(TForm)
lblEnterPassword: TLabel;
lblEnterName: TLabel;
edtName: TEdit;
edtPassword: TEdit;
btnOK: TButton;
btnCancel: TButton;
public
end;
function GetLoginParams(ALoginParams: TStrings): Boolean;
implementation
{$R *.DFM}
function GetLoginParams(ALoginParams: TStrings): Boolean;
var
LoginForm: TLoginForm;
begin
Result := False;
LoginForm := TLoginForm.Create(Application);
try
if LoginForm.ShowModal = mrOk then
begin
ALoginParams.Values['USER NAME'] := LoginForm.edtName.Text;
ALoginParams.Values['PASSWORD'] := LoginForm.edtPassWord.Text;
Result := True;
end;
finally
LoginForm.Free;
end;
end;
end. 
2. 自己先做个登录窗体
在工程文件里先USES 那个单元
再写......
Application.Initialize;
frmlogon:= Tfrmlogon.Create(Application);
frmlogon.ShowModal;
frmlogon.Update;
if frmlogon.ModalResult = mrOK then
begin
frmlogon.Free;
frmlogon.Hide;
Application.CreateForm(TForm1, Form1);
Application.Run;
end 
3. procedure TLoginForm.btnLoginClick(Sender: TObject);
var
ComputerName: String;
CNameBuffer : PChar;
fl_loaded : Boolean;
CLen : ^DWord;
intLogin:Integer;
begin
//取得计算机名称
GetMem(CNameBuffer,255);
New(CLen);
CLen^:= 255;
fl_loaded := GetComputerName(CNameBuffer,CLen^);
if fl_loaded then
ComputerName := StrPas(CNameBuffer)
else
ComputerName := 'Unknown';
FreeMem(CNameBuffer,255);
Dispose(CLen);
//登录系统
if edtUser.Text='' then
begin
MessageBox(Self.Handle,'输入用户!','提示',MB_OK+MB_ICONEXCLAMATION);
edtUser.SetFocus;
exit;
end;
if ComboBox1.items.Count>0 then
begin
if ComboBox1.ItemIndex=-1 then
begin
MessageBox(Self.Handle,'选择帐套!','提示',MB_OK+MB_ICONEXCLAMATION);
ComboBox1.SetFocus;
exit;
end;
end;
//登录系统
if blnFirstLogin=False then dmlPreserve.scPreserve.AppServer.LogoutStatus(CurUserID);
dmlPreserve.scPreserve.AppServer.LoginStatus(edtUser.Text,edtPwd.Text,ComputerName,intLogin,curUserName);
Case intLogin of
0: //成功登录
begin
MessageBox(Self.Handle,'登录成功!','提示',MB_OK+MB_ICONEXCLAMATION);
Fini.WriteString('Preserve','LastUser',edtUser.Text);
Fini.WriteString('Preserve','LastAccount',ComboBox1.Text);
curAccount:=ComboBox1.Text;
MDIMainForm.sbrMain.Panels[3].Text:='当前帐套:'+ComboBox1.Text;
curUserID:=edtUser.Text;
MDIMainForm.sbrMain.Panels[2].Text:='操作用户:'+curUserName;
dmlPreserve.cdsusrUser.Close;
if blnFirstLogin=True then blnFirstLogin:=False;
Close;
end;
1:
begin
MessageBox(Self.Handle,'该用户不存在!','提示',MB_OK+MB_ICONEXCLAMATION);
edtUser.SetFocus;
end;
2:
begin
MessageBox(Self.Handle,'该用户不允许在此计算机上登录!','提示',MB_OK+MB_ICONEXCLAMATION);
edtUser.SetFocus;
end;
3:
begin
MessageBox(Self.Handle,'密码错误!','提示',MB_OK+MB_ICONEXCLAMATION);
edtPwd.SetFocus;
end;
4:
begin
MessageBox(Self.Handle,'该用户已经登录!','提示',MB_OK+MB_ICONEXCLAMATION);
edtUser.SetFocus;
end;
5:
begin
if MessageBox(Self.Handle,'该用户已经登录,是否强行登录?','提示',MB_YESNO+MB_ICONQUESTION)=mrNO then
edtUser.SetFocus;
end;
6:
begin
MessageBox(Self.Handle,'登录中遇到未知错误!','错误',MB_OK+MB_ICONEXCLAMATION);
edtUser.SetFocus;
end;
end;
end 
4. procedure TMDIMainForm.MDIMainFormOnActivate(Sender: TObject);
begin
LoginForm:=TLoginForm.Create(Self);
LoginForm.ShowModal ;
end;
procedure TLoginForm.btnCancelClick(Sender: TObject);
begin
Application.Terminate
end 
5. program Project1;
uses
Forms,
Dialogs,
Unit1 in 'Unit1.pas' {Form1};
{$R *.RES}
Var
Pass:String;
begin
Application.Initialize;
InputQuery('密码','输入密码',Pass);
if Pass<>'OK' then Application.Terminate;
Application.CreateForm(TForm1, Form1);
Application.Run;
end.
本文标题 :如何在主Form出现之前,弹出密码验证From,Cancel就退出程序,Ok后密码正确才出现主Form
本文地址 :CodeGo.net/5175478/ 

posted on 2017-11-18 22:11  癫狂编程  阅读(347)  评论(0编辑  收藏  举报

导航

好的代码像粥一样,都是用时间熬出来的