Home · News · FAQ · Forums · Privacy Policy · Members List · Contact September 10 2010 06:33:53
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
spegla01:19:49
rap7502:34:23
korbach03:11:53
cubchris03:33:18
kid-e03:38:30
andrewbroad07:24:37
loz8112:56:00
cgibinladen13:31:10
allymo14:15:12
nitsypee14:29:37
flakothedog15:03:56
Shoutbox
You must login to post a message.

andrewbroad
07 September 2010
Congratulations to rap75 for raising the Kiwitris all-time high score to 81411.

playgirlbunny
03 September 2010
is watching street fighter and playing manic miner at the same time, how did i get this score lol xx

cubchris
31 August 2010
Thanks Andrew I'll give that a throw

andrewbroad
30 August 2010
The easiest and most efficient solution is to take the most central item first, and then proceed to traverse the screen anticlockwise.

cubchris
30 August 2010
When should I grab the middle thingy in attack of the mutant telephones - start or finish ?

andrewbroad
29 August 2010
Congratulations to cro-tica for taking the Manic Miner Gold Medal with 50717, and becoming one of apparently only six players ever to reach Cavern 28 (all-time No.5 azzubayr reached C.27 with 50947).

playgirlbunny
20 August 2010
haven't been on here 4 a while and im getting ova took by a certain sum1 xx

cubchris
14 August 2010
HOW CAN THE AMOEBATRONS BE SO TRICKY WHEN SO SIMPLE !

cubchris
14 August 2010
Just made 26th - I'm closing in on the top - in about 3 years

playgirlbunny
04 August 2010
omg getting better and better on manic miner xx

matt
22 July 2010
That's EXACTLY what it's for!!!

playgirlbunny
21 July 2010
im shouting is this wot this is 4 lol xx

thunder
14 July 2010
thanks guys that was the problem i will try and set a score

matt
13 July 2010
Hi thunder, if you've tried enabling Java like Andew's said, you might not have Java installed. Click the FAQ link at the top for more steps you can try.

andrewbroad
13 July 2010
Have you enabled Java in your browser, and either allowed all sites to use Java, or trusted both www.darnkitty.com and darnkitty.g2-tech.co.uk to do so?

Shoutbox Archive

Delphi Tips and Tutorials
System tray icons tutorial
Part 3 - Minimizing the application to the system tray

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

Part 3 Introduction

This tutorial builds upon previous parts, so if you arrived here via a search engine or similar, you may like to start at the beginning.
By the end of this tutorial, you will be able to:

  • Minimize the form to the system tray
  • Start the application without the main form visible

Source code for this tutorial

This part of the tutorial continues on from Parts 1 and 2. You can download source code for Part 2 here.
The code for this section can be downloaded here.

Tutorial begins here - 6 steps

STEP 1
Currently, when the application starts, our icon appears in the system tray, and our welcome form appears. But what if we don't want this form to appear, and only want the tray icon?
Easy. Add the following line to the project file: Application.ShowMainForm := False; after the line Application.Initialize in the project file. Your project file will now look like this:

  program SystemTrayIcon3;

  uses
    Forms,
    Unit1 in 'Unit1.pas' {Form1};

  {$R *.res}

  begin
    Application.Initialize;
    Application.ShowMainForm := False;
    Application.CreateForm(TForm1, Form1);
    Application.Run;
  end.

Checkpoint

Try running the application. Notice that the icon still appears in the tray as expected, and there is no sign of the main form. Great! But... how can we exit the application? We can't!

To close the application, you can right click the taskbar, select "Task Manager", select the Processes tab, and end the process (if you know its name). The easier way, if you are running through the Delphi IDE, is to switch to the Delphi IDE and press CTRL-F2.

STEP 2
Okay, let's code a way to exit the application. This should be nice and simple.

Add a new TMenuItem to the popup menu as shown on the right. Name this item miExit, and code its OnClick event as shown:

  procedure TForm1.miExitClick(Sender: TObject);
  begin
    Close;
  end;

That was easy! Try running the application now, and try right-clicking the icon and selecting "Exit" to make sure it does indeed work. No more using Task Manager to kill the process now.

STEP 3
The next part of this tutorial teaches how to minimize the application to the system tray. But first, we need a way of getting our form back on-screen.

Add another TMenuItem to the pop-up menu, call it "Show Form", name it miShowForm, and code its OnClick event as follows:

  procedure TForm1.miShowFormClick(Sender: TObject);
  begin
    ShowForm;
  end;

And code the new ShowForm procedure as follows:

  procedure TForm1.ShowForm;
  begin
    Visible := True;
    HideTrayIcon;
  end;

Another simple yet effective function. Now, when you select "Show Form" from the popup menu, the form becomes visible, and the tray icon disappears from the system tray. Let's code it so that when you left click the icon, the same thing happens. Add the following piece of code to the IconResponse procedure, replacing the existing WM_LBUTTONDOWN case:

  procedure TForm1.IconResponse(var Msg: TMessage);
    ...
      WM_LBUTTONDOWN:
      begin
        ShowForm;
      end;
    ...
  end;

STEP 4
Now comes the time to write the code to minimize the application to the system tray. Notice now how if you minimize the window, it goes to the taskbar like most other applications. We want it to appear to minimize into the system tray. What we actually want to do is hide the form, and make the tray icon reappear.

First of all, we will need to override the application's default Minimize handling. To do that, insert the following line of code as the first line in FormCreate:

  procedure TForm1.FormCreate(Sender: TObject);
  begin
    Application.OnMinimize := MinimizeMe;
    ...

And write the new MinimizeMe function as follows. Notice that it takes one parameter Sender which is a TObject.

  procedure TForm1.MinimizeMe(Sender: TObject);
  begin
    ShowTrayIcon;
    ChangeIcon;
    Visible := False;
  end;

Checkpoint

Try running the application. You can now click the icon to show the form, and minimize the application to hide the form into the taskbar. Great, we're done! Or are we...

Now though... what happens when you try and show the form a second time? Click the icon, and you may see the button appear on the taskbar, but the window won't come to the front. Then, worse still, if you try and minimize the form a second time, nothing happens! What's going on?

STEP 5
Since we have overridden Delphi's default OnMinimize behaviour, there are a couple more things we need to do to get everything to work smoothly. To correct the quirky behaviour described in the checkpoint above, change ShowForm to the following:

  procedure TForm1.ShowForm;
  begin
    Application.Restore;
    If WindowState = wsMinimized then
      WindowState := wsNormal;
    Visible := True;
    SetForegroundWindow(Application.Handle);
    HideTrayIcon;
  end;

Application.Restore and WindowState are two things that Delphi does internally which we now have to do since we are overriding Delphi's default behaviour. And the addition of SetForegroundWindow forces the window to come to the foreground when we show it.

Now, you should be able to show the form and minimize the form as many times as you want.

STEP 6
Rather than have the application retreat to the system tray on minimize, you might like to make it so that this happens when the user closes the form instead. To do this, open the form's OnClose event and add the following code:

  procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
  begin
    if Visible then
    begin
      Action := caNone;
      MinimizeMe(Self);
    end
    else
      Action := caFree;
  end;

Setting Action to caNone tells Delphi not to Free the form when the user clicks the close button. Instead, we will hide the form ourselves using a call to MinimizeMe.

It is important to remember to check if the form is visible first. If the form is not visible, it means that the user has selected "Exit" from the right-click menu, and in that case, we want to "caFree" the form. If we didn't check this, we would once again be left in the situation where the application cannot be terminated.

Tutorial complete

That concludes the System Tray tutorial.

As usual, if you have trouble compiling the project, see the Troubleshooting section at the bottom of the page, or compare your project to the sample project by downloading the source code at the top of the page.




Troubleshooting

Are you having trouble getting your application to compile? Your problem 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
flakothedog51235
cro-tica50717
allymo40034
jetsetwiley39731
waffiron39472
angeleyes2937571
kid-e37008
more... 
 Jet Set Willy
flakothedog23m53*104*
jongtfc20m2382
barneyb18m4667
nitsypee8m0538
matt5m5635
pablobarrel428m0935
dasse3m5623
more... 
 Blagger
smasher18948
korbach3065
playgirlbunny2879
matt1512
ryansew700
duduararas600
cubchris500
more... 
 Kiwitris
rap7581411
stu55317
nitsypee52984
visdodo28877
flakothedog21032
cubchris17811
playgirlbunny13056
more... 
 Cheese, Louise!
andrewbroad2803
?????1824
loz81277
joshnorton244
more... 

 JSW Endurance
flakothedog  39m 26s
pablobarrel42  135m 06s
bulsacu  251m 18s
more... 
Games developed by g2 Technology Ltd Powered by PHP-Fusion version 6 © 2003-2005