unit ABSDlgWait;

interface

{$I ABSVer.inc}

uses
  Windows, Messages, SysUtils, Classes, 
{$IFNDEF D16H}
Graphics, Controls, Forms, Dialogs, StdCtrls, ComCtrls, ExtCtrls
{$ELSE}
vcl.Graphics, vcl.Controls, vcl.Forms, vcl.Dialogs,vcl.StdCtrls, vcl.ComCtrls, vcl.ExtCtrls
{$ENDIF}
;

type
  TfrmWait = class(TForm)
    lMessage: TLabel;
    btnCancel: TButton;
    pb: TProgressBar;
    btnBgMode: TButton;
    ShowTimer: TTimer;
    procedure FormHide(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure btnCancelClick(Sender: TObject);
    procedure btnBgModeClick(Sender: TObject);
    procedure ShowFormOnTimer(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    { Private declarations }
    EWindowList:pointer;
    EActiveWindow:HWND;
    OldOnRestore: TNotifyEvent;
    IsShown: Boolean;
    IsClosed: Boolean;
    function MessageHook(var Msg: TMessage): Boolean;
    procedure Restore(Sender: TObject);
  public
    { Public declarations }
    Terminated: boolean;
    procedure Show(text: string);
    procedure Close;
  end;

implementation

{$R *.DFM}

{ TfrmWait }

procedure TfrmWait.Show(text: string);
begin
  lMessage.Caption:= text;
  Terminated := false;
  ActiveControl:=btnCancel;
  OldOnRestore:=Application.OnRestore;
  IsShown := False;
  IsClosed := False;
  ShowTimer.Enabled := True;
end;

procedure TfrmWait.FormShow(Sender: TObject);
begin
  ShowTimer.Enabled := False;
  EActiveWindow := GetActiveWindow;
  Application.HookMainWindow(MessageHook);
  EWindowList := DisableTaskWindows(0);
  IsShown := True;
end;

procedure TfrmWait.FormHide(Sender: TObject);
begin
  ShowTimer.Enabled := False;
  if (IsShown) then
    begin
      Application.UnhookMainWindow(MessageHook);
      EnableTaskWindows(EWindowList);
      SetActiveWindow(EActiveWindow);
      IsShown := False;
    end;
end;

function TfrmWait.MessageHook(var Msg: TMessage): Boolean;
begin
  Result := False;
end;

procedure TfrmWait.btnCancelClick(Sender: TObject);
begin
  Terminated := true;
end;

procedure TfrmWait.btnBgModeClick(Sender: TObject);
begin
  Application.OnRestore := Restore;
  Hide;
  Application.Minimize;
  Application.ProcessMessages;
end;

procedure TfrmWait.Restore(Sender: TObject);
begin
  Application.OnRestore:=OldOnRestore;
  Show(lMessage.Caption);
  Application.ProcessMessages;
end;

procedure TfrmWait.Close;
begin
  Application.OnRestore:=OldOnRestore;
  Hide;
  Application.Restore;
  IsClosed := True;
  TForm(Self).Close;
end;


procedure TfrmWait.ShowFormOnTimer(Sender: TObject);
begin
  ShowTimer.Enabled := False;
  if ((not IsClosed) and (pb.Position <= 50)) then
    inherited show;
  Application.ProcessMessages;
end;

procedure TfrmWait.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Action := caFree;
end;

initialization

finalization

end.

