Here you go, I made this for a server program I was working on a while ago. To use it all you have to do is call the WindowToSystemTray function to add it you window and WindowFromSystemTray to remove it out.
This was made a while ago, and I haven't had time to test it so you may need to tweek it a little.
' Windows only, I probably should make this cross-compatible
?win32
' ---------------------------------------------------------------
' Import external functions
' ---------------------------------------------------------------
Extern "Win32"
Function Shell_NotifyIcon:Int(iMessage:Int,pbNotifyicondata:Byte Ptr) = "Shell_NotifyIconA@8"
Function Shell_LoadImage:Int(iInstance:Int,strName$z,iType:Int,iDesiredX:Int,iDesiredY:Int,iLoad:Int) = "LoadImageA@24"
End Extern
' ---------------------------------------------------------------
' Type: tSystemTrayWindow
' Description: Holds details on a system tray minimized window
' ---------------------------------------------------------------
Type tSystemTrayWindow
' ------------------------------------------------
' Junk to keep track of
' ------------------------------------------------
Field f_hNID:tNotifyIconData
Field f_hPanel:TGadget
Field f_hWindow:TGadget
' ------------------------------------------------
' Method: DoubleClicked
' Description: Returns true if double clicked
' ------------------------------------------------
Method DoubleClicked:Int()
Return (EventSource()=f_hPanel And EventX()=WM_LBUTTONDBLCLK)
End Method
' ------------------------------------------------
' Function: Create
' Description: Creates a new instance of tSystemTrayWindow
' and returns it
' ------------------------------------------------
Function create:tSystemTrayWindow()
Return (New tSystemTrayWindow)
End Function
End Type
' ---------------------------------------------------------------
' Type: tNotifyIconData
' Description: Used to pass data to the windows os about a
' system tray icon
' ---------------------------------------------------------------
Type tNotifyIconData
' ------------------------------------------------
' Neccessary fields
' ------------------------------------------------
Field f_iSize:Int
Field f_iHWND:Int
Field f_iID:Int
Field f_iFlags:Int
Field f_iCallbackMessage:Int
Field f_iIcon:Int ' HICON
Field f_lTip:Long ' array [0..63] of AnsiChar;
Field f_lTip2:Long
Field f_lTip3:Long
Field f_lTip4:Long
Field f_lTip5:Long
Field f_lTip6:Long
Field f_lTip7:Long
Field f_lTip8:Long
' ------------------------------------------------
' Function: Create
' Description: Creates a new instance of tNotifyIconData
' and returns it
' ------------------------------------------------
Function create:tNotifyIconData()
Return (New tNotifyIconData)
End Function
EndType
' ---------------------------------------------------------------
' Type: tSystemTrayManager
' Description: Responsible minimizing windows to the system tray
' This is set as abstract to stop anyone creating
' an instance of it. Everything in this should be
' global.
' ---------------------------------------------------------------
Type tSystemTrayManager Abstract
' ------------------------------------------------
' System tray icon constants
' ------------------------------------------------
Const c_iNIM_ADD:Int = 0
Const c_iNIM_MODIFY:Int = 1
Const c_iNIM_DELETE:Int = 2
Const c_iNIM_SETFOCUS:Int = 3
Const c_iNIM_SETVERSION:Int = 4
Const c_iNIF_MESSAGE:Int = $00000001
Const c_iNIF_ICON:Int = $00000002
Const c_iNIF_TIP:Int = $00000004
Const c_iNIF_STATE:Int = $00000008
Const c_iNIF_INFO:Int = $00000010
Const c_iNIF_GUID:Int = $00000020
Const c_iIMAGE_BITMAP:Int = 0
Const c_iIMAGE_ICON:Int = 1
Const c_iIMAGE_CURSOR:Int = 2
Const c_iIMAGE_ENHMETAFILE:Int = 3
Const c_iLR_DEFAULTSIZE:Int = 64
Const c_iLR_DEFAULTCOLOR:Int = 0
Const c_iLR_MONOCHROME:Int = 1
Const c_iLR_COLOR:Int = 2
Const c_iLR_COPYRETURNORG:Int = 4
Const c_iLR_COPYDELETEORG:Int = 8
Const c_iLR_LOADFROMFILE:Int = 16
Const c_iLR_LOADTRANSPARENT:Int = 32
Const c_iLR_LOADREALSIZE:Int = 128
Const c_iLR_LOADMAP3DCOLORS:Int = 4096
Const c_iLR_CREATEDIBSECTION:Int = 8192
Const c_iLR_COPYFROMRESOURCE:Int = $4000
Const c_iLR_SHARED:Int = 32768
' ------------------------------------------------
' Function: m_LoadIcon
' Description: Loads a gdi icon using the win api
' and returns it
' ------------------------------------------------
Function m_LoadIcon:Int(strFilename:String,iWidth:Int=16,iHeight:Int=16,iFlags:Int=c_iLR_SHARED)
Return Shell_LoadImage(0,strFilename,c_iIMAGE_ICON,iWidth,iHeight,c_iLR_LOADFROMFILE|iFlags)
End Function
' ------------------------------------------------
' Function: m_SetNotifyIconDataTip
' Description: Sets the tooltip of the system
' tray icon
' ------------------------------------------------
Function m_SetNotifyIconDataTip(hNid:tNotifyIconData, strTip:String)
MemClear(Varptr hNid.f_lTip, 64)
If strTip.Length > 0 Then
Local p:Byte Ptr = strTip.ToCString()
If strTip.Length < 64 Then
MemCopy( Varptr hNid.f_lTip, p, strTip.Length)
Else
MemCopy( Varptr hNid.f_lTip, p, 63)
EndIf
MemFree(p)
EndIf
End Function
' ------------------------------------------------
' Function: WindowToSystemTray
' Description: Minimizes a window to the
' system tray
' ------------------------------------------------
Function WindowToSystemTray:tSystemTrayWindow(hWindow:tGadget,strIconFile:String="",strTooltip:String="")
' Hide window
HideGadget(hWindow)
' Create system tray window
Local hWin:tSystemTrayWindow = tSystemTrayWindow.Create()
hWin.f_hWindow = hWindow
hWin.f_hPanel = CreatePanel(0,0,0,0,hWindow,PANEL_ACTIVE)
' Create icon details
hWin.f_hNID = tNotifyIconData.Create()
hWin.f_hNID.f_iSize = SizeOf(tNotifyIconData)
hWin.f_hNID.f_iHWND = QueryGadget(hWin.f_hPanel,QUERY_HWND)
hWin.f_hNID.f_iID = 0
hWin.f_hNID.f_iCallbackMessage = WM_MOUSEMOVE
If FileType(strIconFile) = 1 Then hWin.f_hNID.f_iIcon = m_LoadIcon(strIconFile)
hWin.f_hNID.f_iFlags = c_iNIF_MESSAGE|c_iNIF_TIP|c_iNIF_ICON
' Set tooltip and minimze to tray
m_SetNotifyIconDataTip(hWin.f_hNID,strTooltip)
Shell_NotifyIcon(c_iNIM_ADD,hWin.f_hNID)
' Return window
Return hWin
End Function
' ------------------------------------------------
' Function: WindowFromSystemTray
' Description: Maximizes a window from the
' system tray
' ------------------------------------------------
Function WindowFromSystemTray(hWin:tSystemTrayWindow)
' Show window
ShowGadget(hWin.f_hWindow)
' Free panel
FreeGadget(hWin.f_hPanel)
' Delete icon
Shell_NotifyIcon(c_iNIM_DELETE,hWin.f_hNID)
End Function
End Type
?
|