Home · FAQ · Forums · Contact · Privacy Policy September 08 2008 20:16:48
Login

Username

Password


Not a member yet?
Click here to register.

Forgotten your password?
Request a new one here.
Games
Manic Miner
Jet Set Willy (in progress)
Cheese, Louise!
Hangman
Education
Flash Card Software
Programming
Delphi Tips/Tutorials
Shoutbox
You must login to post a message.

andrewbroad
19 August 2008
Yes indeed. During every Olympics since 2000, I've been thinking that Manic Miner and Jet Set Willy should be Olympic sports. On your marks, Jet Set, go!

jetsetdanny
19 August 2008
It's no wonder, really. Great minds think alike . And speaking of the Olympics, isn't it a shame that Flash Manic Miner is not an Olympic discipline?

andrewbroad
19 August 2008
Oops - that ) wasn't supposed to come out as a smiley! The Olympic Tennis has, of course, been the (latest) reason for my recent absence from the MM/JSW scene.

andrewbroad
19 August 2008
Wow, Danny, I just put that exact quote from Goldfinger (up to "heaviness" in my last Olympic Tennis report, and now I come here and see you've used it too!

jetsetdanny
19 August 2008
"This is gold, Mr Bond. All my life, I've been in love with its colour, its brilliance, its divine heaviness. I welcome any enterprise that will increase my stock, which is considerable."

jetsetdanny
19 August 2008
"Do or do not." Okay, I have done it, in-between my holiday trips. I have reclaimed Gold in Manic Miner.

andrewbroad
08 August 2008
I was watching Twin Peaks: Episode 1 the other day, and one of the characters said, "Jeez, Louise!" - so that's where it comes from!

andrewbroad
08 August 2008
Wow: I thought I was about to die at about 1600, but recovered for my second-best-ever game of Cheese, Louise with 3902! (Round 22 - ironically my best-ever was 3958 on Round 20).

andrewbroad
03 August 2008
Try not. Do or do not. There is no try... This organisation does not tolerate failure... The amazing sound of the killing hawks... From a great height, from a great heigh...eigh...eigh...eig
ht.


jetsetdanny
03 August 2008
I have tried to regain Gold, but I have failed due to stupid loss of life in Caverns 12 and 36, with no points gained from it. So it looks like Andrew will be reigning in MM in August...

andrewbroad
03 August 2008
Thanks, Danny. Ironically, it's not the birds that are the killers, but the crumbling floors. I died from a very long fall.

jetsetdanny
03 August 2008
Well done, Andrew, congratulations!

andrewbroad
03 August 2008
I WON THE GOLD MEDAL ON MY FIRST VISIT TO THE KILLER BIRDS!!! I got to Cavern 42 without loss of life, then sacrificed two lives, then died in Cavern 43 with my all-time high of 83092.

andrewbroad
01 August 2008
I feel that July's routine has served its purpose: I am now much more confident of reaching Cavern 31, much more familiar with the route through each cavern, and much more aware of the risks.

andrewbroad
01 August 2008
So from now on, I'm only going to play Flash MM at the weekends and holidays, trying to reach later caverns, and to set new all-time world highs for selected caverns.

andrewbroad
01 August 2008
Well, I certainly won't be playing Flash MM between working-days anymore. It has taken a very heavy toll on me in July: I have started losing other goals, and am now extremely tired.

jetsetdanny
01 August 2008
And in August I will be watching your progress every two or three days, as I will be offline most of the time (holiday away from home!). And hoping that my medals will withstand your assault .

jetsetdanny
01 August 2008
And then you will face the formidable Killer Birds! Anyway, improving your all-time personal high by just 1 points in the 60000 pts. range is an achievement worth congratulating!

andrewbroad
31 July 2008
Clearly, I have a lot of practising to do before I'm ready to pass Cavern 31, and then the floodgates should open for me if Caverns 32 to 42 are basically the same as 12 to 22.

andrewbroad
31 July 2008
I played my least efficient game of the whole month to reach Cavern 30, then sacrificed two lives there, and it paid off as I flunked Cavern 31 really badly, taking only one item.

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
jetsetdanny83396
andrewbroad83092
jetsetdanny82424
jetsetdanny81881
jetsetdanny52868
andrewbroad40885
more... 
 Cheese, Louise!
andrewbroad3902
andrewbroad2100
andrewbroad2096
andrewbroad2026
andrewbroad2000
andrewbroad1994
more... 
Hosted by inter.net.nz slightly neurotic web hostingGames developed by g2 developmentPowered by PHP-Fusion v6.00.106 © 2003-2005