Starting an application as an icon in the system tray

This article will show you how to start an applicacion minimized with an icon in the system tray (the area at the right of the task bar)


The system tray is the area at the right of the task bar where "background" applications can place their icons. To place an icon in the tray you have to call the Shell_NotifyIcon API declared in the ShellAPI unit, passing it the constant NIM_ADD (to tell it to add an icon) and the address of a TNotifyIconData record with the icon information that includes the handle of the icon to show, the text to show as tool tip when the mouse is over the icon, the handle of the window that will receive the messages of the icon and the message type the icon will send to this window.


     ..., ShellAPI;


    WM_ICONTRAY = WM_USER + 1; // User-defined message





    NotifyIconData : TNotifyIconData;


  procedure TForm1.FormCreate(Sender: TObject);


    with NotifyIconData do begin

      hIcon := Icon.Handle;

      StrPCopy(szTip, Application.Title);

      Wnd := Handle;

      uCallbackMessage := WM_ICONTRAY;

      uID := 1;

      uFlags := NIF_MESSAGE + NIF_ICON + NIF_TIP;

      cbSize := sizeof(TNotifyIconData);


    Shell_NotifyIcon(NIM_ADD, @NotifyIconData);



Form1 will receive WM_ICONTRAY messages from the icon. To capture and handle these messages we have to declare a new method in our form. For example:


    TForm1 = class(TForm)



      { Private declarations }


      procedure Icontray(var Msg: TMessage); message WM_ICONTRAY;


      { Public declarations }



In the implementation of this method we can for example display a pop-up menu when the user right-clicks on the icon. Assuming you have created a pop-up menu named PopupMenu1, the following code will do the trick:

procedure TForm1.Icontray(var Msg: TMessage);


    CursorPos : TPoint;


    if Msg.lParam = WM_RBUTTONDOWN then begin


      PopupMenu1.Popup(CursorPos.x, CursorPos.y);

    end else




Normally you will include a menu item in the pop-up menu to show the form, so all you have to do is write an event handler to call the Show method of the form and optionally remove the icon from the system tray:

procedure TForm1.mnuShowClick(Sender: TObject);



    // Shell_NotifyIcon(NIM_DELETE, @NotifyIconData);


When the user closes the form, you can for example hide it instead of closing it and optionally show the icon again in the system tray if you have removed it when you made the form visible. For example:

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);


    Action := caNone;


    // Shell_NotifyIcon(NIM_ADD, @NotifyIconData);



You can have a menu item in the pop-up menu to exit the application. In this case you have to write an event handler for its click event to remove the icon and finish the application:

procedure TForm1.mnuExitClick(Sender: TObject);


    Shell_NotifyIcon(NIM_DELETE, @NotifyIconData);




The full source code example of this article can be downloaded from:


Share this article!

Follow us!

Find more helpful articles: