unit Main;

interface

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

CONST

  MYWM_NOTIFYICON = WM_USER + 100;
  MyIconID = 100;

type
  TForm1 = class(TForm)
    Image1: TImage;
    Edit1: TEdit;
    Button1: TButton;
    Button2: TButton;
    PopupMenu1: TPopupMenu;
    CloseItem: TMenuItem;
    ShowItem: TMenuItem;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure Button2Click(Sender: TObject);
    procedure CloseItemClick(Sender: TObject);
    procedure ShowItemClick(Sender: TObject);
    procedure PopupMenu1Popup(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    PROCEDURE MYWMNOTIFYICON(VAR Message: TMessage); MESSAGE MYWM_NOTIFYICON;
  end;

var
  Form1: TForm1;

implementation

USES ShellApi;

{$R *.DFM}

FUNCTION MyTaskBarAddIcon(wnd: HWND; uID: integer; Icon: HICON; S: STRING): Boolean;
VAR tnid: TNOTIFYICONDATA;
BEGIN
  tnid.cbSize := SizeOf(TNOTIFYICONDATA);
  tnid.Wnd := wnd;
  tnid.uID := uID;
  tnid.uFlags := NIF_MESSAGE OR NIF_ICON OR NIF_TIP;
  tnid.uCallbackMessage := MYWM_NOTIFYICON;
  tnid.hIcon := Icon;
  Lstrcpyn(tnid.szTip, PChar(S), SizeOf(tnid.szTip));

  Result := Shell_NotifyIcon(NIM_ADD, @tnid);
END;

FUNCTION MyTaskBarDeleteIcon(wnd: HWND; uID: integer): Boolean;
VAR tnid: TNOTIFYICONDATA;
BEGIN
  tnid.cbSize := SizeOf(TNOTIFYICONDATA);
  tnid.Wnd := wnd;
  tnid.uID := uID;

  Result := Shell_NotifyIcon(NIM_DELETE, @tnid);
END;

FUNCTION MyTaskBarModifyIcon(wnd: HWND; uID: integer; Icon: HICON; S: STRING): Boolean;
VAR tnid: TNOTIFYICONDATA;
BEGIN
  tnid.cbSize := SizeOf(TNOTIFYICONDATA);
  tnid.Wnd := wnd;
  tnid.uID := uID;
  tnid.uFlags := NIF_MESSAGE OR NIF_ICON OR NIF_TIP;
  tnid.uCallbackMessage := MYWM_NOTIFYICON;
  tnid.hIcon := Icon;
  Lstrcpyn(tnid.szTip, PChar(S), SizeOf(tnid.szTip));

  Result := Shell_NotifyIcon(NIM_MODIFY, @tnid);
END;

procedure TForm1.FormCreate(Sender: TObject);
begin
  MyTaskBarAddIcon(Handle, MyIconID, Image1.Picture.Icon.Handle, 'Example');
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  MyTaskBarDeleteIcon(Handle, MyIconID);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  MyTaskBarModifyIcon(Handle, MyIconID, Image1.Picture.Icon.Handle, Edit1.Text);
end;

PROCEDURE TForm1.MYWMNOTIFYICON(VAR Message: TMessage);
VAR Cur: TPoint;
BEGIN
  IF Message.WParam = MyIconID THEN
    CASE Message.LParam OF
      WM_RBUTTONDOWN:
      BEGIN
        GetCursorPos(Cur);
        PopupMenu1.Popup(Cur.X, Cur.Y);
      END;
      WM_LBUTTONUP: MessageDlg('Отжата левая клавиша', mtInformation, [mbOk], 0);
      WM_LBUTTONDBLCLK: MessageDlg('Нажата двойная левая клавиша', mtInformation, [mbOk], 0);
    END;
END;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Action := caHide;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  Visible := False;
end;

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

procedure TForm1.ShowItemClick(Sender: TObject);
begin
  Visible := NOT Visible;
end;

procedure TForm1.PopupMenu1Popup(Sender: TObject);
begin
  IF Visible
  THEN ShowItem.Caption := 'Спрятать'
  ELSE ShowItem.Caption := 'Показать'
end;

end.
