program ExitWin;

uses Windows, Messages, CommCtrl;

const
  AppName = 'ExitWin v2.01 (c) a.lee 1998';

{$R ewres.Res}

var
  sec : byte;
  hwndPB : HWnd;

function About(Dialog: HWnd; Message : Cardinal; WParam : integer; LParam : integer): Bool; export; stdcall;
var
  DlgRect, rcClient : TRect;
begin
  About := True;
  case Message of
    wm_InitDialog : begin
                      InitCommonControls;
                      GetClientRect(Dialog, rcClient);
                      hwndPB := CreateWindowEx(0, PROGRESS_CLASS, #0,
                                               WS_CHILD or WS_VISIBLE, rcClient.left + 10,
                                               rcClient.top + 40,
                                               rcClient.right - 20, 20,
                                               Dialog, 0, HInstance, nil);
                      SendMessage(hwndPB, PBM_SETRANGE, 0, MAKELPARAM(0, 10));
                      SendMessage(hwndPB, PBM_SETSTEP, 1, 0);

                      SetDlgItemText(Dialog, 2, 'Отмена');
                      SetDlgItemText(Dialog, 500, 'Через 10 секунд Windows завершит работу!');
                      GetWindowRect(Dialog, DlgRect);
                      SetWindowPos(Dialog, HWND_TOP,
                                   (GetSystemMetrics(SM_CXSCREEN) - DlgRect.Right + DlgRect.Left) div 2,
                                   (GetSystemMetrics(SM_CYSCREEN) - DlgRect.Bottom + DlgRect.Top) div 2,
                                   DlgRect.Right - DlgRect.Left,
                                   DlgRect.Bottom - DlgRect.Top,
                                   SWP_SHOWWINDOW);
                      Exit;
                    end;
    wm_Command : if LoWord(WParam) = id_Cancel
                 then
                   begin
                     EndDialog(Dialog, LoWord(WParam));
                     PostQuitMessage(0);
                     Exit;
                   end;
  end;
  About := False;
end;

function WindowProc(Wnd: HWnd; Message, WParam: Word; LParam: Longint): Longint; export; stdcall;
begin
  WindowProc := 0;
  case Message of
    wm_Destroy : begin
                   PostQuitMessage(0);
                   Exit;
                 end;
    wm_timer : begin
                 if Sec < 10
                 then
                   begin
                     inc(sec);
                     SendMessage(hwndPB, PBM_STEPIT, 0, 0);
                     if Sec >= 10
                     then
                       begin
                         KillTimer(Wnd, 100);
                         if ParamCount = 1
                         then
                           if (ParamStr(1) = '/R') or (ParamStr(1) = '/r') 
                           then
                             ExitWindowsEx(EWX_REBOOT, 0)
                           else
                             ExitWindowsEx(EWX_SHUTDOWN, 0)
                         else
                           ExitWindowsEx(EWX_SHUTDOWN, 0);
                       end;
                   end;
                 exit;
               end;
  end;
  WindowProc := DefWindowProc(Wnd, Message, WParam, LParam);
end;

procedure WinMainApi; export; stdcall;
const
  WindowClass : TWndClass = (style : 0;
                             lpfnWndProc : @WindowProc;
                             cbClsExtra : 0;
                             cbWndExtra : 0;
                             hInstance : 0;
                             hIcon : 0;
                             hCursor : 0;
                             hbrBackground : 0;
                             lpszMenuName : #0;
                             lpszClassName : AppName);
var
  Message: TMsg;
  Window : HWnd;
begin
  WindowClass.hInstance := HInstance;
  if Windows.RegisterClass(WindowClass) = 0
  then
    Halt(255);
  Window := CreateWindow(AppName, AppName, 0, 0, 0, 0, 0, 0, 0, HInstance, nil);
  ShowWindow(Window, SW_HIDE);
  Sec := 0;
  SetTimer(Window, 100, 1000, nil);
  DialogBox(HInstance, 'ABOUTBOX', Window, @About);
  while GetMessage(Message, 0, 0, 0) do
    begin
      TranslateMessage(Message);
      DispatchMessage(Message);
    end;
  KillTimer(Window, 100);
  UnRegisterClass(AppName, HInstance);
end;

begin
  WinMainApi;
end.
