Shell_NotifyIcon Delphi Tutorial

'

Manipulating Icons in the System Tray using TNotifyIconData and uCallbackMessage

The taskbar status area is located to the right of the Start button (next to the system clock), and provides you with status or notification indicators about your programs. Icons with ToolTips are typically used as indicators in the taskbar status area. You can also animate the icons to indicate an event (e.g. new mail).

To manipulate an icon in the taskbar status area, use the Windows API function Shell_NotifyIcon in the Shell32.dll file. This function allows you to add, modify, delete, set a ToolTip string, and send a callback message to execute mouse events.


unit Unit1;

interface

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

const
  WM_MYICON    = WM_USER + 1;

type
  TForm1 = class(TForm)
    PopupMenu: TPopupMenu;
    miShowForm: TMenuItem;
    miCloseApplication: TMenuItem;
    procedure miShowFormClick(Sender: TObject);
    procedure miCloseApplicationClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    { Private declarations }
    FIconData: TNotifyIconData;
    procedure WMMYIcon(var Message: TMessage); message WM_MYICON;
    procedure AddIcon;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

{ TForm1 }

procedure TForm1.AddIcon;
begin
    with FIconData do
    begin
      cbSize := SizeOf(FIconData);
      Wnd := Self.Handle;
      uID := $DEDB;
      uFlags := NIF_MESSAGE or NIF_ICON or NIF_TIP;
      hIcon := Forms.Application.Icon.Handle;
      uCallbackMessage := WM_MYICON;
      StrCopy(szTip, PChar(Caption));
    end;
    Shell_NotifyIcon(NIM_Add, @FIconData);
end;

procedure TForm1.WMMYIcon(var Message: TMessage);
var
  pt: TPoint;
begin
  case Message.LParam of
    WM_RBUTTONUP:
    begin
      if not Visible then
      begin
        SetForegroundWindow(Handle);
        GetCursorPos(pt);
        PopupMenu.Popup(pt.x, pt.y);
      end else
        SetForegroundWindow(Handle);
    end;
    WM_LBUTTONDBLCLK:
      if Visible then
        SetForegroundWindow(Handle) else
        Showform1click(nil);
  end;

end;

procedure TForm1.miShowFormClick(Sender: TObject);
begin
  ShowModal;
end;

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

procedure TForm1.FormCreate(Sender: TObject);
begin
  AddIcon;
end;

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

end.

This tutorial was kindly provided by Serge Perevoznyk


Google
Web www.Delphi-Central.com
Delphi Central - Delphi Programming Tutorials, Hints and Tips