1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
| unit LoginF;
// copyright(c) hydonlee, 转载请注明原址
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ExtCtrls, StdCtrls, Buttons;
type
TfrmLogin = class(TForm)
pnl1: TPanel;
edUserName: TEdit;
lblUserName: TLabel;
edPasswd: TEdit;
lblPasswd: TLabel;
btnOK: TBitBtn;
btnCancel: TBitBtn;
procedure FormCreate(Sender: TObject);
procedure btnOKClick(Sender: TObject);
private
FTryTimes: Integer;
function CheckUserInfo(AUserName, APassword: string): Boolean;
public
end;
function Login: Boolean; //登录函数
3.登录窗口如何写?
我说:这个比较简单,直接上代码吧。有不懂的再交流。
unit LoginF;
// copyright(c) hydonlee, 转载请注明原址
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ExtCtrls, StdCtrls, Buttons;
type
TfrmLogin = class(TForm)
pnl1: TPanel;
edUserName: TEdit;
lblUserName: TLabel;
edPasswd: TEdit;
lblPasswd: TLabel;
btnOK: TBitBtn;
btnCancel: TBitBtn;
procedure FormCreate(Sender: TObject);
procedure btnOKClick(Sender: TObject);
private
FTryTimes: Integer;
function CheckUserInfo(AUserName, APassword: string): Boolean;
public
end;
function Login: Boolean; //登录函数
implementation
{$R *.DFM}
function Login: Boolean;
begin
//动态创建登录窗口
with TfrmLogin.Create(nil) do
begin
//只有返回OK的时候认为登录成功
Result := ShowModal() = mrOk;
Free;
end;
end;
function TfrmLogin.CheckUserInfo(AUserName, APassword: string): Boolean;
begin
//简单的验证,可以修改为其它的方式
Result := (AUserName = 'lihd' ) and (APassword = 'P@ssw0rd');
end;
procedure TfrmLogin.FormCreate(Sender: TObject);
begin
//尝试登录的次数,初始化为0
FTryTimes := 0;
end;
procedure TfrmLogin.btnOKClick(Sender: TObject);
begin
Inc(FTryTimes); //尝试登录的次数 + 1
if CheckUserInfo(edUserName.Text, edPasswd.Text) then
begin
//如果通过检查,返回OK
Self.ModalResult := mrOk;
end else
begin
if (FTryTimes > 2) then
begin
MessageBox(self.Handle, '你已经尝试登录3次,请联系系统管理员。', '错误', MB_OK + MB_ICONERROR);
Self.ModalResult := mrCancel;
Exit;
end;
//通不过,报错
MessageBox(self.Handle, '错误的用户名或密码,请重新输入!', '错误', MB_OK + MB_ICONERROR);
edUserName.SetFocus();
edUserName.SelectAll();
end;
end;
end. |