unit Captcha;

interface

uses
  Windows, IB_Access, Messages, SysUtils, Variants, Classes, Graphics,
  Controls, Forms, Dialogs, TelaComum, cxGraphics, cxLookAndFeels,
  cxLookAndFeelPainters, Menus, ImgList, StdCtrls, cxButtons,
  ExtCtrls, dxGDIPlusClasses, 
  Buttons, System.ImageList;

type
  TfrmCaptcha = class(TfrmTelaComum)
    edtCapcha: TLabeledEdit;
    lblCaptcha: TLabel;
    Image1: TImage;
    lblTexto: TLabel;
    Label1: TLabel;
    procedure BtGravarClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Label1Click(Sender: TObject);
  private
    { Private declarations }
    function GerarCapcha : string;
  public
    { Public declarations }
  end;

var
  frmCaptcha: TfrmCaptcha;

implementation

{$R *.dfm}

procedure TfrmCaptcha.BtGravarClick(Sender: TObject);
begin
  inherited;
   if edtCapcha.Text <> lblCaptcha.Caption then
   begin
      MessageDlg('Os Caracteres não conferem', mtInformation, [mbOK], 0);
      lblCaptcha.Caption := GerarCapcha;
      edtCapcha.Clear;
      edtCapcha.SetFocus;

      Exit;
   end;

   ModalResult := mrOk;
end;

procedure TfrmCaptcha.FormCreate(Sender: TObject);
begin
  inherited;
   lblCaptcha.Caption := GerarCapcha;
end;

function TfrmCaptcha.GerarCapcha: string;
const str='1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ';
var i : Integer;
begin
   Result := '';
   Randomize;
   for i := 1 to 5 do
      Result := Result +str[random(length(str))+1];

end;

procedure TfrmCaptcha.Label1Click(Sender: TObject);
begin
  inherited;
 lblCaptcha.Caption := GerarCapcha;
end;

end.
