# CoolTrayIcon, ver. 4.4.0

[Properties] [Methods] [Events]
[Hints] [Known Bugs] [Comments]

CoolTrayIcon is a tray icon component. It allows you to easily implement tray icons in your own applications, with various appearances and behaviors.

## Properties

Icon | property Icon: TIcon; The icon to display in the tray. This icon is 16x16 pixels and can be up to 16 colors (4 bit) in Win9x and WinNT 4, 4 billion colors (32 bit) in WinXP, and WinME and Win2000 support icons of a color depth up to the user's display mode. I don't know about Win2003 and Vista, but my guess is 32 bit support. If you use an icon file (*.ico) containing more than one icon, Delphi will use the first icon. If it's not 16x16 pixels it will be scaled, which probably won't look good. |
IconVisible | property IconVisible: Boolean; Whether to display the icon. This property has no effect in design mode. | Default false
Hint | type THintString = ShortString; property Hint: THintString; The tray icon's hint (max 128 chars), which is displayed when you move the mouse cursor over the tray icon. Use #13 as a separator in multi-line hints. NOTE: WinNT doesn't appear to support multi-line hints. NOTE: On some older Windows versions (before IE 5) the hint is max 64 chars. NOTE: The time until the hint window closes is determined by Windows, so don't bother trying to change the interval, because you can't. Setting Application.HintPause and Application.HintHidePause has no effect. |
ShowHint | property ShowHint: Boolean; Whether to show the hint. | Default true
PopupMenu | property PopupMenu: TPopupMenu; The popup menu you want to display when the icon is clicked. | Default true
LeftPopup | property LeftPopup: Boolean; Whether you want the popup menu to pop up when you click the left mouse button. Default is false, meaning only the right button will work. NOTE: LeftPopup must be false (or the popup menu unassigned) if you want to double click the tray icon without displaying the popup menu. LeftPopup must be false if you want CoolTrayIcon to handle a menu that has a default menu item. | Default false
Enabled | property Enabled: Boolean; Works like Enabled for other controls, enabling/disabling keyboard and mouse input. Useful when you want to temporarily disable the popup menu, say while you are displaying an about box or some other modal window. | Default true
IconList | property IconList: TCustomImageList; An ImageList containing icons. If it contains other images than icons, the tray icon won't change when using CycleIcons, but no error occurs. NOTE: When you assign to IconList IconIndex is set to 0. NOTE: In D2 and D3 the property is of type TImageList in stead of TCustomImageList. |
IconIndex | property IconIndex: Integer; The current icon in the assigned iconlist (or 0 if no iconlist is assigned). |
CycleIcons | property CycleIcons: Boolean; Animates the tray icon by cycling through all the icons stored in IconList and assigning them to Icon. When you set CycleIcons to true, it will begin with the first icon in IconList. NOTE: CycleIcons can be true even though you have no IconList. No cycling will occur in that case, but the cycle timer is still active, which will consume some resources. | Default false
CycleInterval | property CycleInterval: Cardinal; The time interval in milisecs. before selecting a new icon from IconList. |
DesignPreview | property DesignPreview: Boolean; Enables you to preview the tray icon in design mode. You can watch what the icon looks like, see its hint, enable/disable it, test the popupmenu, and animate it via CycleIcons. NOTE: Set DesignPreview to false to avoid the design time icon appearing when you run your program. This is only a problem in the Delphi IDE, not in the finished exe-file. I couldn't find a way to kill the design icon in run-time, so you'll have to do it yourself. NOTE: Setting DesignPreview in run-time has no effect. | Default false
Handle | property Handle: HWND; The tray icon's handle. | Read-only
WantEnterExitEvents | property WantEnterExitEvents: Boolean; Whether you would like to receive the OnMouseEnter and OnMouseExit events. These events require an internal timer to be active, so if you have no use for them set WantEnterExitEvents to false to minimize resource usage. | Default false
Behavior | type TBehavior = (bhWin95, bhWin2000); property Behavior: TBehavior; This property does not affect the tray icon's behavior in any visible way. It determines which messages are dispatched to the application when the user interacts with the tray icon (specifically, right mouse clicks result in a WM_RBUTTONDOWN followed by a WM_RBUTTONUP message in Win95 mode, but in Win2000 mode a WM_CONTEXTMENU, NIN_SELECT, or NIN_KEYSELECT message is dispatched. For details, see the Remarks section on this page). Probably the only case where you should consider changing the value of this property, is when you send messages to the tray icon programmatically, using PostMessage or SendMessage. NOTE: This property is public, not published. | Default bhWin95
MinimizeToTray | This property applies only when the owner of the tray icon component is a form. property MinimizeToTray: Boolean; Whether you want to hide the main form instead of minimizing it. The form will hide and the tray icon will show. | Default false

## Methods

Refresh | function Refresh: Boolean; Redraws the tray icon. You probably don't need to ever call this method. | Returns true or false
ShowBalloonHint | type TBalloonHintIcon = (bitNone, bitInfo, bitWarning, bitError, bitCustom); type TBalloonHintTimeOut = 10..60; function ShowBalloonHint(Title: String; Text: String; IconType: TBalloonHintIcon; TimeoutSecs: TBalloonHintTimeOut): Boolean; Shows a balloon hint (a kind of "sticky" popup hint). You can use up to 64 chars for the title and up to 255 chars for the text. The hint is visible for 10-60 seconds, or until the user clicks it (or until you programmatically hide it). You cannot show a balloon hint if the tray icon is not showing, and hiding the tray icon hides any balloon hint. NOTE: Balloon hints are not available in Win95 and Win98. Calling this method in those Windows versions has no effect. NOTE: From WinXP SP 2 the balloon hint supports a custom icon, which is the tray icon itself (the Icon property). To do this, set IconType to bitCustom. If you want the balloon hint to show a custom icon which is not the same as Icon, you must change Icon before calling ShowBalloonHint, then change Icon back again. NOTE: The HideBalloonHint method is automatically called by ShowBalloonHint, so you don't get multiple balloon hints. NOTE: There appears to be a bug with the timeout value in WinXP. See bugs section. | Returns true or false
HideBalloonHint | function HideBalloonHint: Boolean; Hides the balloon hint (if any). | Returns true or false
Popup | procedure Popup(X, Y: Integer); Shows the associated popupmenu (if any) at the specified screen coordinates. |
PopupAtCursor | procedure PopupAtCursor; Shows the associated popupmenu (if any) at the current cursor coordinates. This method is usually called automatically in response to an OnClick event, but you can call it manually if you want the popupmenu to appear at other times. |
BitmapToIcon | function BitmapToIcon(const Bitmap: TBitmap; const Icon: TIcon; MaskColor: TColor): Boolean; Renders and returns an existing icon based on the specified bitmap. The bitmap should be 16x16 pixels. Specify a color to be shown transparently or clNone for no transparency. NOTE: Bitmaps that are not exactly 16x16 will be scaled up or down. | Returns true or false
GetTooltipHandle | function GetTooltipHandle: HWND; All tray icons share the same tooltip window (the hint popup). This method returns the tooltip handle. | Returns handle or 0 if error
GetBalloonHintHandle | function GetBalloonHintHandle: HWND; All applications share the same balloon hint (if supported by the Windows version). This method returns the balloon hint handle. | Returns handle or 0 if error
GetClientIconPos | function GetClientIconPos(X, Y: Integer): TPoint; Returns the cursor position inside the tray icon. Will return coordinates between (-1,-1) and (16,16). You would expect the coordinates to vary between 0 and 15, but Windows adds a border 1 pixel wide to all tray icons, which is appparently considered to be part of the tray icons. Depending on whether the tray icon has other tray icons as neighbours to the left, right, top, or bottom, the border is inserted as Windows sees fit. So in other words, this method is not exact. You should expect a variation of 2 pixels. If anyone sees a way to patch this, let me know. | Returns position in a TPoint
SetFocus | function SetFocus: Boolean; Sets focus to the tray icon (if it is visible). NOTE: Supported from ver. 5.0 of Internet Explorer. | Returns true or false
ShowMainForm | This method applies only when the owner of the tray icon component is a form. procedure ShowMainForm; Shows the main form. NOTE: It is important that you use this method when displaying the form, as it contains important calls that affect how the form and the application display themselves. See the demo app. |
HideMainForm | This method applies only when the owner of the tray icon component is a form. procedure HideMainForm; Hides the main form. |
ShowTaskbarIcon | This method applies only when the owner of the tray icon component is a form. procedure ShowTaskbarIcon; Shows the application's taskbar icon (not to be confused with the tray icon). |
HideTaskbarIcon | This method applies only when the owner of the tray icon component is a form. procedure HideTaskbarIcon; Hides the application's taskbar icon (not to be confused with the tray icon). NOTE: If you display a child form the taskbar icon will become visible again automatically. This is standard Windows behavior. |

## Events

OnClick | TNotifyEvent; Works like the similar event for window controls. NOTE: The Click event corresponds to a MouseDown followed by a MouseUp. If no DblClick event is assigned the Click event fires immediately (ie. after the MouseUp). Otherwise it will wait for a small period of time to see if the user clicks again, in which case we have a double click. If no DblClick event is assigned a double click will actually cause the Click event to fire. This is intentional.
OnDblClick | TNotifyEvent; Works like the similar event for window controls.
OnMouseDown | TMouseEvent; Works like the similar event for window controls.
OnMouseUp | TMouseEvent; Works like the similar event for window controls.
OnMouseMove | TMouseMoveEvent; Works like the similar event for window controls.
OnMouseEnter | TNotifyEvent; Fired after the cursor enters the tray icon. Requires the WantEnterExitEvents property to be true.
OnMouseExit | TNotifyEvent; Fired after the cursor leaves the tray icon. Requires the WantEnterExitEvents property to be true.
OnCycle | procedure(Sender: TObject; NextIndex: Integer) of object; Fired just before the icon changes via CycleIcons. Returns the next index in the assigned imagelist.
OnBalloonHintShow | TNotifyEvent; Fired after the balloon hint shows itself. This doesn't have to occur immediately after you call the ShowBalloonHint method. In case another application is already showing a balloon hint, your application must wait for it to close before it gets to show the balloon hint itself. This event will tell you when it has shown the hint. NOTE: Not supported in Win2000. See bugs section.
OnBalloonHintHide | TNotifyEvent; Fired after the balloon hint closes because another application needs to show a balloon hint, or because you called either the ShowBalloonHint or the HideBalloonHint method. NOTE: Not supported in Win2000.
OnBalloonHintTimeout | TNotifyEvent; Fired after the balloon hint closes because it timed out. NOTE: Not supported in Win2000.
OnBalloonHintClick | TNotifyEvent; Fired when the user clicks the balloon hint. NOTE: Not supported in Win2000.
OnMinimizeToTray | This event applies only when the owner of the tray icon component is a form. TNotifyEvent; Fired when the application is about to minimize to tray. Useful if you want to do special processing, like animation effects, when the app. gets minimized (hidden). NOTE: MinimizeToTray must be true for this event to fire.
OnStartup | This event applies only when the owner of the tray icon component is a form. procedure(Sender: TObject; var ShowMainForm: Boolean) of object; Fired initially at application startup when the main form is about to display. Set the ShowMainForm parameter to false if you don't want the form to display. NOTE: May fire multiple times for an MDI form (once for every child form created at startup). NOTE: This event occurs before the form's OnCreate method (if used with a form). NOTE: Replaces the StartMinimized property found in earlier versions.

## Hints

- How do I start my app. with the tray icon visible and the main form invisible?
  At design time set IconVisible to true and set the form's Visible property to false. Set the ShowMainForm parameter to false in the OnStartup event. See the StartHidden demo.
- What is the proper way to restore the form?
  ALWAYS use the method ShowMainForm! This method contains important calls that affect how the form and the application display themselves. Simply setting the form visible or calling Application.Restore is not enough. A common symptom is the form with tabpages that don't update themselves because ShowMainForm wasn't used.
- I used the OnStartup event to hide the form, but I can still see a quick flash on the taskbar when the app. is starting?
  If it bothers you you can set the Application.ShowMainForm property in the main project source, like this:

        .....
        Application.Initialize;
        Application.ShowMainForm := False;
        .....

- Why does the tray icon not appear in my NT/2000/XP service app.?
  Set the service's Interactive property to true, set ServiceType to stWin32, and set the tray icon's IconVisible property to true. See also the bugs section concerning services in NT4.
- I want multiple tray icons in my app.?
  Just create a CoolTrayIcon object for each tray icon you want. The component is optimized to share resources between the tray icons.
- How do I send a message to the tray icon through code?
  Use PostMessage or SendMessage and specify the message as the LParam parameter. Here's an example of sending a WM_LBUTTONDOWN message, invoking the tray icon's OnMouseDown event:

        PostMessage(CoolTrayIcon1.Handle, WM_TRAYNOTIFY, Integer(CoolTrayIcon1), WM_LBUTTONDOWN);

- I want to hide ALL windows, including dialogs and popup forms?
  Minimize the app. before calling HideMainForm, like this:

        Application.Minimize;
        CoolTrayIcon1.HideMainForm;

- I want to override the "hide inactive tray icons" feature in WinXP?
  Calling the tray icon's Refresh method at regular intervals should do the trick (I think). Anyway, it's not a good idea to override standard Windows behavior.
- When does the OnStartup event occur?
  The OnStartup event occurs before the owner form's OnCreate event.
- How do I show the balloon hint for less than 10 seconds?
  Set a timer to close the hint prematurely, using the HideBalloonHint method.
- How do I check if balloon hints are available (since they don't work in Win95 and Win98)?
  This should work in D4 and up:

        uses ComCtrls;
        if (GetComCtlVersion < ComCtlVersionIE4) or
           ((Win32Platform = VER_PLATFORM_WIN32_WINDOWS) and
            (Win32MinorVersion <> 90)) then
          // Balloon hints NOT supported

- Can I use the ballon hints without the tray icon?
  Take a look at the zBalloon component.

## Known Bugs

- It appears there's a bug in WinXP (apparently both with SP 1 and SP 2 versions of WinXP - possibly it's a general bug independent of service packs). The bug makes the balloon hint appear for a random amount of time, no matter which timeout value you specify in the call to ShowBalloonHint. I haven't verified this with Microsoft, but it is not a problem in other Windows versions. Also, other tray icon components suffer from this problem, so it is most likely an issue with WinXP. Please tell me if you have this problem and you are not running WinXP SP 1 or SP 2.
- Some people report that the balloon hint events never fire. This is a Win2000 issue. It doesn't appear to matter which service pack, Internet Explorer version, or comctl32.dll version you use. This makes me suspect that Win2000 simply does not support these events.
- The tray icon may disappear in NT4 if it is used in an interactive service and you log off, then log back on. I've been trying to fix this, but have so far been unsuccessful. An explanation for why the problem occurs can be found here. (BTW: some service packs, like SP6 seem to fix this problem).
- It is possible to disable balloon hints in Windows. If the balloon hint doesn't display when it's supposed to (and if you're not using Win9x), you should verify this setting.
- Occasionally someone reports that an application using CoolTrayIcon prevents the user from logging off or shutting down until he manually terminates the application. I've more or less come to the conclusion that this is not a bug in CoolTrayIcon. More likely it's a bug in some other third party component. At least it seems people always get this error when they use CoolTrayIcon together with other third party components. If you get this error try removing the other components one at a time and see if that doesn't solve the problem (or even simpler - run my demo app. and see if it has the problem). Afterwards, if you still have reason to believe CoolTrayIcon is the problem, don't hesitate to tell me, but please perform this simple test of exclusion first. (NOTE: It would seem various components in Delphi's FastNet collection have this problem - independently of CoolTrayIcon).
  Also make sure you use CoolTrayIcon correctly. If your app. "closes to tray" (goes to the tray when the user clicks the form's close button) you need to handle the WM_QUERYENDSESSION message manually, or the app. (unsurprisingly) won't terminate when Windows shuts down. See the source in the CoolTrayTest demo for a solution.
- If you're using CoolTrayIcon with an ActionList (associated with popup menu items), you'll find that actions based on TCustomAction (like TFileExit) don't execute if the form that owns the tray icon is invisible. I'm guessing TCustomAction is designed to behave like this. However, this problem doesn't occur with standard actions.

## Comments

The CoolTrayIcon component is free for personal and commercial use. Feel free to use and improve it, but please include all original files if you redistribute the zip-file. If you have any comments or corrections to the component I would very much like to hear them. A lot of Delphi programmers have already told me they like the component and use it, which is a huge boost for my ego. Thanks a lot, guys - especially those of you who gave suggestions and pointed out the bugs.

If you use the component some credit somewhere in your application would be a nice gesture, but it is not a requirement.

The component should work on any Windows platform (Win9x, ME, NT, 2000, XP, 2003, Vista). If you experience any problems related to the operating system you use, please tell me. Also, it should work in Delphi 3 and up (Delphi 2?) and C++Builder 3 and up. Again, tell me if I'm wrong.

A word about services: CoolTrayIcon does not require its owner to be a form. This allows you to use it for non-windowed apps. such as services. Some of the properties, methods, and events don't make sense without a form, but rather than creating a superclass without these properties/methods/events I've simply let them stay in CoolTrayIcon itself. You can use them in your service app., but they will not do anything. I figured the component would be more flexible and easier to extend into subclasses this way.

Get the latest version from http://subsimple.com/delphi.asp.

Troels Jakobsen
troels.jakobsen@gmail.com