Nov 20

Take a look at your Task Bar. See the area where the time is located? Are there any other icons there? The place is called the Windows System Tray. Would you like to place your Delphi application’s icon there? Would you like that icon to be animated – or reflect the state of your application?

This would be useful for programs that are left running for long periods of time with no user interaction (background tasks you typically keep running on your PC all day long).

What you can do is to make your Delphi applications look as they are minimizing to the Tray (instead to the Task Bar – right to the Win Start button) by placing an icon in the tray and simultaneously making your form(s) invisible.

Fortunately, creating an application that runs in the system tray is pretty easy – only one (API) function, Shell_NotifyIcon, is needed to accomplish the task.

The function is defined in the ShellAPI unit and requires two parameters. The first is a flag indicating whether the icon is being added, modified, or removed, and the second is a pointer to a TNotifyIconData structure holding the information about the icon. 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.

First, in your main form’s Private section put the line:

TrayIconData: TNotifyIconData;

type

TMainForm = class(TForm)

procedure FormCreate(Sender: TObject);

private

TrayIconData: TNotifyIconData;

{ Private declarations }

public

{ Public declarations }

end;

Then, in your main form’s OnCreate method, initialize the TrayIconData data structure and call the Shell_NotifyIcon function:

procedure TForm1.FormCreate(Sender: TObject);

begin

with TrayIconData do

begin

cbSize := SizeOf(TrayIconData);

Wnd := Handle;

uID := 0;

uFlags := NIF_MESSAGE + NIF_ICON + NIF_TIP;

uCallbackMessage := WM_ICONTRAY;

hIcon := Application.Icon.Handle;

StrPCopy(szTip, Application.Title);

end;

Shell_NotifyIcon(NIM_ADD, @TrayIconData);

end;

The Wnd parameter of the TrayIconData structure points to the window that receives notification messages associated with an icon.

The hIcon points to the icon we want to ad to the Tray – in this case Applications main icon is used.

The szTip holds the Tooltip text to display for the icon – in our case the title of the application. The szTip can hold up to 64 characters.

The uFlags parameter is set to tell the icon to process application messages, use the application’s icon and its tip. The uCallbackMessage points to the application defined message identifier. The system uses the specified identifier for notification messages that it sends to the window identified by Wnd whenever a mouse event occurs in the bounding rectangle of the icon. This parameter is set to WM_ICONTRAY constant defined in the interface section of the forms unit and equals: WM_USER + 1;

You add the icon to the Tray by calling the Shell_NotifyIcon API function. The first parameter NIM_ADD adds an icon to the Tray area. The other two possible values, NIM_DELETE and NIM_MODIFY are used to delete or modify an icon in the Tray – we’ll see how later in this article. The second parameter we send to the Shell_NotifyIcon is the initialized TrayIconData structure.

If you RUN your project now you’ll see an icon near the Clock in the Tray. Note three things.

  • First, nothing happens when you click (or do anything else with the mouse) on the icon placed in the Tray – we haven’t created a procedure (message handler), yet.
  • Second, there is a button on the Task Bar (we obviously don’t want it there).
  • Third, when you close your application, the icon remains in the Tray.

Let’s solve this backward. To have the icon removed from the Tray when you exit the application, you have to call the Shell_NotifyIcon again, but with the NIM_DELETE as the first parameter. You do this in the OnDestroy event handler for the Main form.

To hide the application from the Task Bar we’ll use a simple trick. In the Projects source code adds the following line: Application.ShowMainForm := False; before the Application.CreateForm(TMainForm, MainForm); in Project1.bdsproj E.g. let it look like:

program Project1;

uses

Forms,

Unit1 in ‘Unit1.pas’ {Form1};

{$R *.res}

begin

Application.Initialize;

Application.ShowMainForm := False;

Application.CreateForm(TForm1, Form1);

Application.Run;

end.

And finally to have our Tray icon respond to mouse events we need to create a message handling procedure. First we declare a message handling procedure in the public part of the form declaration: procedure TrayMessage(var Msg: TMessage); and message WM_ICONTRAY; Second the definition of this procedure looks like:

procedure TForm1.TrayMessage(var Msg: TMessage);

begin

case Msg.lParam of

WM_LBUTTONDOWN:

begin

ShowMessage(‘Left button clicked – let”s SHOW the Form!’);

Form1.Show;

end;

WM_RBUTTONDOWN:

begin

ShowMessage(‘Right button clicked – let”s HIDE the Form!’);

Form1.Hide;

end;

end;

end;

This procedure is designed to handle only our message, the WM_ICONTRAY. It takes the LParam value from the message structure which can give us the state of the mouse upon the activation of the procedure. For the sake of simplicity we’ll handle only left mouse down (WM_LBUTTONDOWN) and right mouse down (WM_RBUTTONDOWN). When the left mouse button is down on the icon we show the main form, when the right button is pressed we hide it. Of course there are other mouse inputs messages you can handle in the procedure, like, button up, button double click etc.

That’s it. In the next article you’ll see how to animate the icon in the Tray and how to have that icon reflect the state of your application. Even more, you’ll see how to display a pop up menu near the icon. Check the entire code, to make sure you haven’t missed something from this part.

Download Source Code

Incoming search terms for the article:

delphi WM_ICONTRAY, wm_icontray delphi, System Tray Delphi, delphi xe system tray, delphi xe2 system tray, application icon delphi, delphi xe2 minimize, delphi xe2 taskbar, wm_icontray delphi xe, delphi system tray, show hide delphi, delphi xe create tray application, delphi tray application, delphi tray message, ttrayicon delphi xe, delphi tray, delphi xe2 minimize event, delphi systray, delphi xe tray icon, delphi application in system tray, tray icon in delphi, delphi WM_ICONTRAY unit, trayicon delphi 6, Delphi SySTray message, system tray delphi xe, delphi XE2 TTrayIcon, delphi tray tooltip, Minimize delphy in notification, Delphi Shell_NotifyIcon, delphi system tray icon, delphi systray msg, how to add a application to tray - delphi 7, delphi message taskbar, send double click to tray delphi, shell_notifyicon delphi, sizeof(65533) delphi, delphi tray program, delphi xe cbSize := SizeOf(TrayIconData);, delphi xe start minimized to tray, delphi xe2 application icon, delphi 2010 wm_icontray, delphi 6 tray, delphi xe2 hide taskbar, xe2 hide taskbar, WM_ICONTRAY, delphi 6 system tray application, delphi xe ttrayicon, delphi 6 system tray, how to prevent a window to minimize delphi, How to invoke traymessage on delphi 6, how to put application in system tray in delphi 7, how to minimize to system tray delphi 7, how to generate pop up message on notification area icons using c/c, hide from toolbar delphi xe, how put a icon on program with delphi 6, how do invisible icon of program delphi, delphi xe try application, hide taskbar icon xe2, hide show form delphi xe, hide or show taskbar icons delphi, how to put your delphi application in icon tray, how to run program invisible from tray icon, keep program running with ttrayicon delphi, delphi XE tray icon application, delphi xe tray application, keep showing icon in tray icon delphi, make a tray application delphi xe, delphi xe tray applicaiton, make app in try DElphi, make program invisible taskbar, init tray delphi, init app with main form on tray delphi, how to show zeoslib icons on toolbar in xe2, how to trasy taskbar in delphi 7, howto delphi close a form at initialization, delphi xe TrayIcon, delphixe2 code, icon system tray hidden delphi, icons delphi xe trayicon, icontray delphi, how add program in system tray using delphi, delphi xe2 ???, hide application tray delphi first run, delphiXE TNotifyIconData, delphie systemtray, delphi: system tray icon, delphi6 mensaje en el icontry, delphi xe2 windows sendto, delphi xe2 unit function procedure, delphi xe windows taskbar notification area, delphi xe2 trayicon minimize problem, delphi xe2 minimize to tray, Delphi XE2 trayIcon function, delphi xe2 trayicon, delphi xe2 tray application, delphi xe2 tray, delphi xe2 not int taskbar, delphi xe2 taskbar hide, delphi xe2 systray, delphi xe2 tasbar use

Leave a Reply

Features Stats Integration Plugin developed by YD