TTrayIcon
Fuente: source/classes/ttray.prg
Standalone class (not derived from TControl)
TTrayIcon creates and manages an icon in the Windows system tray notification area. It allows applications to run minimized or in the background while providing user interaction via mouse click events, a tooltip, and a context menu. It is commonly used for background services, instant-messenger-style apps, and applications that minimize to the system tray.
Key DATA Members
| DATA | Type | Description |
|---|---|---|
oWnd | Object | The associated main window (hidden or visible) |
oWndParent | Object | The parent window for notification forwarding |
oIcon | Object | The icon displayed in the system tray |
cCaption | Character | Tooltip text shown on hover |
bLClicked | Block | Code block executed on left-click |
bRClicked | Block | Code block executed on right-click |
bLDblClick | Block | Code block executed on double-left-click |
Methods
| Method | Description |
|---|---|
New( oWnd, oIcon, cTip, bLClick, bRClick ) | Create a new tray icon |
End() | Remove the icon from the system tray and release resources |
Refresh( oIcon, cTip ) | Refresh the tray icon display |
SetIcon( oIcon, cTip ) | Change the icon and/or tooltip text |
SetText( cTitle ) | Set the caption text |
Example: Tray Icon with Menu and Restore on Double-Click
#include "FiveWin.ch"
function Main()
local oWnd, oTray, oMenu
DEFINE WINDOW oWnd TITLE "Tray Demo" SIZE 400, 300
oTray := TTrayIcon():New( oWnd, "app.ico", "My Application",;
{|| MsgInfo( "Left click" ) },;
{|| oMenu:Show( oTray ) } )
oTray:bLDblClick := {|| oWnd:Restore(), oWnd:SetFocus() }
MENU oMenu POPUP
MENUITEM "&Restore" ACTION {|| oWnd:Restore() }
MENUITEM "&Exit" ACTION {|| oTray:End(), oWnd:End() }
ENDMENU
ACTIVATE WINDOW oWnd CENTERED
return nil
Notes
- TTrayIcon does not inherit from TControl; it is a standalone class that interacts directly with the Windows shell notification area via
Shell_NotifyIconAPI calls. - The
oIconparameter can be an icon file path (.ico), a resource name, or a number identifying a resource icon. - Always call
End()before the application exits to remove the icon from the tray. Failure to do so may leave a ghost icon until the user hovers over it. - The
bLClicked,bRClicked, andbLDblClickcode blocks provide full mouse interaction. Right-click is typically used to show a popup context menu. - On Windows 11, tray icon visibility may depend on the "Notification Area" system settings. Use
SetIcon()to update the icon dynamically at runtime.