Minimize to Systray Demo

[Home]   [Projects]    [Delphi Techniques]   [Math Topics]   [Library]   [Utilities]

 

Available Now

Search

Google
 

Search WWW             

Search delphiforfun.org

Contact

Feedback:  Send an e-mail with your comments about this program (or anything else).

Help support DFF

 If you benefit from the website,  in terms of knowledge, entertainment value, or something otherwise useful, consider making a donation via PayPal  to help defray the costs.  (No PayPal account necessary to donate via credit card.)  Transaction is secure.

 

Google
 

Search WWW

Search delphiforfun.org

 

 

Here a program that shows what it takes to minimize a program to the system tray.  Like many things in this business, it is not as straightforward as one would would think.   There is no  "Minimize to tray" procedure in Delphi nor even in Windows. 

So here are the main tricks - documented in lots of places on the Web:

  • Create the icon that will be displayed in the tray, one time.
  • When it is time to "minimize" the program, 
    • Hide the form
    • Add the icon to the system tray
    • The normal window button in the taskbar area should  probably be deleted - no sense of having both the taskbar button and the systray icon.
  • When the user clicks the icon;
    • Show the form.
    • If the program was actually minimized,  restore the application.
    • Check WindowsState for the form and set to WS_Normal if necessary.  
    • Bring the restored main form to the foreground so that it can be seen on top of other open windows.
    • Delete the icon from the system tray (if desired - many programs leave the icon in the systems tray whether the program is visible or not).
  •  At termination time:  Delete the systray icon for the last time in a "FormDestroy" exit to ensure that it does not become "orphaned" through a program crash.

In  this demo, you can use the minimize or the close icon to hide the application.  In practice, programs would probably use one technique or the other.   It is also common for programs that use tray icons  to use right clicks for a popup menu to display user action choices.  That is not implemented in this demo.  

While this demo illustrates the function required to use systray icons, there are a number of free Delphi components available to simplify their use..  I would probably "de-componentize" one of these (convert it back to a unit) for use in real applications.

Addendum February 14, 2004:   I posted SystrayDemo2 today replacing my API calls with a slightly modified version of the freeware component CoolTrayIcon.     CoolTrayIcon can be downloaded from a number  of sites on  the web and the version with my mods is included in the download below.    CooltrayIcon looks to be a comprehensive  implementation including popup menus, animated icons, hint timing control, etc. .   I use the component uninstalled - meaning that my program is responsible for creating the instance and overriding property values from defaults as required.   I added Boolean properties automatically restore the application when the tray icon is clicked (ShowFormOnTrayIconClick)  and to  hide the tray icon when the program is visible (PersistentTrayIcon).     I also made the default icon be the icon defined for this program.  With these changes, adding the minimize to tray function only requires adding a couple of lines to the FormCreate procedure.

Addendum May 7, 2004:  The original SystrayDemo program did not switch between tab sheets on a page control after being restored - even thought he restored form looked fine.  The one line fix was posted today. - after restoring the form, its Visible property must be set to true.    I dug this out from the CoolTrayIcon component in SystrayDemo2.   Why is it necessary?  Looking at the source code for the Delphi's Visible procedure taught me  only that a lot of "behind the scenes" stuff happens in the process and some of it is even necessary!    

 Download SystrayDemo Source

 Download SystrayDemo2 Source

 
Created: February 10, 2004

Modified: February 01, 2007

 

 

 

 

 


  [Feedback]   [Newsletters (subscribe/view)] [About me]
Copyright © 2000-2008, Gary Darby    All rights reserved.