home *** CD-ROM | disk | FTP | other *** search
Wrap
Visual Basic class definition | 1998-09-16 | 8.5 KB | 298 lines
VERSION 1.0 CLASS BEGIN MultiUse = -1 'True END Attribute VB_Name = "TrayIcon" Attribute VB_GlobalNameSpace = False Attribute VB_Creatable = True Attribute VB_PredeclaredId = False Attribute VB_Exposed = True '-------------------------------------------------------------------------- ' This file is part of the SG Window ' ' Class TrayIcon shows how to use SGWindow to add VB program ' to the system tray. ' ' USAGE: ' ' Copyright ⌐ 1998 Stinga ' All rights reserved ' ' You have a right to use and modify this code. However, Stinga ' takes no responsibility for what you may do with this or any ' modification of this code. '-------------------------------------------------------------------------- Option Explicit ' API declarations Private Const NIM_ADD = &H0 Private Const NIM_DELETE = &H2 Private Const NIM_MODIFY = &H1 Private Const NIF_MESSAGE = &H1 Private Const NIF_ICON = &H2 Private Const NIF_TIP = &H4 Private Type NOTIFYICONDATA cbSize As Long hWnd As Long uID As Long uFlags As Long uCallbackMessage As Long hIcon As Long szTip As String * 64 End Type Private Type POINTAPI x As Long y As Long End Type Private Type RECT Left As Long Top As Long Right As Long Bottom As Long End Type Private Declare Function Shell_NotifyIcon Lib "shell32.dll" Alias "Shell_NotifyIconA" (ByVal dwMessage As Long, lpData As NOTIFYICONDATA) As Long Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long Private Declare Function SetForegroundWindow Lib "user32" (ByVal hWnd As Long) As Long Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long Private Declare Function TrackPopupMenu Lib "user32" (ByVal hMenu As Long, ByVal wFlags As Long, ByVal x As Long, ByVal y As Long, ByVal nReserved As Long, ByVal hWnd As Long, lprc As RECT) As Long ' Message callback interface Implements sgWindow.IsgMessageSink ' Private constants and variables Private Const CallbackMessage = &H8001& Private mWnd As sgWindow.Window ' SG Window object Private mNID As NOTIFYICONDATA ' Comunication structure Private mIcon As Object ' Tray icon image Private mbAdded As Boolean ' True if icon is on the system tray Private mbEnabled As Boolean ' True if tray icon fires events in responds to mouse messages Private mFrm As Object ' Associated form Private mPopupMenu As Object ' Popup menu Private mbRaisePopupMenuEvent As Boolean ' True if popup menu event should be fired ' Event declaration Event PopupMenu(ByVal x As Single, ByVal y As Single) Event MouseMove(ByVal x As Single, ByVal y As Single) Event Click(ByVal x As Single, ByVal y As Single) Event DblClick(ByVal x As Single, ByVal y As Single) Event LButtonDown(ByVal x As Single, ByVal y As Single) Event LButtonUp(ByVal x As Single, ByVal y As Single) Event MButtonDown(ByVal x As Single, ByVal y As Single) Event MButtonUp(ByVal x As Single, ByVal y As Single) Event RButtonDown(ByVal x As Single, ByVal y As Single) Event RButtonUp(ByVal x As Single, ByVal y As Single) '-------------------------------------------------------------------------- ' Class interface methods '-------------------------------------------------------------------------- Public Property Set Form(frm As Object) Set mFrm = frm End Property Public Property Get Enabled() As Boolean Enabled = mbEnabled End Property Public Property Let Enabled(val As Boolean) mbEnabled = val End Property Public Property Set PopupMenu(mnu As Object) Set mPopupMenu = mnu End Property Public Property Get Tip() As String Tip = mNID.szTip End Property Public Property Let Tip(sTip As String) mNID.szTip = IIf(Len(sTip) > 63, Left(sTip, 63), sTip) & vbNullChar Modify NIF_TIP End Property Public Property Get Icon() As Object Set Icon = mIcon End Property Public Property Set Icon(ic As Object) Set mIcon = ic mNID.hIcon = mIcon.Handle Modify NIF_ICON End Property Public Sub Add() If Not mbAdded Then mNID.uFlags = NIF_TIP + NIF_MESSAGE ' Do we have icon image If mIcon.Handle <> 0 And mIcon.Type = vbPicTypeIcon Then mNID.hIcon = mIcon.Handle mNID.uFlags = mNID.uFlags + NIF_ICON End If ' Add icon to the system tray Dim rc As Long rc = Shell_NotifyIcon(NIM_ADD, mNID) If rc <> 0 Then ' OK mbAdded = True Else ' Icon was not added mbAdded = False Err.Raise vbError + 1, "SGTrayIcon", "Failed to add icon to the system try" End If End If End Sub Public Sub Remove() If mbAdded Then Shell_NotifyIcon NIM_DELETE, mNID mbAdded = False End If End Sub '-------------------------------------------------------------------------- ' Class implementation methods '-------------------------------------------------------------------------- Private Sub Modify(what As Long) Dim rc& mNID.uFlags = what rc = Shell_NotifyIcon(NIM_MODIFY, mNID) If rc = 0 Then End If End Sub Private Sub Class_Initialize() mbAdded = False mbRaisePopupMenuEvent = True mbEnabled = True ' Create SG Window object Set mWnd = New sgWindow.Window ' Create hidden window that will receive tray icon messages mWnd.Create "STATIC", "SG Window TrayIcon Helper", 0, 0, _ 10, 10, 100, 20, 0, 0 ' Hook callback message mWnd.EnableMessage CallbackMessage, True mWnd.SetMessageCallback Me mWnd.Hooked = True ' Initialize tray icon structure mNID.cbSize = Len(mNID) mNID.hWnd = mWnd.hWnd mNID.uID = 1 mNID.uFlags = 0 mNID.uCallbackMessage = CallbackMessage mNID.hIcon = 0 mNID.szTip = "" End Sub Private Sub Class_Terminate() ' Remove icon from the system try Remove ' Destroy hidden window mWnd.Hooked = False mWnd.Destroy Set mFrm = Nothing End Sub '-------------------------------------------------------------------------- ' System tray message handler '-------------------------------------------------------------------------- Private Sub IsgMessageSink_Message(ByVal msg As Long, ByVal wParam As Long, ByVal lParam As Long, result As Long) ' ' This is place where we catch messages sent by system tray. ' ' wParam = icon identifier. We have only one icon per window, ' so we do not need this information. ' lParam = mouse message code ' If Not mbEnabled Then result = mWnd.CallWindowProc(msg, wParam, lParam) Exit Sub End If Dim pt As POINTAPI Call GetCursorPos(pt) Select Case lParam Case wm_MOUSEMOVE ' Mouse Move event RaiseEvent MouseMove(pt.x, pt.y) Case wm_LBUTTONDBLCLK ' Double click event RaiseEvent DblClick(pt.x, pt.y) Case wm_LBUTTONDOWN ' Button down event RaiseEvent LButtonDown(pt.x, pt.y) Case wm_LBUTTONUP ' Button up event RaiseEvent LButtonUp(pt.x, pt.y) RaiseEvent Click(pt.x, pt.y) Case wm_MBUTTONDBLCLK ' Double click event RaiseEvent DblClick(pt.x, pt.y) Case wm_MBUTTONDOWN ' Button down event RaiseEvent MButtonDown(pt.x, pt.y) Case wm_MBUTTONUP ' Button up event RaiseEvent MButtonUp(pt.x, pt.y) RaiseEvent Click(pt.x, pt.y) Case wm_RBUTTONDBLCLK ' Double click event RaiseEvent DblClick(pt.x, pt.y) Case wm_RBUTTONDOWN ' Button down event RaiseEvent RButtonDown(pt.x, pt.y) ' Pop up menu event Dim hWnd& hWnd = 0 If Not mFrm Is Nothing Then hWnd = mFrm.hWnd Call SetForegroundWindow(hWnd) End If If mPopupMenu Is Nothing Then RaiseEvent PopupMenu(pt.x, pt.y) ElseIf Not mFrm Is Nothing Then mFrm.PopupMenu mPopupMenu End If If hWnd <> 0 Then _ Call PostMessage(hWnd, wm_NULL, 0, 0) Case wm_RBUTTONUP ' Button up event RaiseEvent RButtonUp(pt.x, pt.y) RaiseEvent Click(pt.x, pt.y) Case Else ' Default processing result = mWnd.CallWindowProc(msg, wParam, lParam) End Select End Sub