Create The Perfect Tray Application

 The Steps to Perfection

Windows Notification Area

 Delphi provides a simple Tray-Icon component. But to make the best use of all the features of TTrayIcon, you need to create a complex logic and master all the shoals of the Windows API. This post will help you with that and provide a template you can download for your perfect Windows Tray application!

Let's first summarize the steps to work with the TTrayIcon component in a Delphi VCL application to achieve the following features:

1. The application starts minimized to the Tray Area: 
2. Clicking the Form's Minimize or Close button hides the application to the system tray
   (The CTRL key overrides this on the Close button and the application is closed)
3. Left-clicking the Tray Icon Icon restores the application window
4. Right-clicking the Tray Icon shows the application's popup-menu:
   
Let's now go through the individual steps:
First you need to insert a TTrayIcon component into the Form:

 

Now set the following properties in the ObjectInspector:
 ‣ Set the Form's Visible property to False:
   
 ‣ Set the TrayIcon's Visible property to True:
   

And in the Form's OnCreate event, ensure the form starts hidden:

   

Now, when you compile and run the application, it will hide the application's Form and show it's Icon in the Tray area 👍

To add user-interactivity, in the main Form's interface section, declare the methods that will handle the Minimize/Close actions:
   
... and in the implementation section insert the associated method implementations:
     

Of course, you must also implement the Form's OnCloseQuery event that is needed when you CTRL-click the Form's Close button:

   

Now, when you compile and run your application, you can hide the main Form by clicking the Minimize or Close button in the Form's title bar - please note how the application is really closed when you CTRL-click the Close button.

(To make the Form visible in the first place you can temporarily reset the Form's Visible property back to True - don't forget to set it back to False later).

Now we need to add the functionality to show the Form from the Tray Icon: For this purpose, we add a handler to the Tray Icon's Click event:

   

Try it out: Compile and run the program. Then left-click the Tray Icon to show the application form window. 👁️

Now we can OPTIMIZE the Form's behavior:
   

   

At the end, we can create a Popup Menu that is shown when right-clicking the Tray Icon. For this purpose, we drop a TPopupMenu component on the main Form:

   

Don't forget to assign it to the Tray Icon's PopupMenu property:

   

This will make sure the Popup Menu is invoked when right-clicking the Tray Icon at run-time.

Of course, we first need to add the Menu Items to the Popup Menu and implement their respective click handlers:

1. For the first menu item, "Show the Main Form," we can reuse the same procedure ShowMainForm we already have used in the Tray Icon's Click handler:

   

2. For the second menu item, "Show the Windows Notification Area Settings," we can alternatively use a particular Control Panel Setting that still works in Windows 10 to show the Notification area Settings DIRECTLY:

   

   

These settings allow you to determine which tray icons should be displayed permanently and which should be moved to the "Overflow Area" when needed.

(You could also use Method 1 - but that requires the user to do another step for accessing the Notification Area settings because the newest Windows versions do not provide direct access to those settings).

3. For the last menu item, "Close the Application," we use a Flag FCanClose to achieve the same effect as with the CTRL key in the FormCloseQuery event handler:

   

   

Now we have covered the most important aspects of a Tray Application in the Windows Notification Area using Delphi's TTrayIcon component. 

If you have any questions, feel free to contact me at PA-Soft.com:

Don't forget to download the Source Code and the compiled TrayDemo application at the bottom of this page:

Downloads

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce vulputate tortor in nulla lacinia ac blandit massa condimentum. Donec pharetra adipiscing ante, ac convallis justo bibendum.
Nunc sapien mauris, imperdiet ac pellentesque quis, facilisis non sapien. Maecenas congue vehicula mi, id luctus mi scelerisque nec.