(************************************************************************
*    This File is Part of the AHM Triton Tools                          *
*    for Borland Delphi and C++ Builder.                                *
*************************************************************************
* Copyright © 1995-1999 Alexander H. Mehlhorn (AHM) and Tritontools.com *
* All Rights reserved.                                                  *
*                                                                       *
*    For more Information contact us at                                 *
*    E-Mail : support@tritontools.com                                   *
*    URL : http://www.tritontools.com                                   *
*                                                                       *
*    AHM is copyright by Alexander H. Mehlhorn                          *
*    All Parts of this Collection were developed,                       *
*    designed and implemented by AHM                                    *
*                                                                       *
*    Under no Circumstances may you distribute this file                *
*    in this or any other format. This File is supplied                 *
*    as Evaluation of Source only. No Delphi Components may be developed*
*    using extracts or subsets of this code without expressed permission*
*    by AHM.                                                            *
*                                                                       *
*************************************************************************)
unit AHMTSerialNumber;

interface
{$I AHM.INC}
uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, Buttons, ExtCtrls,AHMDialogClass;

type
  TAHMSerialNumber = class(TAHMDialogComponent)
  private
    fprefix,fsuffix,FCalcSerialCode,FCalcOwnerName : String;
    FUniqueFiveDigits : Integer;
    FOnWrongCode : TNotifyEvent;
    FOnRightCode : TNotifyEvent;
    FFormCaption:String;
    FOnCancel : TNotifyEvent;
    FSerialCaption,FNameCaption : String;
    fok,fcancel : TAHMButtonDetails;
    FFont : TFont;
    fdoclose: Boolean;
    procedure SetcoName(value : String);
    procedure SetInt(value : Integer);
    procedure Setpre(value : String);
    procedure SetOk(value : TAHMButtonDetails);
    procedure SetFont(Value : TFont);
    procedure SetCancel(value : TAHMButtonDetails);
    procedure Setsuf(value : String);
    procedure SerialCloseQuery(Sender: TObject; var CanClose: Boolean);
  protected
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
    Function Execute:BOOLEAN; override;
  published
    Property Prefix : String read fprefix write Setpre stored true;
    Property Suffix : String read fsuffix write Setsuf stored true;
    property UniqueFiveDigits : Integer read FUniqueFiveDigits write Setint stored true;
    Property CalcOwnerName : String read FCalcOwnerName write Setconame stored true;
    Property CalcSerialCode : String read FCalcSerialCode write FCalcSerialCode;
    Property FormCaption : String read FFormCaption write FFormCaption;
    Property OnWrongCode : TNotifyEvent read FOnWrongCode write FOnWrongCode;
    Property OnRightCode : TNotifyEvent read FOnRightCode write FOnRightCode;
    Property CloseInputOnWrongCode : Boolean read fdoclose write fdoclose;
    Property ButtonOk : TAHMButtonDetails read fok write Setok;
    Property ButtonCancel : TAHMButtonDetails read fcancel write SetCancel;
    Property Font : TFont read FFont write SetFont;
    Property NameCaption : String read FNameCaption write FNameCaption;
    Property SerialCaption : String read FSerialCaption write FSerialCaption;
    Property OnCancel : TNotifyevent read FOnCancel write FOnCancel;
  end;


implementation
uses AHMDialogConst;

type
  TAHMSerialForm = class(TForm)
  private
    Panel2: TPanel;
    Panel1: TPanel;
    Edit1: TEdit;
    Edit2: TEdit;
    BitBtn1: TBitBtn;
    BitBtn2: TBitBtn;
    procedure Panel1MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure Panel1MouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
    procedure Panel1MouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure FormCreate(Sender: TObject);
    procedure BitBtn2Click(Sender: TObject);
    procedure BitBtn1Click(Sender: TObject);
    procedure Edit1Change(Sender: TObject);
    procedure FormShow(Sender: TObject);
  private
    OldX,
    OldY,
    OldLeft,
    OldTop   : Integer;
    ScreenDC : HDC;
    MoveRect : TRect;
    Moving   : Boolean;
  public
    sapre,sasuf : String;
    saint : Integer;
    adoclose,SerialEntered : boolean;
    FSerialComp : TAHMSerialNumber;
    procedure Setupform;

  end;

var
  AHMSerialForm: TAHMSerialForm;

procedure TAHMSerialForm.SetupForm;
begin
  Left := 100;
  Top := 100;
  BorderIcons := [];
  BorderStyle := bsToolWindow;
  ClientHeight := 140;
  ClientWidth := 292;
  Color := clBtnFace;
  FormStyle := fsStayOnTop;
  Position := poScreenCenter;
  OnCreate := FormCreate;
  OnShow := FormShow;
  OnCloseQuery:=FSerialComp.SerialCloseQuery;
  Caption:=FSerialComp.FFormCaption;
  Panel2:=TPanel.Create(Self);
  with Panel2 do
  begin
    Left := 0;
    Top := 0;
    Width := 292;
    Height := 140;
    Align := alClient;
    BevelWidth := 2;
    BorderWidth := 1;
    TabOrder := 0;
    Panel1:= TPanel.Create(Panel2);
    with Panel1 do
    Begin
      Parent:=Self;
      Left := 3;
      Top := 3;
      Width := 286;
      Height := 134;
      Align := alClient;
      BevelInner := bvRaised;
      BevelOuter := bvLowered;
      TabOrder := 0;
      OnMouseDown := Panel1MouseDown;
      OnMouseMove := Panel1MouseMove;
      OnMouseUp := Panel1MouseUp;
      with TLabel.Create(self) do
      begin
        Parent:=Panel1;
        Left := 16;
        Top := 8;
        Width := 111;
        Height := 13;
        Caption :=FSerialComp.FNameCaption;
      end;
      with TLabel.Create(self) do
      begin
        Parent:=Panel1;
        Left := 16;
        Top := 56;
        Width := 151;
        Height := 13;
        Caption:=FSerialComp.FSerialCaption;
      end;
      Edit1:= TEdit.Create(self);
      with Edit1 do
      Begin
        Parent:=Panel1;
        Left := 16;
        Top := 24;
        Width := 257;
        Height := 24;
        MaxLength := 50;
        TabOrder := 0;
        OnChange := Edit1Change;
      end;
      Edit2:= TEdit.Create(Self);
      with Edit2 do
      begin
        Parent:=Panel1;
        Left := 16;
        Top := 72;
        Width := 257;
        Height := 24;
        Color := clAqua;
        TabOrder := 1;
      end;
      BitBtn1:= TBitBtn.Create(Self);
      with BitBtn1 do
      begin
        Parent:=Panel1;
        Left := 16;
        Top := 96;
        Width := 75;
        Height := 25;
        Default := True;
        TabOrder := 2;
        Caption:=FSerialComp.FOk.Caption;
        Glyph:=FSerialComp.FOk.Glyph;
        NumGlyphs:=2;
        OnClick := BitBtn1Click;
      end;
      BitBtn2:= TBitBtn.Create(Self);
      with Bitbtn2 do
      Begin
        Parent:=Panel1;
        Left := 96;
        Top := 96;
        Width := 75;
        Height := 25;
        Caption:=FSerialComp.FCancel.Caption;
        Glyph.Assign(FSerialComp.FCancel.Glyph);
        NumGlyphs:=2;
        TabOrder := 3;
        OnClick := BitBtn2Click;
      end;
    end;
  end;
end;

Procedure TAHMSerialNumber.SetcoName(value : String);
var st : string;
    a,fin : integer;
begin
  fin:=0;
  FCalcOwnerName:=value;
  if length(FCalcOwnerName)>0 then
  for a:=0 to length(FCalcOwnerName) do
   fin:=fin+ord(FCalcOwnerName[a])+FUniqueFiveDigits;
  st:=inttostr(fin);
  st:=concat(fprefix,st,fsuffix);
  FCalcSerialCode:=st;
end;

procedure TAHMSerialNumber.Setpre(value : String);
begin
  fprefix:=value;
  Setconame(FCalcOwnerName);
end;

procedure TAHMSerialNumber.SetOk(Value : TAHMButtonDetails);
begin
  fok:=value;
end;

procedure TAHMSerialNumber.SetFont(Value : TFont);
begin
  FFont.assign(value);
end;

procedure TAHMSerialNumber.SetCancel(Value : TAHMButtonDetails);
begin
  fcancel:=value;
end;

procedure TAHMSerialNumber.Setsuf(value : String);
begin
  fsuffix:=value;
  Setconame(FCalcOwnerName);
end;

procedure TAHMSerialNumber.Setint(value : Integer);
begin
  if (value<=99999) and (value>=10000) then
  begin
    FUniqueFiveDigits:=value;
    Setconame(FCalcOwnerName);
  end;
end;

constructor TAHMSerialNumber.Create(AOwner: TComponent);
begin
  fsuffix:=ahmrcdlgsuffix;
  fprefix:=ahmrcdlgprefix;
  inherited Create(AOwner);
  FFormCaption:= ahmrcdlgenterdetails;
  FSerialCaption:=ahmrcdlgenterserial;
  FNameCaption:=ahmrcdlgentername;
  fok:=TAHMButtonDetails.create(self);
  fcancel:=TAHMButtonDetails.create(self);
  fok.Caption:=ahmrcdlgok;
  FFont := TFont.create;
  fcancel.Caption:=ahmrcdlgcancel;
  FUniqueFiveDigits:=11111;
end;

destructor TAHMSerialNumber.Destroy;
begin
  fok.free;
  fcancel.free;
  FFont.free;
  inherited Destroy;
end;


function TAHMSerialNumber.execute:BOOLEAN;
Begin
   inherited Execute;
   AHMSerialForm:=TAHMSerialForm.createNew(Application);
   with AHMSerialForm do
   try
     FSerialComp:=Self;
     SetupForm;
     Panel1.Font:=FFont;
     sapre:=fprefix;
     sasuf:=fsuffix;
     saint:=FUniqueFiveDigits;
     adoclose:=fdoclose;
     showmodal;
     execute:=SerialEntered;
   finally
     free;
   end;
end;


procedure TAHMSerialForm.Panel1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  if Button = mbLeft then begin
    SetCapture(Panel1.Handle);
    ScreenDC := GetDC(0);
    OldX := X;
    OldY := Y;
    OldLeft := X;
    OldTop := Y;
    MoveRect := BoundsRect;
    DrawFocusRect(ScreenDC,MoveRect);
    Moving := True;
  end;
end;

procedure TAHMSerialForm.Panel1MouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
begin
  if Moving then begin
    DrawFocusRect(ScreenDC,MoveRect);
    OldX := X;
    OldY := Y;
    MoveRect := Rect(Left+OldX-OldLeft,Top+OldY-OldTop,
                     Left+Width+OldX-OldLeft,Top+Height+OldY-OldTop);
    DrawFocusRect(ScreenDC,MoveRect);
  end;
end;

procedure TAHMSerialForm.Panel1MouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
   if Button = mbLeft then begin
    ReleaseCapture;
    DrawFocusRect(ScreenDC,MoveRect);
    Left := Left+X-OldLeft;
    Top := Top+Y-OldTop;
    ReleaseDC(0,ScreenDC);
    Moving := False;
  end;

end;

procedure TAHMSerialForm.FormCreate(Sender: TObject);
begin
  SetWindowLong(Handle,GWL_STYLE, GetWindowLong(Handle,GWL_STYLE) AND NOT
WS_CAPTION);
   Clientheight:=Height;
end;

procedure TAHMSerialForm.BitBtn2Click(Sender: TObject);
begin
  SerialEntered:=false;
  if FSerialComp.FCancel.ConfirmOnPress then
  begin
    if MessageDlg(FSerialComp.FCancel.ConfirmMessage,mtconfirmation,[mbyes,mbno],0)=mryes then
    begin
      if assigned(FSerialComp.FOnCancel) then FSerialComp.FOnCancel(self);
      close;
    end;
  end else
  begin
    if assigned(FSerialComp.FOnCancel) then FSerialComp.FOnCancel(self);
    close;
  end;
end;

procedure TAHMSerialForm.BitBtn1Click(Sender: TObject);
var st : string;
    a,fin : integer;
begin
  if edit1.text<>'' then
  if FSerialComp.FCancel.ConfirmOnPress then
  begin
  if MessageDlg(FSerialComp.FCancel.ConfirmMessage,mtconfirmation,[mbyes,mbno],0)=mryes then
  begin
    fin:=0;
    if length(edit1.text)>0 then
    for a:=0 to length(edit1.text) do
     fin:=fin+ord(edit1.text[a])+saint;
    st:=inttostr(fin);
    st:=concat(sapre,st,sasuf);
    if st=edit2.text then
    begin
      if assigned(FSerialComp.OnRightCode) then FSerialComp.OnRightCode(self);
      SerialEntered:=true;
      close;
    end else
    begin
      if assigned(FSerialComp.OnWrongCode) then FSerialComp.OnWrongCode(self);
      if adoclose then close;
   end;
  end;
  end else
  begin
    fin:=0;
    if length(edit1.text)>0 then
    for a:=0 to length(edit1.text) do
     fin:=fin+ord(edit1.text[a])+saint;
    st:=inttostr(fin);
    st:=concat(sapre,st,sasuf);
    if st=edit2.text then
    begin
      if assigned(FSerialComp.OnRightCode) then FSerialComp.OnRightCode(self);
      SerialEntered:=true;
      close;
    end else
    begin
      if assigned(FSerialComp.OnWrongCode) then FSerialComp.OnWrongCode(self);
      if adoclose then close;
   end;
  end;
end;

procedure TAHMSerialForm.Edit1Change(Sender: TObject);
begin
  if edit1.text<>'' then bitbtn1.enabled:=true
  else bitbtn1.enabled:=false;
end;

procedure TAHMSerialForm.FormShow(Sender: TObject);
begin
  if edit1.text<>'' then bitbtn1.enabled:=true
  else bitbtn1.enabled:=false;
end;

procedure TAHMSerialNumber.SerialCloseQuery(Sender: TObject;
  var CanClose: Boolean);
begin
  if assigned(FOnCancel) and not(AHMSerialForm.SerialEntered) then FOnCancel(Self);
end;

end.
