狼行神码

导航

Delphi做验证码登录窗口

在五月麦田的帮助下做成了,感觉挺好,验证码输入的时候需要大写:

组件: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 [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.

 

其实也可以这样写:

  

  代码如下:

  

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 [1..36]of string ;
begin
  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';
  Image1.Canvas.TextOut(5 ,5,arrStr[random(36)]);
  Image1.Canvas.TextOut(25,5,arrStr[random(36)]);
  Image1.Canvas.TextOut(45,5,arrStr[random(36)]);
  Image1.Canvas.TextOut(65,5,arrStr[random(36)]);
end;

end

 

 

  

posted on 2017-05-05 14:44  狼行神码  阅读(1466)  评论(0编辑  收藏  举报