unit USysTray;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Menus;

const MyTrayIcon = WM_USER + 1;
type
  TForm1 = class(TForm)
    BCreate: TButton;
    BDelete: TButton;
    PopupMenu1: TPopupMenu;
    MRestore: TMenuItem;
    MClose: TMenuItem;
    MDelete: TMenuItem;
    BHide: TButton;
    procedure BDeleteClick(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure MCloseClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure BCreateClick(Sender: TObject);
    procedure MRestoreClick(Sender: TObject);
    procedure BHideClick(Sender: TObject);
    procedure MDeleteClick(Sender: TObject);
  private
    { Private declarations }
   procedure MTIcon(var a: TMessage); message MyTrayIcon;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}
uses ShellAPI;

var NID: TNotifyIconData;

procedure TForm1.FormCreate(Sender: TObject);
begin
 Application.ShowMainForm := false;
 with NID do
  begin
   cbSize := SizeOf(TNotifyIconData);
   Wnd := Handle;
   uId := 1;
   uFlags := NIF_ICON or NIF_MESSAGE or NIF_TIP;
   uCallbackMessage := MyTrayIcon;
   hIcon := Application.Icon.Handle;
   szTip := 'Мое приложение';
  end;
  Shell_NotifyIcon(NIM_ADD, @NID);
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  Shell_NotifyIcon(NIM_DELETE, @NID);
end;

procedure TForm1.MTIcon(var a: TMessage);
var P: TPoint;
begin
 case a.lParam of
  WM_LBUTTONDBLCLK:
   begin
    Show;
    SetForegroundWindow(Handle);
   end;
  WM_RBUTTONDOWN:
   begin
    GetCursorPos(P);
    PopupMenu1.Popup(P.X, P.Y);
   end;
 end;
end;

procedure TForm1.MRestoreClick(Sender: TObject);
begin
 Show;
end;

procedure TForm1.MCloseClick(Sender: TObject);
begin
 Close;
end;

procedure TForm1.MDeleteClick(Sender: TObject);
begin
 Shell_NotifyIcon(NIM_DELETE, @NID);
 BCreate.Enabled := true;
 BDelete.Enabled := false;
 BHide.Enabled := false;
 Show;
end;

procedure TForm1.BCreateClick(Sender: TObject);
begin
 Shell_NotifyIcon(NIM_ADD, @NID);
 BCreate.Enabled := false;
 BDelete.Enabled := true;
 BHide.Enabled := true;
end;

procedure TForm1.BDeleteClick(Sender: TObject);
begin
 Shell_NotifyIcon(NIM_DELETE, @NID);
 BCreate.Enabled := true;
 BDelete.Enabled := false;
 BHide.Enabled := false;
end;

procedure TForm1.BHideClick(Sender: TObject);
begin
 Visible := false;
// Application.ShowMainForm := false;
end;

end.
