delphi 生成验证码

procedure TFormTwo.Button1Click(Sender: TObject);
begin
  if LowerCase(Edit1.Text) = LowerCase(str) then
  begin
    ShowMessage('验证码正确!');
  end
  else
  begin
    ShowMessage('验证码错误');
  end;
end;

procedure TFormTwo.Button2Click(Sender: TObject);
begin
  str := DrawImg(Image1);
end;

function TFormTwo.DrawImg(img: TImage): string;
var
  I,j,k:   Integer;
  vPoint:   TPoint;
  vLeft:   Integer;
  arrStr:array [1..62]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';
  arrStr[37] := 'a'; arrStr[38]:='b'; arrStr[39]:='c'; arrStr[40]:='d';
  arrStr[41] := 'e'; arrStr[42]:='f'; arrStr[43]:='g'; arrStr[44]:='h';
  arrStr[45] := 'i'; arrStr[46]:='j'; arrStr[47]:='k'; arrStr[48]:='l';
  arrStr[49] := 'm'; arrStr[50]:='n'; arrStr[51]:='o'; arrStr[52]:='p';
  arrStr[53] := 'q'; arrStr[54]:='r'; arrStr[55]:='s'; arrStr[56]:='t';
  arrStr[57] := 'u'; arrStr[58]:='v'; arrStr[59]:='w'; arrStr[60]:='x';
  arrStr[61] := 'y'; arrStr[62]:='z';
  For j:=1 to 4  do
  begin
    Randomize;
    k := strtoint(Format('%.1d',[Random(62)]));
    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 TFormTwo.FormCreate(Sender: TObject);
begin
  str := DrawImg(Image1);
end;

 

posted @ 2020-07-09 15:30  夏天的西瓜君  阅读(415)  评论(0编辑  收藏  举报