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

Known Bugs

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