PowerBuilder – Application Icon in the System Tray

Posted on Tuesday, December 14th, 2010 at 8:20 pm in

One challenge for Powerbuilder developers today is the lack of current information/programming tips relating to more current (10 and above) versions. This also applies to Windows versions as well. There are a variety of different solutions to having an application icon in the System Tray but almost without exception they relate to Windows 95/98 primarily. One of the best examples, called Icontray, can be found on the Roland Smith’s website TopWizProgramming.com.

This example launches the application and minimizes it to the system tray. Right clicking on the icon opens an options window. This window will open based on the location of the system tray on your desktop. It has only been tested on a single monitor machine so there may be the need to tweak it if multiple monitor configuration is used. On the options window is a datawindow which gets populated upon opening. This demonstrates how you can add or remove options based on some condition in the main application. The popup default is to close after ten seconds. You have the option to ‘pin’ the window so it always stays open.

Almost all of the system tray functionality is done via a series of API calls encapsulated within an nvo. These include:
Shell_NotifyIcon – adds, modifies, or removes the icon (ANSI and Unicode versions exist). This API uses a structure called NOTIFYICONDATA which passes the various elements and commands needed.
In this sample the NOTIFYICONDATA structure is set up to return the ‘callback’ message mapped to the PBM_CUSTOM01 event on the window associated with the icon. This has been mapped to a window event called ‘trayevent’. The callback itself takes the form of various events such as ‘left clicked’, ‘right clicked’, etc. If you are already using PBM_CUSTOM01 for something else, you can use any of the other PBM_CUSTOM events (they are numbered 1024 to 1098 for events 01 through 75).

LoadImage – loads an icon, cursor, or bitmap and returns a handle to it (ANSI and Unicode versions exist). These can be loaded from files themselves or can be extracted from .dlls they have been included in.
FindWindow – returns a handle to the System Tray window.
GetWindowRect – populates a structure with the coordinates of a window.
These two methods tell us where the system tray is.

The main application window allows you to change the icon ‘tip text’ which is displayed when you hover on the icon in the system tray. You can also have the icon ‘blink’ by pressing the ‘Blink’ button. This can be used when some notification condition occurs within the main application.

There is also a Bubblehelp button which is disabled. This should allow you to change the icon bubble help (and display the bubble help itself) but I am unable to get this to work. If anyone can figure it out I would appreciate knowing how.

Many system tray applications pop up a menu allowing you to perform some standard list of options. I’ve provided some objects for this but have not implemented them in this application since I want the application to be more flexible in regards to notifications and processing options (hence the options window).

You can download a .Zip file which contains the .pbl and target as well as the exports for the objects. It was written in PB11.5.1