Code archives/Miscellaneous/Simple system tray module
This code has been declared by its author to be Public Domain code.
Download source code
| |||||
Simple tray module that allows you to place an icon and hook it up to mouse messages. 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. Thanks to grable for the base trayapp code :) Here's an example of hiding app window on pressing close button and showing it when pressing the tray icon. 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 | |||||
' Tray minimize win32 application ?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 |
Comments
| ||
Nice! |
| ||
Nice! Questions: 1. Is there a way to get one of those little boxes with options in it like "Open","Close","Pause"... 2. Is there a way to change th icon and/or tooltip in the middle of the program. |
| ||
This code is a modification of post #1. It was provided by Seb and can ONLY be used with MAXGUIEx! Reference: http://www.blitzbasic.com/Community/posts.php?topic=82310 |
| ||
I made this version of the code, which is updated to work with version 1.36 of MaxGUI (thanks to SebHoll for the fix). It also gives an example of how to add a popup menu to the system tray and other useful details. :3 |
| ||
The last example doesn't do anything, and the one before it won't compile. The first example doesn't do anything when the system tray icon is clicked. |
| ||
It does work for me still, so no updates should have broken it since last time... o.O Though I didn't add any icon to the example if that might cause confusion - so it ends up as an empty box in the system tray that can be right clicked for a pop-up menu and double-clicked to be restored. |
Code Archives Forum