Delphi做验证码登录

 

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 [1..36]of string ;
  strResult: string;
begin
  strResult := '';
  arrStr[1] := '0';  arrStr[2]:='1';  arrStr[3]:='2';  arrStr[4]:='3';
  arrStr[5] := '4';  arrStr[6]:='5';  arrStr[7]:='6';  arrStr[8]:='7';
  arrStr[9] := '8';  arrStr[10]:='9'; arrStr[11]:='A'; arrStr[12]:='B';
  arrStr[13] := 'C'; arrStr[14]:='D'; arrStr[15]:='E'; arrStr[16]:='F';
  arrStr[17] := 'G'; arrStr[18]:='H'; arrStr[19]:='I'; arrStr[20]:='J';
  arrStr[21] := 'K'; arrStr[22]:='L'; arrStr[23]:='M'; arrStr[24]:='N';
  arrStr[25] := 'O'; arrStr[26]:='P'; arrStr[27]:='Q'; arrStr[28]:='R';
  arrStr[29] := 'S'; arrStr[30]:='T'; arrStr[31]:='U'; arrStr[32]:='V';
  arrStr[33] := 'W'; arrStr[34]:='X'; arrStr[35]:='Y'; arrStr[36]:='Z';
  For j:=1 to 4  do
  begin
    Randomize;
    k := strtoint(Format('%.1d',[Random(36)]));
    strResult := strResult + trim(arrStr[k])
  end;
  vLeft := 10;
  img.picture := nil;
  for I := 1 to Length(strResult) do
  begin
    with Img do
    begin
      Canvas.Font.Size := Random(10)+ 9;
      Canvas.Font.Color := RGB(Random(256) and $C0,
      Random(256) and $C0, Random(256) and $C0);
      if Random(2)=1 then
        Canvas.Font.Style := [fsBold]
      else Canvas.Font.Style := [];
      begin
        Canvas.Font.Name := Screen.Fonts[10];
        vPoint.X := Random(4)+ vLeft;
        vPoint.Y := Random(5);
        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.

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值