Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


System Tray

Posted on 1998-04-16
Medium Priority
Last Modified: 2010-04-04
How can I make a program in Delphi which run in System Tray, eg : like many anti virus program do or maybe volume  ?
Question by:de_ep
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Accepted Solution

ZifNab earned 200 total points
ID: 1361608
Hi de_ep,

This is well explained by Brendan Delumpa at his Delphi Corner page (http://www.delumpa.com/). Read for yourself (your question is explained in the second part) :

Hiding an Application from Windows

How do I keep an application from displaying a button on the Windows task bar and prevent users from using [Alt-Tab] to switch to it
once it executes?

By convention, any application's window that has its extended window style bit set as a tool window (WS_EX_TOOLWINDOW) will be hidden
from the task bar and will not be able to switched to. In Delphi, every form has a BorderStyle property of bsToolWindow, so you might think that
all you have to do is set the borderstyle of the main form of the application to bsToolWindow, and you'll be able to hide the application from the task
bar. Not really. The reason for this is because the main form of the application really isn't the main form of the application. Huh? Okay, I'll explain.

The application object that encapsulates all Delphi programs (TApplication) actually has a form of its own. It's totally transparent, so you can't really
see it. This is the window we need to interact with in order to hide the application. So how do we do it? Well, if you remember what I said at the top
of this tip, the Windows convention for "hiding" an application is to set its extended window style bit to WS_EX_TOOLWIN. And that's easily
done with a little Windows API trickery. Yes, that's right, trickery. We trick the application into displaying as a tool window instead of an
application window.

Specifically, what you have to do to accomplish this is to edit the project's source code. Here's a complete listing (it's actually pretty short):

program Project1;

  Unit1 in 'Unit1.pas' {Form1},

{$R *.RES}

//Declare a var to retrieve current window information
  ExtendedStyle : Integer;


  //Get the Extended Styles of the Application, by passing its
  //handle to GetWindowLong
  ExtendedStyle := GetWindowLong(Application.Handle, GWL_EXSTYLE);

  //Now, set the Extended Style by doing a bit masking operation.
  //OR in the WS_EX_TOOLWINDOW bit, and AND out the WS_EXAPPWINDOW bit
  //This effectively converts the application from an App Windows to a
  //Tool Window.
  SetWindowLong(Application.Handle, GWL_EXSTYLE, ExtendedStyle OR WS_EX_TOOLWINDOW
                                                 AND NOT WS_EX_APPWINDOW);
  Application.CreateForm(TForm1, Form1);

The code above is pretty well-commented, so I won't go into a lot of detail. But notice that in code above, I've treated the application object like a
window. It's legal to do that because the application object as I previously mentioned has its own window - it's just hidden. So by using a couple of
functions to retrieve then set the window display properties, we can achieve the desired effect. GetWindowLong is the first function I used. It's a
Windows API function that takes an application handle and a constant representing a bit offset stored in Windows that describes a particular
window. For our purposes, we pass the GWL_EXSTYLE offset value to retrieve the extended window style information. Once we've got that, it's
a simple matter of making a call to SetWindowLong to make the change to the application. SetWindowLong takes the application's handle, the
information offset, and what you want to change at that offset. Once called, it changes the window appropriately. The net effect is that no button is
displayed for the application on the task bar AND, you won't be able to [Alt-Tab] to the application. It's really hidden.

You should probably study the online documentation regarding the Get- and SetWindowLong Windows API functions to better familiarize yourself
with the functions. You'll see that you can do a lot more with these functions than what I've just brushed over here.

Getting Some Use Out of All This...

When I originally started writing this article, I was going to leave it at hiding an application from Windows. But then I thought to myself, "What use is
it?" The most obvious use of the technique is for creating system tray applications; that is, applications that add an icon to the system tray and are
invoked from there as opposed to the task bar. For example, the Audio Control (the little loudspeaker at the bottom right of your screen) is a
system tray application. So how do you create a system tray application? Easy. Just look at the code below:

{This sets up the application to be a system tray application.
 This is the main form for the application. It has a popup menu
 that will be used to display the main form, or close the application.

 And using the ShellAPI unit, we can then use a couple of calls to display
 the application's icon on the system tray, and make it respond to a
 right mouse click}

unit Unit1;


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

  TForm1 = class(TForm)
    PopupMenu1: TPopupMenu;
    ShowMainForm1: TMenuItem;
    N1: TMenuItem;
    ExitApplication1: TMenuItem;
    procedure FormCreate(Sender: TObject);
    procedure ShowMainForm1Click(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure ExitApplication1Click(Sender: TObject);
    procedure WndProc(var Msg : TMessage); override;
    IconNotifyData : TNotifyIconData;

  Form1: TForm1;


{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
  //Set the border icons to have only a system menu. This will
  //leave just the close button.
  BorderIcons := [biSystemMenu];

  //Now set up the IconNotifyData structure so that it receives
  //the window messages sent to the application and displays
  //the application's tips
  with IconNotifyData do begin
    hIcon := Application.Icon.Handle;
    uCallbackMessage := WM_USER + 1;
    cbSize := sizeof(IconNotifyData);
    Wnd := Handle;
    uID := 100;

  //Copy the Application's Title into the tip for the icon
  StrPCopy(IconNotifyData.szTip, Application.Title);

  //Add the Icon to the system tray and use the
  //the structure and its values
  Shell_NotifyIcon(NIM_ADD, @IconNotifyData);

procedure TForm1.WndProc(var Msg : TMessage);
  p : TPoint;
  case Msg.Msg of
    WM_USER + 1:
    case Msg.lParam of
      WM_RBUTTONDOWN: begin
                        PopupMenu1.Popup(p.x, p.y);

procedure TForm1.ShowMainForm1Click(Sender: TObject);

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

procedure TForm1.ExitApplication1Click(Sender: TObject);
  Shell_NotifyIcon(NIM_DELETE, @IconNotifyData);


There's really not much to this. Just study the code to see what's going on. But the important thing you should concentrate on is the Create method of
the form and what is done to the IconNotifyData structure. This is a record structure declared in the ShellAPI unit that stores information for a tray
icon. Notice the flags that were used: NIF_MESSAGE + NIF_ICON + NIF_TIP. These tell the icon to process application messages, use the
application's icon and its tip, respectively. Once we've set up all that, then it's a simple matter of creating the window interaction stuff, like we'd
normally do at design time. The tray icon doesn't come into play until runtime.

The other thing to look at is the override of the WndProc procedure. WndProc is short for Window Procedure. It intercepts all the messages sent
to the window, and acts as the central message dispatcher. In that procedure, you can trap specific Windows messages by overriding the inherited
procedure. In our case, we trap two things: the Msg field of the message sent to the application. If it was our custom message (WM_USER + 1)
defined for the IconNotifyData variable, then we want to handle a right-click. All other messages sent to the application are handled in their normal

I realize that this was pretty quick and dirty, so I encourage you to play around with the code. Just keep in mind that you have to do two things, if
you're going to create system tray application:

   1.You need to first create the "hiding" mechanism for the application. That was handled in the first part of this article
   2.Then, you need to create the interface so that you can interact with the application when its main for isn't being displayed. That was done in
     the second part.

Have fun!

Copyright ) 1997 Brendan V. Delumpa

Regards, ZiF.


Author Comment

ID: 1361609
Thank's a lot ZifNab

Featured Post

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question