To show, hide a System Tray on task bar
Introduction
If you want to show a System Tray on task bar, to do integration is required.
You can show, hide a System Tray, popup a menu and receive the mouse event through integration API.
API Classes
There are 3 API classes to integrate with System Tray:
public class SystemTray; public class SystemTrayMenu; public interface SystemTrayCallback;
The class SystemTray stands for a System Tray instance, it is used to control the System Tray's appearance:
public class SystemTray { /** * Constructor. * The second is to construct System Tray with a windows 2000 style balloon info. */ public SystemTray(int nIcon, String szTip); public SystemTray(int nIcon, String szTip, String szInfo, String szInfoTitle, int nInfoType); /** * Change the system tray. The usage is the same as constructor. * Any int argument < 0 means not to change. * Any String argument == null means not to change. */ public void Change(int nIcon, String szTip); public void Change(int nIcon, String szTip, String szInfo, String szInfoTitle, int nInfoType); /** Show and hide */ public void Show(); public void Hide(); /** * Set the mouse event callback handler, to receive mouse event and menu event */ public static void setSystemTrayCallback(SystemTrayCallback callback); }
The second class SystemTrayMenu stands for a menu, you can popup it in the mouse event callback:
public class SystemTrayMenu { /** * Append a menu item to current menu */ public void Append(String title, int itemid); /** * Append a sub-menu to current menu */ public void Append(String title, SystemTrayMenu popup); /** * Append a separator line to current menu */ public void AppendSeparator(); /** * Show current menu */ public void Popup(); }
The interface SystemTrayCallback is to receive System Tray event:
public interface SystemTrayCallback { /** * If mouse event occurs, this method will be called. */ public void OnMouseClick(SystemTray tray, int mouseEvent); /** * When popup menu selected. */ public void OnMenuCommand(int menuid); }
Demo Code
Typical usage of System Tray:
1. Call constructor to create a System Tray instance:
systemTray = new SystemTray( 1, // 1 - the first 'icon image', 2 - the second, etc "Hello System Tray" // the tips string );
2. Create a menu instance:
systemTrayMenu = new SystemTrayMenu(); systemTrayMenu.Append("Hide", 1); systemTrayMenu.AppendSeparator(); systemTrayMenu.Append("Exit", 2);
3. Set the System Tray event handler:
SystemTray.setSystemTrayCallback(new SystemTrayCallback() { public void OnMouseClick(SystemTray tray, int mouseEvent) { if(mouseEvent == SystemTray.RIGHT_CLICK) { systemTrayMenu.Popup(); } } public void OnMenuCommand(int menuid) { if(menuid == 1) { systemTray.Hide(); } else if(menuid == 2) { System.exit(0); } } });
4. Show the System Tray:
systemTray.Show();
5. Call Hide on exit:
systemTray.Hide();
See Also
- See System Tray Introduction page for more informaion about System Tray.
- Hello System Tray page for a demo program of System Tray.
- Integrate API - 20K, API library to integrate with generated exe.
Add new comment