Minimize to Systray Demo

[Home]   [Puzzles & Projects]    [Delphi Techniques]   [Math topics]   [Library]   [Utilities]

 

 

Search

Search WWW

Search DelphiForFun.org

As of October, 2016, Embarcadero is offering a free release of Delphi (Delphi 10.1 Berlin Starter Edition ).     There are a few restrictions, but it is a welcome step toward making more programmers aware of the joys of Delphi.  They do say "Offer may be withdrawn at any time", so don't delay if you want to check it out.  Please use the feedback link to let me know if the link stops working.

 

Support DFF - Shop

 If you shop at Amazon anyway,  consider using this link. 

     

We receive a few cents from each purchase.  Thanks

 


Support DFF - Donate

 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.

Mensa® Daily Puzzlers

For over 15 years Mensa Page-A-Day calendars have provided several puzzles a year for my programming pleasure.  Coding "solvers" is most fun, but many programs also allow user solving, convenient for "fill in the blanks" type.  Below are Amazon  links to the two most recent years.

Mensa® 365 Puzzlers  Calendar 2017

Mensa® 365 Puzzlers Calendar 2018

(Hint: If you can wait, current year calendars are usually on sale in January.)

Contact

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

Search DelphiForFun.org only

 

 

 

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:

bulletCreate the icon that will be displayed in the tray, one time.
bulletWhen it is time to "minimize" the program, 
bulletHide the form
bulletAdd the icon to the system tray
bulletThe normal window button in the taskbar area should  probably be deleted - no sense of having both the taskbar button and the systray icon.
bulletWhen the user clicks the icon;
bulletShow the form.
bulletIf the program was actually minimized,  restore the application.
bulletCheck WindowsState for the form and set to WS_Normal if necessary.  
bulletBring the restored main form to the foreground so that it can be seen on top of other open windows.
bulletDelete the icon from the system tray (if desired - many programs leave the icon in the systems tray whether the program is visible or not).
bullet 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: May 12, 2018

 

 

 

 

 

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