⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mysqlauthdialog.pas

📁 通过Tmysql来访问MSQL Server数据库的应用案例.
💻 PAS
字号:
unit mySQLAuthDialog;                         

{
  Unit:    TmySQLAuthDialog - Authentication Dialog Form
  Project: TmySQL - http://www.productivity.org/projects/tmysql
  Author:  Justin P. Yunke <yunke@productivity.org>
  Date:    November 1998 - July 2001
  ** Join the TmySQL mailing list at http://www.elists.org **

  Copyrights/Credits:
    Copyright (c) 2001 Justin P. Yunke <yunke@productivity.org>

  License:
    Open Source.  You must retain copyrights and distribution information
    listed in these files.

  Description:
    This unit provides a class, TFmySQLAuthDialog, which presents a Username/
  Password dialog box to the user for ease in authentication.

  Usage Example:
    var F: TFmySQLAuthDialog;
    begin
      F:=TFmySQLAuthDialog.Create(Self);
      F.Setup(
        Banner: String,
        Username: String,
        Password: String,
        OnOKButton: procedure(Sender: TObject) of object -- see TOnButton,
        OnCancelButton: procedure(Sender: TObject) of object -- see TOnButton,
        Options: [] set -- see TAuthDialogOptions
      );
    end;

  Requirements:
    Delphi 3.0

  Date Log:
    11.18.1998a: Created, tested.
    12.02.1998a: Added AuthDialog_DontHideNobodyUsername option.
    01.08.2000a: Added To/FromRotX "weak encryption" routines for
                 help in Saving Passwords without cleartext
}

interface

uses
  Windows, Messages, SysUtils, Classes, Forms, StdCtrls, Controls;

const
{Last Modification Information}
  TMYSQLAUTHDIALOG_VERSION       = '2.0a';
  TMYSQLAUTHDIALOG_LAST_MODIFIED = '07.23.2001a';
  TMYSQLAUTHDIALOG_LAST_AUTHOR   = 'jy';

type
  TAuthDialogOptions=(
    AuthDialog_DontHideNobodyUsername,
    AuthDialog_DontAutoFocusOnPassword,
    AuthDialog_DontAutoPositionDialogInCenter,
    AuthDialog_DontSelectAllWhenFocused,
    AuthDialog_DontAsteriskPassword,
    AuthDialog_DontDoUsernameReturnHack,
    AuthDialog_SavePassword
  );

  TSetAuthDialogOptions = set of TAuthDialogOptions;

  TOnButton = procedure(Sender: TObject) of object;

  TFmySQLAuthDialog = class(TForm)
    Username:   TEdit;
    Password:   TEdit;
    Banner:     TLabel;

    L_Username: TLabel;
    L_Password: TLabel;

    B_OK:       TButton;
    B_Cancel:   TButton;
    CB_SavePassword: TCheckBox;

    procedure FormCreate(Sender: TObject);
    procedure B_OKClick(Sender: TObject);
    procedure B_CancelClick(Sender: TObject);
    procedure UsernameKeyPress(Sender: TObject; var Key: Char);
  private
    FOnOKButton,
    FOnCancelButton : TOnButton;

    FOptions : TSetAuthDialogOptions;

    procedure PutSavePassword(B : boolean);
    function  GetSavePassword : boolean;
  public
    property  OnOkButton     : TOnButton read FOnOKButton write FOnOKButton;
    property  OnCancelButton : TOnButton read FOnCancelButton write FOnCancelButton;
    property  SavePassword   : Boolean read GetSavePassword write PutSavePassword;

    procedure Setup(const BannerMsg, User, Pass: string; EventOK: TOnButton; EventCancel: TOnButton; const Options: TSetAuthDialogOptions);
  end;

function ToRotX(const Num : integer; const CryptText : string) : string;
function FromRotX(const Num : integer; const CryptText : string) : string;

var
  FmySQLAuthDialog: TFmySQLAuthDialog;

implementation

{$R *.DFM}

procedure TFmySQLAuthDialog.FormCreate(Sender: TObject);
begin
  FOnOkButton:=nil;
  FOnCancelButton:=nil;
  FOptions:=[];
end;

procedure TFmySQLAuthDialog.B_OKClick(Sender: TObject);
begin
  if (not (AuthDialog_DontDoUsernameReturnHack in FOptions)) and (Username.Focused) then begin
    Password.SetFocus;
    exit;
  end;

  Hide;
  if Assigned(FOnOKButton) then FOnOkButton(self);
  Release;
end;

procedure TFmySQLAuthDialog.B_CancelClick(Sender: TObject);
begin
  Hide;
  if Assigned(FOnCancelButton) then FOnCancelButton(self);
  Release;
end;

procedure TFmySQLAuthDialog.Setup(const BannerMsg, User, Pass: string; EventOK: TOnButton; EventCancel: TOnButton; const Options: TSetAuthDialogOptions);
var
  R : TRect;
begin
  FOptions:=Options;

  if not (AuthDialog_SavePassword in FOPtions) then
    CB_SavePassword.Enabled:=FALSE;

  if AuthDialog_DontAsteriskPassword in FOptions then
    Password.PasswordChar:=#0;

  Banner.Caption:=BannerMsg;

  Caption:='Authentication: '+BannerMsg;

  if (User='nobody') and (not (AuthDialog_DontHideNobodyUsername in FOptions)) then
    Username.Text :=''
  else
    Username.Text :=User;

  Password.Text :=Pass;

  if not (AuthDialog_DontAutoPositionDialogInCenter in FOptions) then begin
    SystemParametersInfo( SPI_GETWORKAREA, 0, @R, 0 );

    Left:=R.Left+((R.Right-R.Left) div 2) - (Width div 2);
    Top :=R.Top+ ((R.Bottom-R.Top) div 2) - (Height div 2);
  end;

  Show;

  if (not (AuthDialog_DontAutoFocusOnPassword in FOptions)) and (UserName.Text<>'') then
    Password.SetFocus;

  if AuthDialog_DontSelectAllWhenFocused in FOptions then begin
    if Password.Focused then
      Password.SelStart:=Length(Password.Text)
    else
      Username.SelStart:=Length(Username.Text);
  end else begin
    if Password.Focused then
      Password.SelectAll
    else
      Username.SelectAll;
  end;

  FOnOKButton:=EventOK;
  FOnCancelButton:=EventCancel;
end;

function TFmySQLAuthDialog.GetSavePassword : boolean;
begin
  Result:=CB_SavePassword.Checked;
end;

procedure TFmySQLAuthDialog.PutSavePassword(B : boolean);
begin
  CB_SavePassword.Checked:=B;
end;

function ToRotX(const Num : integer; const CryptText : string) : string;
var
  sTemp : string;
  i : integer;
  b : byte;
  LowerC, UpperC : boolean;
begin
  sTemp:='';

  for i:=1 to Length(CryptText) do begin
    b:=Ord(CryptText[i]);

    LowerC:=(b>=65) and (b<=90);
    UpperC:=(b>=97) and (b<=122);

    if (LowerC) or (UpperC) then begin
      b:=b+Num;
      if LowerC then begin
        if      b<65 then b:=b+26
        else if b>90 then b:=b-26;
      end else if UpperC then begin
        if      b<97  then b:=b+26
        else if b>122 then b:=b-26;
      end;
      sTemp:=sTemp+Chr(b);
    end
    else
      sTemp:=sTemp+Chr(b);
  end;

  Result:=sTemp;
end;

function FromRotX(const Num : integer; const CryptText : string) : string;
begin
  Result:=ToRotX(-Num,CryptText);
end;

procedure TFmySQLAuthDialog.UsernameKeyPress(Sender: TObject;
  var Key: Char);
begin
  if Key=#27 then
    B_CancelClick(self);
end;

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -