Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

How To

How To Implement a Systray Icon (Revision 3) by Glenn9999
Posted: 7 Aug 09 (Edited 21 Jun 11)

This is a component which can be placed on a form that enables the use of a system tray icon for your program.


unit systray_comp;
  { system tray icon component written by Glenn9999 of tek-tips.com }

  {$R SYSTRAY_comp.DCR}
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  shellapi, registry;

  // constants for systray icon control
  NIF_ICON = 2;
  NIF_TIP = 4;
  NIF_INFO = $10;

  // constants for systray balloon tip control
  NIIF_NONE = $00;    // no bt icon
  NIIF_INFO = $01;    // info icon
  NIIF_WARNING = $02; // warning icon
  NIIF_ERROR = $03;   // error icon
  NIIF_USER = $04;    // same icon as the system tray
  NIIF_NOSOUND = $10; // no sound to be played

  // events generated off of systray icon balloon tip.
  {NINF_KEY = $1;


  // the new systray icon structures.  Old ones are defined in shellapi
  // as TNotifyIconData.

    case Integer of
      0: (uTimeout: UINT);
      1: (uVersion: UINT);

  TNotifyIconData2 = record
    cbSize: DWORD;
    Wnd: HWND;
    uID: UINT;
    uFlags: UINT;
    uCallbackMessage: UINT;
    hIcon: HICON;
    szTip: array [0..127] of Char;
    dwState: DWORD;
    dwStateMask: DWORD;
    szInfo: array [0..255] of Char;
    szInfoTitle: array [0..63] of Char;
    dwInfoFlags: DWORD;

  TBTipType = (btNone, btInfo, btWarning, btError, btUser);

  TSysTrayIcon = class(TComponent)
  private   { Private declarations }
    FTrayID: integer;
    FAppTitle: string;
    FIcon: TIcon;
    FTipInfo: string;
    FTipTitle: string;
    FTipTimeOut: integer;
    FTipType: TBTipType;
    FVisible: Boolean;
    FUseSound: Boolean;
    FTransparentColor: TColor;
    FHandle: HWnd;
    FTrayIconData: TNotifyIconData2;
    // notification events
    FOnMouseMove: TMouseMoveEvent;
    FOnLClick: TNotifyEvent;
    FOnRClick: TNotifyEvent;
    FOnMouseUp: TMouseEvent;
    FOnMouseDown: TMouseEvent;
    FOnLDblClick: TNotifyEvent;
    FOnRDblClick: TNotifyEvent;
    FOnBalloonShow: TNotifyEvent;
    FOnBalloonHide: TNotifyEvent;
    FOnBalloonTimeOut: TNotifyEvent;
    FOnBalloonClick: TNotifyEvent;
    procedure SetIcon(Value: TIcon);
    procedure SetVisible(Value: Boolean);
  protected { Protected declarations }
    procedure Loaded; override;
    procedure TrayMessage(var Message: TMessage); message WM_ICONTRAY;
  public    { Public declarations }
    Constructor Create(AOwner: TComponent); override;
    Destructor Destroy; override;
    procedure addtray;
    procedure modifytray;
    procedure deletetray;
    procedure showballoontip;
     function BalloonTipsAllowed: boolean;
    procedure BitMapToIcon(myBitmap: TBitmap);
  published { Published declarations }
    property AppTitle: string read FAppTitle write FAppTitle;
    property Icon: TIcon read FIcon write SetIcon;
    property TrayID: integer read FTrayID write FTrayID default 15;
    property TipInfo: string read FTipInfo write FTipInfo;
    property TipTitle: string read FTipTitle write FTipTitle;
    property TipTimeOut: integer read FTipTimeOut write FTipTimeOut default 300;
    property TipType: TBTipType read FTipType write FTipType;
    property UseSound: Boolean read FUseSound write FUseSound;
    property TransparentColor: TColor read FTransparentColor write FTransparentColor;
    property Visible: Boolean read FVisible write Setvisible;
    property OnLClick: TNotifyEvent read FOnLClick write FOnLClick;
    property OnRClick: TNotifyEvent read FOnRClick write FOnRClick;
    property OnMouseMove: TMouseMoveEvent read FOnMouseMove write FOnMouseMove;
    property OnMouseUp: TMouseEvent read FOnMouseUp write FOnMouseUp;
    property OnMouseDown: TMouseEvent read FOnMouseDown write FOnMouseDown;
    property OnLDblClick: TNotifyEvent read FOnLDblClick write FOnLDblClick;
    property OnRDblClick: TNotifyEvent read FOnRDblClick write FOnRDblClick;
    property OnBalloonShow: TNotifyEvent read FOnBalloonShow write FOnBalloonShow;
    property OnBalloonHide: TNotifyEvent read FOnBalloonHide write FOnBalloonHide;
    property OnBalloonTimeOut: TNotifyEvent read FOnBalloonTimeOut write FOnBalloonTimeout;
    property OnBalloonClick: TNotifyEvent read FOnBalloonClick write FOnBalloonClick;

procedure Register;
procedure hidetaskbar(winhandle: HWnd);
procedure showtaskbar(winhandle: HWnd);


Constructor TSysTrayIcon.Create(AOwner: TComponent);
    FIcon := TIcon.Create;
    FHandle := AllocateHWnd(TrayMessage);
    Inherited Create(AOwner);

Destructor TSysTrayIcon.Destroy;

 function TSysTrayIcon.BalloonTipsAllowed: boolean;
   { determines if balloon tips are allowed }
      HKEY_CURRENT_USER = $80000001;
      keyname = '\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced';
      strname = 'EnableBalloonTips';
      workcheck: TRegistry;
      isinreg: boolean;
      balloontips: integer;
      isinreg := true;
      workcheck := TRegistry.Create;
      workcheck.rootkey := HKEY_CURRENT_USER;
      if workcheck.openkey(keyname, false) then
            balloontips := workcheck.ReadInteger(strname);
            if balloontips = 0 then isinreg := false;
            isinreg := true;
      Result := isinreg;

procedure TSysTrayIcon.Loaded;
  { initializations of the control }
    warning_message = 'Balloon tips are not configured for this system '
       + 'and will not work in the TSysTrayIcon component.' + #13#10
       + 'Please contact your administrator. For details see Microsoft KB #307729';
    inherited loaded;
    // enforce defaults
    if (csDesigning in ComponentState) then
        if BalloonTipsAllowed = false then
        if FIcon.Empty then FIcon.Assign(Application.Icon);
        if TipInfo = '' then
          TipInfo := 'You need to assign your own balloon tip message here.';

procedure TSysTrayIcon.SetIcon(Value: TIcon);

procedure TSysTrayIcon.SetVisible(Value: Boolean);
    FVisible := Value;
    if (csDesigning in ComponentState) then exit;
    FVisible := not FVisible;
    If Value then

procedure TSysTrayIcon.AddTray;
    if FVisible then exit;

    with FTrayIconData do
        cbSize := SizeOf(FTrayIconData);
        Wnd := FHandle;
        uID := Ftrayid;
        uFlags := NIF_MESSAGE + NIF_ICON + NIF_TIP;
        uCallbackMessage := WM_ICONTRAY;
        hIcon := FIcon.Handle;
        StrPCopy(szTip, Fapptitle);
    Shell_NotifyIcon(NIM_ADD, @FTrayIconData);
   //need to set version with this tray icon to fully support tool tips.
    Integer(FTrayIconData.DUMMYUNIONNAME) := 4;
    Shell_NotifyIcon(NIM_SETVERSION, @FTrayIconData);
    FVisible := true;

procedure TSysTrayIcon.ModifyTray;
    if not FVisible then exit;
    with FTrayIconData do
        cbSize := SizeOf(FTrayIconData);
        Wnd := FHandle;
        uID := Ftrayid;
        uFlags := NIF_MESSAGE + NIF_ICON + NIF_TIP;
        uCallbackMessage := WM_ICONTRAY;
        hIcon := FIcon.Handle;
        StrPCopy(szTip, Fapptitle);
    Shell_NotifyIcon(NIM_MODIFY, @FTrayIconData);

procedure TSysTrayIcon.DeleteTray;
    if not FVisible then exit;
    Shell_NotifyIcon(NIM_DELETE, @FTrayIconData);
    FVisible := False;

procedure TSysTrayIcon.ShowBalloonTip;
    ni_value: integer;
    if not FVisible then exit;

    case FTipType of
      btNone: ni_value := NIIF_NONE;
      btInfo: ni_value := NIIF_INFO;
      btWarning: ni_value := NIIF_WARNING;
      btError: ni_value := NIIF_ERROR;
      btUser: ni_value := NIIF_USER;
      ni_value := 0;
    if FUseSound = false then
      ni_value := ni_value + NIIF_NOSOUND;
    With FTrayIconData do
        cbSize := SizeOf(FTrayIconData);
        uFlags := NIF_INFO;
        strPLCopy(szInfo, FTipInfo, SizeOf(szInfo) - 1);
        DUMMYUNIONNAME.uTimeout := FTipTimeout;
        strPLCopy(szInfoTitle, FTipTitle, SizeOf(szInfoTitle) - 1);
        dwInfoFlags := ni_value;
    Shell_NotifyIcon(NIM_MODIFY, @FTrayIconData);

procedure TSysTrayIcon.BitMapToIcon(myBitmap: TBitmap);
  IconSizeX : integer;
  IconSizeY : integer;
  AndMask : TBitmap;
  oldcol : integer;
  ColorBMP : TBitmap;
  IconInfo : TIconInfo;
  IconSizeX := myBitmap.Width;
  IconSizeY := myBitmap.Height;

  AndMask := TBitmap.Create;
  ColorBMP := TBitmap.Create;
  // AndMask needs to be black everywhere the image is supposed to show.
  // if you want the BMP to show entirely as is, make this BMP to be all black
    AndMask.Width := IconSizeX;
    AndMask.Height := IconSizeY;
    oldcol := SetBkColor(myBitmap.Canvas.Handle, ColorToRGB(fTransparentColor));
    AndMask.Monochrome := true;
    AndMask.Canvas.CopyMode := cmSrcCopy;
    AndMask.Canvas.Draw(0, 0, myBitmap);
    SetBkColor(myBitmap.Canvas.Handle, oldcol);

  // ColorBMP is the full color image.  The transparent color must be replaced
  // with black in this image for the transparency to work.
    ColorBMP.Width := IconSizeX;
    ColorBMP.Height := IconSizeY;
    ColorBMP.PixelFormat := pf24bit;
    ColorBMP.Canvas.Brush.Color := clBlack;
    ColorBMP.Canvas.FillRect(Rect(0, 0, IconSizeX, IconSizeY));
    myBitmap.TransparentColor := fTransparentColor;
    myBitmap.Transparent := true;
    ColorBMP.Canvas.StretchDraw(Rect(0,0,IconSizeX,IconSizeY), myBitmap);

   {Create a icon}
    IconInfo.fIcon := false;
    IconInfo.xHotspot := 0;
    IconInfo.yHotspot := 0;
    IconInfo.hbmMask :=  AndMask.Handle;
    IconInfo.hbmColor := ColorBMP.Handle;
    Icon.Handle := CreateIconIndirect(IconInfo);

procedure TSysTrayIcon.TrayMessage(var Message: TMessage);
  { driver routine, forms messages and dispatches them to the user of the
    component. }
    P: TPoint;
    if Message.Msg = WM_ICONTRAY then
        case Message.lParam of
              if Assigned(FOnLClick) then OnLClick(Self);
              if Assigned(FOnMouseDown) then
                 OnMouseDown(Self, mbLeft, [ssShift], p.x, p.y);
            if Assigned(FOnMouseUp) then
              OnMouseUp(Self, mbLeft, [ssShift], p.x, p.y);
            if Assigned(FOnLDblClick) then OnLDblClick(Self);
              if Assigned(FOnRClick) then OnRClick(Self);
              if Assigned(FOnMouseDown) then
                OnMouseDown(Self, mbRight, [ssShift], p.x, p.y);
            if Assigned(FOnMouseUp) then
              OnMouseDown(Self, mbRight, [ssShift], p.x, p.y);
            if Assigned(FOnRDblClick) then OnRDblClick(Self);
            If Assigned(FOnMouseMove) then
              OnMouseMove(Self, [ssShift], p.x, p.y);
        //  NIN_BALLOONSHOW - done when the b. tip is shown,
            if Assigned(FOnBalloonShow) then OnBalloonShow(Self);
        //  NIN_BALLOONHIDE - done when the b. tip disappears; like if the icon is
        //    deleted,
            if Assigned(FOnBalloonHide) then OnBalloonHide(Self);
        //  NIN_BALLOONTIMEOUT - done when the b.tip timeout occurs; the timeout
        //    doesn't start if the app the systray icon belongs to has focus - this
        //    will also occur upon clicking the [X],
            if Assigned(FOnBalloonTimeout) then OnBalloonTimeout(Self);
        //  NIN_BALLOONUSERCLICK - done if the b.tip itself is clicked.
            if Assigned(FOnBalloonClick) then OnBalloonClick(Self);
      Message.Result := DefWindowProc(Fhandle, Message.Msg,
                                  Message.WParam, Message.LParam);

procedure hidetaskbar(winhandle: HWnd);
  // this all hides the app from the task bar
    GWL: Integer;
    ShowWindow(winhandle, SW_HIDE);
    GWL := getWindowLong(Winhandle, GWL_EXSTYLE) or WS_EX_TOOLWINDOW;
    SetWindowLong(winhandle, GWL_EXSTYLE, GWL);
    ShowWindow(WinHandle, SW_SHOW);

procedure showtaskbar(winhandle: HWnd);
  // this shows the app on the task bar
    GWL: Integer;
    ShowWindow(winhandle, SW_HIDE);
    GWL := not WS_EX_TOOLWINDOW and getWindowLong(Winhandle, GWL_EXSTYLE);
    SetWindowLong(winhandle, GWL_EXSTYLE, GWL);
    ShowWindow(WinHandle, SW_SHOW);

procedure Register;
  RegisterComponents('Samples', [TSysTrayIcon]);


Back to Embarcadero: Delphi FAQ Index
Back to Embarcadero: Delphi Forum

My Archive

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close