How To Create a Tray Icon Controlled Application in Delphi

Problem:

We want to create a System Tray Icon featured application that doesn’t shut down when the user closes the main window. Instead, the form simply hides and is reactivated when the user clicks the associated tray icon.

Solution:

Tested in Delphi 2009.

1. Hide the main form.

Since closing the main form automatically terminates the application we need to hide it from the user. This is done by setting Application.ShowMainForm to False in the project settings.

Application.Initialize;
Application.ShowMainForm := False;
Application.MainFormOnTaskbar := True;
:
Application.Run;

2. Add a TTrayIcon to Main Form

Set the Icon property and add an OnClick event handler that displays your real main form (the one that the user will see).

procedure TMainForm.TrayIcon1Click(Sender: TObject);
begin
  MyOtherForm.Show;
end;

3. Drop a popup menu on the main form and connect it to the tray icon.

Add a close menu item that calls Application.Terminate to kill the application.

procedure TMainForm.Close1Click(Sender: TObject);
begin
  Application.Terminate;
end;

4. Make the real main form show on start of application

Simply add a OnCreate event handler to the form and invoke Show.

procedure TMyOtherForm.FormCreate(Sender: TObject);
begin
Show;
end;

That’s it!

Alternative solution 1.

OBSERVE! This solution has the rather big flaw that it stops the computer from shutting down.

1. Intercept the automatic termination mechanism when the main form is closing

procedure TMainForm.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
  // Minimize application instead of closing
  CanClose := False;
  Application.Minimize;
  // Remove the main form from the Task bar
  Self.Hide;
end;

2. Clicking on the tray icon restores the application

procedure TMainForm.TrayIcon1Click(Sender: TObject);
begin
  // Restore application from minimized state
  Application.Restore;
  Application.BringToFront;
  // We must invoke Show on the main form, otherwise
  // it won't be able to minimize properly a second time
  // Tested on D2009 Upd 3, and Windows XP
  Self.Show;
end;

3. Add a popup menu with a close action to the tray icon, and use Application.Terminate in the event handler to kill the application

procedure TMainForm.CloseActionExecute(Sender: TObject);
begin
  Application.Terminate;
end;

Alternative solution 2

If you want to do it without the TTrayIcon component, this article has what you need: System Tray Delphi Application – Quick and Easy

RSS feed | Trackback URI

1 Comment »

Comment by Aloysius Tan
2010-02-22 09:48:57

Hi can the icon be create by the site of the browser? To locate it beside the minimize button instead at the system tray? thanks

 
Name (required)
E-mail (required - never shown publicly)
URI
Your Comment (smaller size | larger size)
You may use <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> in your comment.