Home · News · FAQ · Forums · Privacy Policy · Members List · Contact February 04 2012 06:52:36
Login

Username

Password


Not a member yet?
Click here to register.

Forgotten your password?
Request a new one here.
Games
Manic Miner
Blagger
Jet Set Willy
Kiwitris
Cheese, Louise!
Daily Sudoku
Third-party showcase
Mountain Party
The Pyramid Maze
Many more...
Other things
Hangman
Flash Card Software
Delphi Tips/Tutorials
Future Games
Last Seen Users
quumpher00:50:29
dangman1302:23:56
matt07:17:53
manicminerman12:17:31
keithb313:32:57
jimbo13:40:42
mario106614:20:45
eastey14:44:44
kimminermad14:44:49
goldenspiral15:05:57
angelalouise10016:42:11
Shoutbox
You must login to post a message.

matt
03 February 2012
Hi Darrell, the original uses Java which might not be installed, go here and it will tell you http://java.com/en/downlo
ad/testjava.jsp


darrell
02 February 2012
the oringenal jet set willy wont open, the other 2 do but the original dont,, any advice anyone?

darrell
02 February 2012
THANKS SO MUCH MATT , YOU BROUGHT ME RIGHT BACK TO MY CHILDHOOD,, , THANKS AGAIN MATE

kimminermad
20 January 2012
LOVE THIS SITE

slimboats
15 January 2012
This is a cool site, l loved playing manic miner back in the early 80s when l was just started high school

ian_arvey
14 December 2011
Hello

aimeee123
22 September 2011
hey andrewboard hows it hanging my man,

monpoker
01 September 2011
hanging on...

hoppy
31 August 2011
helloooo

andrewbroad
01 August 2011
Wow: I didn't expect that I would be holding three gold medals at this point in time! Jet Set Willy, Kiwitris and Cheese, Louise! – and all with really mediocre performances, it has to be said.

rajji-miner
25 July 2011
level 8 wardo

cro-tica
28 May 2011
Thanks flakothedog ;-). I've achieved my goal to finish cavern 43, but I still don't have a routine for it.

flakothedog
27 May 2011
cro-tica - nice work mate. You've got me beaten cos I've forgotten my routine for Killer Birds!

newzealandgal
13 May 2011
Black Friday?? :0

newzealandgal
13 May 2011
Argh

Shoutbox Archive

Delphi Tips and Tutorials
System tray icons tutorial
Part 1 - Creating a system tray icon

Delphi tips and tutorials -> System tray icons tutorial -> Part 1 -> Part 2 -> Part 3

Part 1 Introduction

By the end of this tutorial, you will be able to:

  • Write an application in Delphi which will show an icon in the system tray, and also remove this icon.

Source code for this tutorial

This tutorial starts from scratch to help you better understand the steps required to create a system tray icon. If you do not want to follow the steps yourself, you can download the source code in an example project by clicking here.

Things to note

I use the terminology System Tray throughout this tutorial to refer to the area containing icons next to the clock in Windows 2000 and above. You may also hear this area called the Notification Area.

Tutorial begins here - 8 steps

STEP 1
This tutorial starts with a blank application. Start a new application by selecting   File -> New -> Application

STEP 2
Add the following two variables to the private declarations section of TForm1:

   { Private declarations }
   FIconShown: Boolean;
   FTrayIconData: TNotifyIconData;

FIconShown will be set to true once we show the icon. We can use this variable to ensure we don't waste time displaying the icon twice.
FTrayIconData is a structure that contains the actual icon itself. The type TNotifyIconData is declared in the unit ShellApi, so you will need to add this unit to the uses clause.

STEP 3
In this step we will do the actual creating of the icon and showing it. Therefore, this is the most complicated step, so pay close attention :-)
Create a new procedure called ShowTrayIcon. When called, this procedure will create an icon in the system tray.

  procedure TForm1.ShowTrayIcon;
  begin
    if not FIconShown then
    begin
      with FTrayIconData do
      begin
        uID := 1;
        Wnd := Handle;
        cbSize := SizeOf(FTrayIconData);
        hIcon := Application.Icon.Handle;
        uCallbackMessage := WM_ICONRESPONSE;
        uFlags := NIF_TIP + NIF_MESSAGE + NIF_ICON;
        StrPCopy(szTip, 'Here is your system tray icon, at your service!');
      end;
      Shell_NotifyIcon(NIM_ADD, @FTrayIconData);
      FIconShown := True;
    end;
  end;

The TNotifyIconData variable FTrayIcondata contains many horrible properties that must be set. I will run through several of them here (however if you aren't interested, you can skip over them):

hIcon - the handle of the icon that will be used. Here we are telling the application to use the same icon as the form itself. In Part 2 we will use a custom icon.
uCallbackMessage - the icon will post this message to the form when the icon is interacted with (for example, when it is clicked). In this tutorial, nothing will happen when the icon is clicked - this will be expanded in Part 2.
szTip - this is the tooltip that will show when you hover the mouse over the icon.

Some of the other properties will be touched on in later parts. To actually display the icon, this procedure calls Shell_NotifyIcon with the NIM_ADD flag, and the memory address of our FTrayIconData variable.

Now that that's out of the way, we need to declare WM_ICONRESPONSE. Add the following constant at the top of the unit - after the first uses clause but before the type clause. This is a custom Windows message that the tray icon will send when we interact with it.

  const
    WM_ICONRESPONSE = WM_USER + 1;

STEP 4
Now we need a similar procedure to hide the icon. You'll be pleased to know it's a lot simpler than the code to create the icon! Here it is:

  procedure TForm1.HideTrayIcon;
  begin
    if FIconShown then
    begin
      Shell_NotifyIcon(NIM_DELETE, @FTrayIconData);
      FIconShown := False;
    end;
  end;

This procedure checks to see that the icon is actually being shown, and if it is, it removes it by calling Shell_NotifyIcon with the NIM_DELETE flag.

STEP 5
Using the Delphi IDE, create a button on the form, and give it an amusing caption (perhaps something a bit more imaginative than I have done here!):

We will code this button so that when clicked, it will cause the icon to be shown in the system tray, and when clicked again, the icon will be removed from the tray.

STEP 6
Double-click the button to access the OnClick event. In this event, we want to call the ShowTrayIcon procedure if the icon is not visible, and HideTrayIcon if the icon is visible. Here is the code - nice and simple:

  procedure TForm1.Button1Click(Sender: TObject);
  begin
    if FIconShown then
      HideTrayIcon
    else
      ShowTrayIcon;
  end;

This code reads simply: If the tray icon is currently visible, then hide it, else, show it.

STEP 7
There is one more thing that needs to be added to the project to get it to compile. Create a procedure with the following signature and definition:

  procedure IconResponse(var Msg: TMessage); message WM_ICONRESPONSE;

  procedure TForm1.IconResponse(var Msg: TMessage);
  begin
    // Leave empty
  end;

For now, leave this procedure empty. This procedure defines what happens when the user clicks the icon in the tray. We will code this procedure in the next section of the tutorial.

STEP 8
One final piece of housekeeping. If you run the application now, then press the button to create the icon, then close the application, the icon sticks around in the tray. When you hover over the icon, it will remove itself from the tray.

What we want to do is remove the icon from the tray when the application is closed (if it is being shown). Create a new OnDestroy event for Form1, and enter the following code:

  procedure TForm1.FormDestroy(Sender: TObject);
  begin
    HideTrayIcon;
  end;

As you may recall from Step 4, the HideTrayIcon procedure removes the icon from the tray only if it actually exists there.

Tutorial complete

Run the application, and you should see your form appear with the "Click Me" button. Click this button, and you will see your icon appear in the system tray! Click the button a second time, and the icon will vanish.


There it is, on the left hand side!

If you hover the mouse over the icon, you should see the tool tip specified in Step 3!

If the project will not compile, see the Troubleshooting section at the bottom of the page.

Discussion

The icon is not very interesting is it - in fact it's the standard Delphi icon. Part 2 of this tutorial shows you how to change the icon to something more interesting, and also make it respond when clicked.

Where to next?

Part 2 covers changing the icon, and responding to mouse clicks by showing a popup menu.




Troubleshooting

Problem: Undeclared identifier: 'TNotifyIconData' (or Shell_NotifyIcon or NIM_DELETE or several others)
Solution: You need to add ShellAPI to the uses clause.

Problem 1: Undeclared identifier: 'WM_ICONRESPONSE'
Problem 2: Illegal message method index
Solution: Review step 3 above, and ensure you have added the WM_ICONRESPONSE constant right at the top of the unit, after the uses clause but before the type clause.

Do you have another problem not covered above? It might be covered in the forum below. If not, you can ask there yourself.

Forum coming very very soon!

Google





Current high scores    

 Manic Miner
allymo57137
johnnyjetset42004
rod-dy37579
manicmancity34538
gascoyne31080
blissy30880
oklahoma30413
more... 
 Jet Set Willy
jongtfc24m11*104*
jimbo20m0762
manicminerman15m5860
mario10663m0713
matt3m1810
goldenspiral0m252
tharnkrise0m372
more... 
 Blagger
discusb29895
angelalouise10027581
blagger15553
jongtfc3802
mario10663757
jimbo2711
lovespectrum1300
more... 
 Kiwitris
manicminerman56559
jongtfc36650
goldenspiral10210
tharnkrise4480
mario10663517
more... 
 Cheese, Louise!
andrewbroad1280
?????1253
mario1066769
chrissian205
more... 

 JSW Endurance
jongtfc  27m 21s
more... 
Games developed by g2 Technology Ltd Powered by PHP-Fusion version 6 © 2003-2005