Simple system tray module


👉Blast3D is now FREE👈

Support Future Updates by Becoming a Blitz Backer on Patreon.

 

Tweet blitzmax code-archives miscellaneous
BlitzCoder

Simple tray module that allows you to place an icon and hook it up to mouse messages.

Author: ozak

Win32 only! You can call the functions on other platforms and they will simple do nothing. That way you can support multiple platforms while still having trayapp support on windows.

Example (at the bottom part) of hiding app window on pressing close button and showing it when pressing the tray icon.

' Tray minimize win32 application

Import MaxGUI.drivers

?Win32
' tray icons 
'
Const NIM_ADD:Int           = 0
Const NIM_MODIFY:Int        = 1
Const NIM_DELETE:Int        = 2
Const NIM_SETFOCUS:Int      = 3
Const NIM_SETVERSION:Int    = 4

Const NIF_MESSAGE:Int   = $00000001
Const NIF_ICON:Int      = $00000002
Const NIF_TIP:Int       = $00000004
Const NIF_STATE:Int = $00000008
Const NIF_INFO:Int      = $00000010
Const NIF_GUID:Int      = $00000020

Type TNotifyIconData
    Field Size:Int
    Field HWND:Int
    Field id:Int
    Field Flags:Int
    Field CallbackMessage:Int
    Field Icon:Int              ' HICON 
    Field Tip:Long              ' array [0..63] of AnsiChar;
    Field Tip2:Long
    Field Tip3:Long
    Field Tip4:Long
    Field Tip5:Long
    Field Tip6:Long
    Field Tip7:Long
    Field Tip8:Long
EndType

Extern "WIN32"
    Function Shell_NotifyIcon:Int( message:Int, notifyicondata:Byte Ptr) = "Shell_NotifyIconA@8"
EndExtern

Function SetNotifyIconDataTip( nid:TNotifyIconData, s:String)
    MemClear( Varptr nid.Tip, 64)
    If s.length > 0 Then
        Local p:Byte Ptr = s.ToCString()
        If s.length < 64 Then
            MemCopy( Varptr nid.Tip, p, s.length)
        Else            
            MemCopy( Varptr nid.Tip, p, 63)         
        EndIf
        MemFree( p)
    EndIf
EndFunction

'
' window messages (allso used by tray icon)
'
Const WM_MOUSEMOVE:Int        = $0200
Const WM_LBUTTONDOWN:Int      = $0201
Const WM_LBUTTONUP:Int        = $0202
Const WM_LBUTTONDBLCLK:Int    = $0203
Const WM_RBUTTONDOWN:Int      = $0204
Const WM_RBUTTONUP:Int        = $0205
Const WM_RBUTTONDBLCLK:Int    = $0206
Const WM_MBUTTONDOWN:Int      = $0207
Const WM_MBUTTONUP:Int        = $0208
Const WM_MBUTTONDBLCLK:Int    = $0209

'
' icon resources
'
Const IMAGE_BITMAP:Int      = 0
Const IMAGE_ICON:Int        = 1
Const IMAGE_CURSOR:Int      = 2
Const IMAGE_ENHMETAFILE:Int = 3

Const LR_DEFAULTSIZE:Int      = 64
Const LR_DEFAULTCOLOR:Int     = 0
Const LR_MONOCHROME:Int       = 1
Const LR_COLOR:Int            = 2
Const LR_COPYRETURNORG:Int    = 4
Const LR_COPYDELETEORG:Int    = 8
Const LR_LOADFROMFILE:Int     = 16
Const LR_LOADTRANSPARENT:Int  = 32
Const LR_LOADREALSIZE:Int     = 128
Const LR_LOADMAP3DCOLORS:Int  = 4096
Const LR_CREATEDIBSECTION:Int = 8192
Const LR_COPYFROMRESOURCE:Int = $4000 ' 0x4000
Const LR_SHARED:Int           = 32768           

Global nid:TNotifyIconData

Extern "WIN32"
    Function LoadImage_:Int( Instance:Int, Name$z, Type_:Int, DesiredX:Int, DesiredY:Int, Load:Int) = "LoadImageA@24"
EndExtern

Function LoadIcon:Int( filename:String, width:Int=16,Height:Int=16, Flags:Int=LR_SHARED)
    Return LoadImage_( 0, filename, IMAGE_ICON, width,Height, LR_LOADFROMFILE| Flags)
EndFunction

?

' Register tray icon
Function RegisterTrayIcon(window:TGadget, toolTip:String, iconFile:String)

?Win32
    nid = New TNotifyIconData
    nid.Size = SizeOf(TNotifyIconData)
    nid.hwnd = QueryGadget(window, QUERY_HWND)
    nid.id = 0
    nid.CallbackMessage = WM_MOUSEMOVE
    nid.Icon = LoadIcon( iconFile)
    nid.Flags = NIF_MESSAGE | NIF_TIP | NIF_ICON
    SetNotifyIconDataTip( nid, toolTip )
    Shell_NotifyIcon( NIM_ADD, nid)
?
End Function   

'Remove tray icon
Function RemoveTrayIcon()    
?Win32
    Shell_NotifyIcon(NIM_DELETE, nid)
?
End Function

Example

SuperStrict

Include "trayappmodule.bmx"

'
' EXAMPLE
'
Local window:TGadget = CreateWindow( "Window", 80,80,640,480)
Local panel:TGadget = CreatePanel( 0,0, 0,0, window, PANEL_ACTIVE)

RegisterTrayIcon(panel, "Click to activate app", "beholder.ico")

Repeat
    WaitEvent()

    Select EventID()
        Case EVENT_MOUSEMOVE
            If EventSource() = panel Then
                Select EventX()
'                   Case WM_MOUSEMOVE       Print "mouse move"
'                   Case WM_LBUTTONDOWN Print "left down"
                    Case WM_LBUTTONUP       ShowGadget(window)'; Shell_NotifyIcon( NIM_DELETE, nid)
                    Case WM_LBUTTONDBLCLK   ShowGadget(window)'; Shell_NotifyIcon( NIM_DELETE, nid)
'                   Case WM_RBUTTONDOWN Print "right down"
'                   Case WM_RBUTTONUP       Print "right up"
'                   Case WM_RBUTTONDBLCLK   Print "right doucle click"
'                   Case WM_MBUTTONDOWN Print "middle down"
'                   Case WM_MBUTTONUP       Print "middle up"
'                   Case WM_MBUTTONDBLCLK   Print "middle double click"
                    Default
'                       Print "unknown " + EventX()
                EndSelect
            EndIf
        Case EVENT_WINDOWCLOSE
            'Shell_NotifyIcon( NIM_ADD, nid)        
            HideGadget(window)
    EndSelect
Forever

' unregister tray icon
RemoveTrayIcon()

End
hardcoal commented:

Cool man. i really love the blitz community.. lots of good people..
thanks for this sharing..

i think blitz has the best programming language ever..
if only mark really wanted it to succeed..

hardcoal commented:

btw you didnt add import max gui..

BlitzCoder commented:

Cheers HC. This is as copied from the code archives and I have always wondered why the old codes does not require it, but running it in recent versions would require the import maxgui on top.. added

Reply To Topic (minimum 10 characters)

Please log in to reply