Advanced Delphi Systems- Login Dialog

Yazdığınız makaleleri ve üyelerimizin işine yarayacağını düşündüğünüz kodlarınızı gönderebilirsiniz. Bu foruma soru sormayın!
Cevapla
Kullanıcı avatarı
Asri
Kıdemli Üye
Mesajlar: 767
Kayıt: 16 Eyl 2003 09:54
Konum: istanbul

Advanced Delphi Systems- Login Dialog

Mesaj gönderen Asri »

Aşağıdaki unit'i unit1'de uses olarak ekleyip bu unit içindeki procedure ve function'ları kullanbilirsiniz.

Bu unit program login dialog işleminde kullanılır.

Kod: Tümünü seç

unit ads_DlgLogin;
{Copyright(c)2000 Advanced Delphi Systems

 Richard Maley
 Advanced Delphi Systems
 12613 Maidens Bower Drive
 Potomac, MD 20854 USA
 phone 301-840-1554
 maley@advdelphisys.com

 The code herein can be used or modified by anyone.  Please retain references
 to Richard Maley at Advanced Delphi Systems.  If you make improvements to the
 code please send your improvements to maley@advdelphisys.com so that the
 entire Delphi community can benefit.  All comments are welcome.
}

(*
Description: ads_DlgLogin.pas.pas

This unit contains

*)

interface

{!~DlgLogin_ads

}
Function DlgLogin_ads(out UserName,Password: String): Boolean;

implementation


Uses
  ads_GraphicStrings,
  ads_Exception,
  Windows,
  Messages,
  SysUtils,
  Classes,
  Graphics,
  Controls,
  Forms,
  Dialogs,
  StdCtrls,
  ExtCtrls,
  Buttons
  ;

Var
  UnitName : String;
  ProcName : String;


type
  TfrmLogin = Class(TScrollingWinControl)
  Public
    Constructor Create(AOwner: TComponent); Override;
    Destructor  Destroy; Override;
  Public
    pnlLoginBase: TPanel;
    pnlButtonsBase: TPanel;
    pnlLoginEditBase0: TPanel;
    pnlLoginEditBase: TPanel;
    pnlServerBase0: TPanel;
    pnlServerBase: TPanel;
    pnlServerLabel: TPanel;
    pnlServerSpacer: TPanel;
    pnlServerName: TPanel;
    pnlIDBase: TPanel;
    pnlIDLabel: TPanel;
    pnlIDSpacer: TPanel;
    pnlIDEdit: TPanel;
    edtLogin: TEdit;
    pnlPwdBase: TPanel;
    pnlPwdLabel: TPanel;
    pnlPwdSpacer: TPanel;
    pnlPwdEdit: TPanel;
    edtPassword: TEdit;
    btnOK: TBitBtn;
    btnCancel: TBitBtn;
    procedure btnOKClick(Sender: TObject);
    procedure FormActivate(Sender: TObject);
  private
    FSuccess: Boolean;
    FPassword: String;
    FUserName: String;
    procedure SetPassword(const Value: String);
    procedure SetSuccess(const Value: Boolean);
    procedure SetUserName(const Value: String);
    { Private declarations }
  public
    { Public declarations }
  published
    property Success  : Boolean read FSuccess write SetSuccess;
    property UserName : String read FUserName write SetUserName;
    property Password : String read FPassword write SetPassword;
  end;



procedure TfrmLogin.btnOKClick(Sender: TObject);
Var
  sgLogin : String;
  sgPW    : String;
begin
  Success := False;
  sgLogin := Trim(edtLogin.Text);
  sgPW    := Trim(edtPassword.Text);
  If sgLogin = '' Then Exit;
  If sgPW    = '' Then Exit;
  UserName := sgLogin;
  Password := sgPW;
  Success := True;
end;

procedure TfrmLogin.FormActivate(Sender: TObject);
Var
  User_Name   : string;
  UserNameLen : Dword;
begin
  FSuccess    := False;
  FPassword   := '';
  UserNameLen := 255;
  SetLength(User_Name, UserNameLen);
  If GetUserName(PChar(User_Name), UserNameLen) Then
    FUserName := Copy(User_Name,1,UserNameLen - 1)
  Else
    FUserName := '';
  edtLogin.Text    := FUserName;
  edtPassword.Text := FPassword;
  pnlServerName.Caption := ExtractFileName(Application.ExeName);
  If FUserName = '' Then
    TForm(Owner).ActiveControl := edtLogin
  Else
    TForm(Owner).ActiveControl := edtPassword;
end;

procedure TfrmLogin.SetPassword(const Value: String);
begin
  If FPassword <> Value Then FPassword := Value;
  edtPassword.Text := FPassword;
end;

procedure TfrmLogin.SetSuccess(const Value: Boolean);
begin
  If FSuccess <> Value Then FSuccess := Value;
end;

procedure TfrmLogin.SetUserName(const Value: String);
begin
  If FUserName <> Value Then FUserName := Value;
  edtLogin.Text := FUserName;
end;


Constructor TfrmLogin.Create(AOwner: TComponent);
  Function IsControl(Obj: TObject): Boolean;
  Begin
    Result := (Obj is TControl);
  End;
Begin
  ProcName  := 'TfrmLogin.Create'; Try
  inherited;
  Self.Parent := TWincontrol(AOwner);

  pnlLoginBase := TPanel.Create(AOwner);
  With pnlLoginBase Do
  Begin
    If IsControl(pnlLoginBase) Then
    Begin
      Parent      := Self;
    End;
    Left          := 0;
    Top           := 0;
    Width         := 254;
    Height        := 170;
    Align         := alClient;
    BevelOuter    := bvNone;
    BorderWidth   := 5;
    Caption       := '  ';
    TabOrder      := 0;
  End;

  pnlButtonsBase := TPanel.Create(AOwner);
  With pnlButtonsBase Do
  Begin
    Parent        := pnlLoginBase;
    Left          := 5;
    Top           := 126;
    Width         := 244;
    Height        := 39;
    Align         := alBottom;
    BevelOuter    := bvNone;
    BorderWidth   := 5;
    Caption       := '  ';
    TabOrder      := 0;
  End;

  btnOK := TBitBtn.Create(AOwner);
  With btnOK Do
  Begin
    Parent        := pnlButtonsBase;
    Left          := 88;
    Top           := 8;
    Width         := 75;
    Height        := 25;
    Caption       := '&OK';
    TabOrder      := 0;
    OnClick       := btnOKClick;
    Kind          := bkOK;
  End;

  btnCancel := TBitBtn.Create(AOwner);
  With btnCancel Do
  Begin
    Parent        := pnlButtonsBase;
    Left          := 168;
    Top           := 8;
    Width         := 75;
    Height        := 25;
    TabOrder      := 1;
    Kind          := bkCancel;
  End;

  pnlLoginEditBase0 := TPanel.Create(AOwner);
  With pnlLoginEditBase0 Do
  Begin
    Parent        := pnlLoginBase;
    Left          := 5;
    Top           := 41;
    Width         := 244;
    Height        := 85;
    Align         := alClient;
    BevelInner    := bvRaised;
    BevelOuter    := bvLowered;
    BorderWidth   := 1;
    Caption       := '  ';
    TabOrder      := 1;
  End;

  pnlLoginEditBase := TPanel.Create(AOwner);
  With pnlLoginEditBase Do
  Begin
    Parent        := pnlLoginEditBase0;
    Left          := 3;
    Top           := 3;
    Width         := 238;
    Height        := 79;
    Align         := alClient;
    BevelOuter    := bvNone;
    BorderWidth   := 5;
    Caption       := '  ';
    TabOrder      := 0;
  End;

  pnlIDBase := TPanel.Create(AOwner);
  With pnlIDBase Do
  Begin
    Parent        := pnlLoginEditBase;
    Left          := 5;
    Top           := 5;
    Width         := 228;
    Height        := 34;
    Align         := alTop;
    BevelOuter    := bvNone;
    BorderWidth   := 5;
    Caption       := '  ';
    TabOrder      := 0;
  End;

  pnlIDLabel := TPanel.Create(AOwner);
  With pnlIDLabel Do
  Begin
    Parent        := pnlIDBase;
    Left          := 5;
    Top           := 5;
    Width         := 60;
    Height        := 24;
    Align         := alLeft;
    Alignment     := taRightJustify;
    BevelOuter    := bvNone;
    Caption       := '&User Name:';
    TabOrder      := 0;
  End;

  pnlIDSpacer := TPanel.Create(AOwner);
  With pnlIDSpacer Do
  Begin
    Parent        := pnlIDBase;
    Left          := 65;
    Top           := 5;
    Width         := 24;
    Height        := 24;
    Align         := alLeft;
    BevelOuter    := bvNone;
    Caption       := '  ';
    TabOrder      := 1;
  End;

  pnlIDEdit := TPanel.Create(AOwner);
  With pnlIDEdit Do
  Begin
    Parent        := pnlIDBase;
    Left          := 89;
    Top           := 5;
    Width         := 134;
    Height        := 24;
    Align         := alClient;
    Alignment     := taLeftJustify;
    BevelOuter    := bvNone;
    Caption       := '  ';
    TabOrder      := 2;
  End;

  edtLogin := TEdit.Create(AOwner);
  With edtLogin Do
  Begin
    Parent        := pnlIDEdit;
    Left          := 0;
    Top           := 0;
    Width         := 121;
    Height        := 21;
    TabOrder      := 0;
  End;

  pnlPwdBase := TPanel.Create(AOwner);
  With pnlPwdBase Do
  Begin
    Parent        := pnlLoginEditBase;
    Left          := 5;
    Top           := 39;
    Width         := 228;
    Height        := 34;
    Align         := alTop;
    BevelOuter    := bvNone;
    BorderWidth   := 5;
    Caption       := '  ';
    TabOrder      := 1;
  End;

  pnlPwdLabel := TPanel.Create(AOwner);
  With pnlPwdLabel Do
  Begin
    Parent        := pnlPwdBase;
    Left          := 5;
    Top           := 5;
    Width         := 60;
    Height        := 24;
    Align         := alLeft;
    Alignment     := taRightJustify;
    BevelOuter    := bvNone;
    Caption       := '&Password:';
    TabOrder      := 0;
  End;

  pnlPwdSpacer := TPanel.Create(AOwner);
  With pnlPwdSpacer Do
  Begin
    Parent        := pnlPwdBase;
    Left          := 65;
    Top           := 5;
    Width         := 24;
    Height        := 24;
    Align         := alLeft;
    BevelOuter    := bvNone;
    Caption       := '  ';
    TabOrder      := 1;
  End;

  pnlPwdEdit := TPanel.Create(AOwner);
  With pnlPwdEdit Do
  Begin
    Parent        := pnlPwdBase;
    Left          := 89;
    Top           := 5;
    Width         := 134;
    Height        := 24;
    Align         := alClient;
    Alignment     := taLeftJustify;
    BevelOuter    := bvNone;
    Caption       := '  ';
    TabOrder      := 2;
  End;

  edtPassword := TEdit.Create(AOwner);
  With edtPassword Do
  Begin
    Parent        := pnlPwdEdit;
    Left          := 0;
    Top           := 0;
    Width         := 121;
    Height        := 21;
    PasswordChar  := '*';
    TabOrder      := 0;
  End;

  pnlServerBase0 := TPanel.Create(AOwner);
  With pnlServerBase0 Do
  Begin
    Parent        := pnlLoginBase;
    Left          := 5;
    Top           := 5;
    Width         := 244;
    Height        := 36;
    Align         := alTop;
    BevelInner    := bvRaised;
    BevelOuter    := bvLowered;
    BorderWidth   := 1;
    Caption       := '  ';
    TabOrder      := 2;
  End;

  pnlServerBase := TPanel.Create(AOwner);
  With pnlServerBase Do
  Begin
    Parent        := pnlServerBase0;
    Left          := 3;
    Top           := 3;
    Width         := 238;
    Height        := 30;
    Align         := alClient;
    BevelOuter    := bvNone;
    BorderWidth   := 5;
    Caption       := '  ';
    TabOrder      := 0;
  End;

  pnlServerLabel := TPanel.Create(AOwner);
  With pnlServerLabel Do
  Begin
    Parent        := pnlServerBase;
    Left          := 5;
    Top           := 5;
    Width         := 64;
    Height        := 20;
    Align         := alLeft;
    Alignment     := taRightJustify;
    BevelOuter    := bvNone;
    Caption       := 'Server:';
    TabOrder      := 0;
  End;

  pnlServerSpacer := TPanel.Create(AOwner);
  With pnlServerSpacer Do
  Begin
    Parent        := pnlServerBase;
    Left          := 69;
    Top           := 5;
    Width         := 24;
    Height        := 20;
    Align         := alLeft;
    BevelOuter    := bvNone;
    Caption       := '  ';
    TabOrder      := 1;
  End;

  pnlServerName := TPanel.Create(AOwner);
  With pnlServerName Do
  Begin
    Parent        := pnlServerBase;
    Left          := 93;
    Top           := 5;
    Width         := 140;
    Height        := 20;
    Align         := alClient;
    Alignment     := taLeftJustify;
    BevelOuter    := bvNone;
    Caption       := 'Oasis';
    TabOrder      := 2;
  End;

  Except On E : Exception Do RaiseError(UnitName,ProcName,E); End;
End;

Destructor TfrmLogin.Destroy;
Begin
  ProcName  := 'TfrmLogin.Destroy'; Try
  pnlServerName    .Free;
  pnlServerSpacer  .Free;
  pnlServerLabel   .Free;
  pnlServerBase    .Free;
  pnlServerBase0   .Free;
  edtPassword      .Free;
  pnlPwdEdit       .Free;
  pnlPwdSpacer     .Free;
  pnlPwdLabel      .Free;
  pnlPwdBase       .Free;
  edtLogin         .Free;
  pnlIDEdit        .Free;
  pnlIDSpacer      .Free;
  pnlIDLabel       .Free;
  pnlIDBase        .Free;
  pnlLoginEditBase .Free;
  pnlLoginEditBase0.Free;
  btnCancel        .Free;
  btnOK            .Free;
  pnlButtonsBase   .Free;
  pnlLoginBase     .Free;
  inherited Destroy;
  Except On E : Exception Do RaiseError(UnitName,ProcName,E); End;
End;

{!~DlgLogin_ads

}
Function DlgLogin_ads(out UserName,Password: String)
 
 
: Boolean;
Var
  Dialog    : TForm;
  Form      : TfrmLogin;
Begin
  Result    := False;
  Dialog    := nil;
  ProcName  := 'DlgLogin_ads'; Try
  Try
    Dialog  := TForm.Create(nil);
    Form       := TfrmLogin.Create(Dialog);
    Form.Parent:= Dialog;
    Form.Align := alClient;
    With Dialog Do
    Begin
      Left          := 509;
      Top           := 252;
      BorderStyle   := bsDialog;
      Caption       := 'Server Login';
      ClientHeight  := 170;
      ClientWidth   := 254;
      Color         := clBtnFace;
      Font.Color    := clWindowText;
      Font.Height   := -11;
      Font.Name     := 'MS Sans Serif';
      Font.Style    := [];
      FormStyle     := fsStayOnTop;
      OldCreateOrder:= False;
      Position      := poScreenCenter;
      OnActivate    := Form.FormActivate;
      PixelsPerInch := 96;
    End;

    Dialog.ShowModal;
    If Dialog.ModalResult = mrOK Then
    Begin
      //Do Something here
      Result := True;
      UserName := Form.UserName;
      Password := Form.Password;
    End;
  Finally
    Dialog.Free;
  End;
  Except On E : Exception Do RaiseError(UnitName,ProcName,E); End;
End;

Initialization
  UnitName := 'ads_DlgLogin';
  ProcName := 'Unknown';
End.
Öğrenmek ve öğretmek, akıntıya karşı yüzmek gibidir ilerleyemediğiniz taktirde gerilersiniz.
Cevapla