program snowshow;

uses Windows, Messages, ShellAPI;

const
  AppName = 'SnowShow v2.01 (c) a.lee 1998';
  id_about = 102;
  id_exit = 103;
  wm_nid = wm_user + 203;
  AboutShow : boolean = false;

var
  NID : TNotifyIconData;

function HookKeyboard(Hook : Boolean) : Boolean; stdcall; external 'SSDLL.DLL' index 1;

{$R ssres.Res}

function About(Dialog: HWnd; Message : Cardinal; WParam : integer; LParam : integer): Bool; export; stdcall;
var
  DlgRect : TRect;
begin
  About := True;
  case Message of
    wm_InitDialog : begin
                      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_Ok) or (LoWord(WParam) = id_Cancel)
                 then
                   begin
                     EndDialog(Dialog, LoWord(WParam));
                     Exit;
                   end;
  end;
  About := False;
end;

function WindowProc(Wnd: HWnd; Message, WParam: Word; LParam: Longint): Longint; export; stdcall;
var
  Menu, Menu1 : HMenu;
  PopUp : TPoint;
begin
  WindowProc := 0;
  case Message of
    wm_Destroy : begin
                   PostQuitMessage(0);
                   Exit;
                 end;
    wm_command : case LOWORD(wParam) of
                   id_about : begin
                                if not AboutShow
                                then
                                  begin
                                    AboutShow := true;
                                    DialogBox(HInstance, 'ABOUTBOX', Wnd, @About);
                                    AboutShow := false;
                                  end;
                                exit;
                              end;
                   id_exit : begin
                               PostQuitMessage(0);
                               Exit;
                             end;
                 end;
    wm_nid : case lParam of
               wm_rbuttonup : begin
                                Menu := LoadMenu(HInstance, 'MENU');
                                Menu1 := GetSubMenu(Menu, 0);
                                GetCursorPos(PopUp);
                                TrackPopupMenu(Menu1, TPM_RIGHTALIGN or TPM_RIGHTBUTTON, PopUp.X, PopUp.Y, 0, Wnd, nil);
                                DestroyMenu(Menu1);
                                DestroyMenu(Menu);
                                exit;
                              end;
             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);
  with NID do
    begin
      uFlags := nif_icon or nif_tip or nif_message;
      cbSize := SizeOf(NID);
      Wnd := Window;
      szTip := AppName;
      hIcon := LoadIcon(HInstance, 'MAINICON');
      uCallBackMessage := wm_nid;
    end;
  Shell_NotifyIcon(nim_add, @NID);
  if not HookKeyboard(true)
  then
    MessageBox(0, 'Error in SSDLL.DLL', AppName, mb_iconstop or mb_ok);
  while GetMessage(Message, 0, 0, 0) do
    begin
      TranslateMessage(Message);
      DispatchMessage(Message);
    end;
  HookKeyboard(false);
  Shell_NotifyIcon(nim_delete, @NID);
end;

begin
  WinMainApi;
end.
