Class TrayIcon() Foundation

A class for managing system tray icons with notification and interaction capabilities.

Description

The TrayIcon class provides functionality to install and manage icons in the system's notification area (system tray). Once such an icon is installed, the class enables applications to display custom tooltips and popup notifications, and to handle user interactions such as mouse clicks.

System tray icons are frequently used for controlling applications which do not have a UI, or for displaying status information for a temporary asynchronous operation such as a file upload.

To display an icon in the system tray and to display notifications in the system's tray area, all that is required is to create an instance of the TrayIcon class and use the methods :setIcon() and :showBalloonTip(). However, in order to be able to customize the icon's behavior, such as the way the icon responds to left mouse button clicks, a subclass of the TrayIcon class must be created. You can then overwrite the required callbacks for defining the desired behavior in this user-defined class. This is shown in the example below.

Constructors
:new()
Initializes a new TrayIcon instance.
Life Cycle
:create()
Allocates resources for the TrayIcon instance.
:destroy()
Frees resources.
Methods
:hide()
Hides the tray icon.
:setAnimationTimeout()
Sets timeout for animating icons.
:setDLLName()
Sets the name of the DLL to load resources from.
:setIcon()
Changes the tray icon.
:setToolTipText()
Changes the tooltip text.
:show()
Shows the tray icon.
:showBalloonTip()
Displays a balloon tip notification.
Event Handlers
:onAfterMenuPopup()
Popup menu is closed.
:onBalloonTipClick()
Balloon tip notification is clicked.
:onBalloonTipHide()
Balloon tip notification is hidden.
:onBalloonTipShow()
Balloon tip notification is shown.
:onBeforeMenuPopup()
Popup menu is displayed.
:onLbClick()
Left mouse button is clicked.
:onMotion()
The mouse is moved.
:onRbClick()
Right mouse button is clicked.
Examples
Basic usage of the TrayIcon class
#include "xbp.ch" 
#include "appevent.ch" 

PROCEDURE Main() 
LOCAL oTrayIcon 
LOCAL oIcon 
LOCAL oButton 

  SET CHARSET TO ANSI 

  // Load the icon to be used for the tray icon 
  oIcon := XbpIcon():new():create() 
  oIcon:load( "xppnat.dll", 1 ) 

  // Create the tray icon object, set the icon and tooltip text 
  oTrayIcon := MyTrayIcon():new():create() 
  oTrayIcon:setIcon( oIcon ) 
  oTrayIcon:setToolTipText( "Tray icon created by Xbase++" ) 

  // Show the icon in the tray area 
  oTrayIcon:show() 

  // Create a push button for simulating displaying a notification 
  // via a balloon tip 
  oButton := XbpPushButton():new() 
  oButton:activate := {|| oTrayIcon:showBalloonTip("Notification Message", "Test", "information")} 
  oButton:caption  := "Display notification" 
  oButton:create(,,, {150,40} ) 
  CenterControl( oButton ) 

  @ 2,7 SAY "The tray icon is active now. Right-click to open the pop-up menu." 
  @ 3,7 SAY "Hover the mouse over the icon to display the tooltip." 

  SetAppWindow():showModal() 
  oTrayIcon:destroy() 
RETURN 


// User-defined tray icon class with overloaded event handler for populating the 
// popup menu with items 
CLASS MyTrayIcon FROM TrayIcon 
EXPORTED: 
  METHOD onBeforeMenuPopup() 
ENDCLASS 


// Method called by the framework when the popup menu is about to be opened. 
// Populate the menu with items if it is empty. 
METHOD MyTrayIcon:onBeforeMenuPopup( oMenu ) 
LOCAL nItems 

  nItems := oMenu:numItems() 
  IF nItems > 0 
     RETURN 
  ENDIF 

  oMenu:addItem( {"Test Item", {|| MsgBox("Test item was clicked!", "Xbase++ Tray Icon Example")}} ) 
  oMenu:addItem( {,, XBPMENUBAR_MIS_SEPARATOR} ) 
  oMenu:addItem( {"Quit", {|| PostAppEvent(xbeP_Close)}} ) 
RETURN 

Feedback

If you see anything in the documentation that is not correct, does not match your experience with the particular feature or requires further clarification, please use this form to report a documentation issue.