在五月麦田的帮助下做成了,感觉挺好,验证码输入的时候需要大写:
组件:LabelEdit 3个 Button 2个 image 1个。
代码如下:
unit Unit1; interface uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ExtCtrls; type
TForm1 = class(TForm)
Image1: TImage;
LabeledEdit1: TLabeledEdit;
LabeledEdit2: TLabeledEdit;
LabeledEdit3: TLabeledEdit;
Button1: TButton;
Button2: TButton;
procedure Button2Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure Image1Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
function DrawImg(img: TImage): string;
public
{ Public declarations }
end; var
Form1: TForm1;
var
str:string;
implementation {$R *.dfm} function TForm1.DrawImg(img: TImage): string; //画出验证码函数
var
I,j,k: Integer;
vPoint: TPoint;
vLeft: Integer;
arrStr:array [..]of string ;
strResult: string;
begin
strResult := '';
arrStr[] := ''; arrStr[]:=''; arrStr[]:=''; arrStr[]:='';
arrStr[] := ''; arrStr[]:=''; arrStr[]:=''; arrStr[]:='';
arrStr[] := ''; arrStr[]:=''; arrStr[]:='A'; arrStr[]:='B';
arrStr[] := 'C'; arrStr[]:='D'; arrStr[]:='E'; arrStr[]:='F';
arrStr[] := 'G'; arrStr[]:='H'; arrStr[]:='I'; arrStr[]:='J';
arrStr[] := 'K'; arrStr[]:='L'; arrStr[]:='M'; arrStr[]:='N';
arrStr[] := 'O'; arrStr[]:='P'; arrStr[]:='Q'; arrStr[]:='R';
arrStr[] := 'S'; arrStr[]:='T'; arrStr[]:='U'; arrStr[]:='V';
arrStr[] := 'W'; arrStr[]:='X'; arrStr[]:='Y'; arrStr[]:='Z';
For j:= to do
begin
Randomize;
k := strtoint(Format('%.1d',[Random()]));
strResult := strResult + trim(arrStr[k])
end;
vLeft := ;
img.picture := nil;
for I := to Length(strResult) do
begin
with Img do
begin
Canvas.Font.Size := Random()+ ;
Canvas.Font.Color := RGB(Random() and $C0,
Random() and $C0, Random() and $C0);
if Random()= then
Canvas.Font.Style := [fsBold]
else Canvas.Font.Style := [];
begin
Canvas.Font.Name := Screen.Fonts[];
vPoint.X := Random()+ vLeft;
vPoint.Y := Random();
Canvas.TextOut(vPoint.X, vPoint.Y,strResult[I]);
vLeft := vPoint.X + Canvas.TextWidth(strResult[I]);
end;
end;
end;
result := strResult; //返回值
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
str := DrawImg(Image1);
end; procedure TForm1.Image1Click(Sender: TObject);
begin
str := DrawImg(Image1);
end; procedure TForm1.Button1Click(Sender: TObject);
begin
if (LabeledEdit1.Text = 'Hello') and (LabeledEdit2.Text = 'world') and (LabeledEdit3.Text = str) then
MessageBox(Handle, '登陆成功!', '提示', MB_OK); if LabeledEdit1.Text <> 'Hello' then
begin
MessageBox(Handle, '用户名错误,请重新输入!', '提示', MB_OK);
LabeledEdit1.Text := '';
LabeledEdit2.Text := '';
LabeledEdit3.Text := '';
str := DrawImg(Image1);
end else if LabeledEdit2.Text <> 'world' then
begin
MessageBox(Handle, '密码错误,请重新输入!', '提示', MB_OK);
LabeledEdit1.Text := '';
LabeledEdit2.Text := '';
LabeledEdit3.Text := '';
str := DrawImg(Image1);
end else if LabeledEdit3.Text <> str then
begin
MessageBox(Handle, '验证码错误,请重新输入!', '提示', MB_OK);
LabeledEdit1.Text := '';
LabeledEdit2.Text := '';
LabeledEdit3.Text := '';
str := DrawImg(Image1);
end;
end; procedure TForm1.Button2Click(Sender: TObject); //退出
begin
close;
end; end.
其实也可以这样写:
代码如下:
unit Unit1; interface uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ExtCtrls; type
TForm1 = class(TForm)
Image1: TImage;
Button1: TButton;
Edit1: TEdit;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end; var
Form1: TForm1; implementation {$R *.dfm} procedure TForm1.Button1Click(Sender: TObject);
var
arrStr:array [..]of string ;
begin
arrStr[] := ''; arrStr[]:=''; arrStr[]:=''; arrStr[]:='';
arrStr[] := ''; arrStr[]:=''; arrStr[]:=''; arrStr[]:='';
arrStr[] := ''; arrStr[]:=''; arrStr[]:='A'; arrStr[]:='B';
arrStr[] := 'C'; arrStr[]:='D'; arrStr[]:='E'; arrStr[]:='F';
arrStr[] := 'G'; arrStr[]:='H'; arrStr[]:='I'; arrStr[]:='J';
arrStr[] := 'K'; arrStr[]:='L'; arrStr[]:='M'; arrStr[]:='N';
arrStr[] := 'O'; arrStr[]:='P'; arrStr[]:='Q'; arrStr[]:='R';
arrStr[] := 'S'; arrStr[]:='T'; arrStr[]:='U'; arrStr[]:='V';
arrStr[] := 'W'; arrStr[]:='X'; arrStr[]:='Y'; arrStr[]:='Z';
Image1.Canvas.TextOut( ,,arrStr[random()]);
Image1.Canvas.TextOut(,,arrStr[random()]);
Image1.Canvas.TextOut(,,arrStr[random()]);
Image1.Canvas.TextOut(,,arrStr[random()]);
end; end