Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

System Tray Icon (code included)

Can someone please check this out and let me know where im going wrong, Thanks!!

##########################
CODE SNIPPETS#####
##########################
******************
Class Module (class1)
*********************
Option Explicit
Public Event TrayIconLeftCLick()
Public Event TrayIconRightCLick()
Public Event TrayIconMove()
Public Event TrayIconDoubleCLick()
Private WithEvents pct As PictureBox
Private Declare Function Shell_NotifyIcon Lib "shell32.dll" Alias _
"Shell_NotifyIconA" (ByVal dwMessage As Long, lpData As _
NOTIFYICONDATA) As Long
Private Declare Function FindWindow Lib "user32" _
Alias "FindWindowA" (ByVal lpClassName As String, _
ByVal lpWindowName As String) As Long

Private Declare Function SendMessage Lib "user32" Alias _
"SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, _
ByVal wParam As Long, lParam As Any) As Long

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 Const NIM_ADD = &H0
Private Const NIM_MODIFY = &H1
Private Const NIM_DELETE = &H2
Private Const NIF_MESSAGE = &H1
Private Const NIF_ICON = &H2
Private Const NIF_TIP = &H4 'Make your own constant, e.g.:
Private Const NIF_DOALL = NIF_MESSAGE Or NIF_ICON Or NIF_TIP
Private Const WM_MOUSEMOVE = &H200
Private Const WM_LBUTTONDBLCLK = &H203
Private Const WM_LBUTTONDOWN = &H201
Private Const WM_RBUTTONDOWN = &H204
Public Sub CreateIcon(Optional Tip As String = "")
Dim Tic As NOTIFYICONDATA
If pct Is Nothing Then Exit Sub

Tic.cbSize = Len(Tic)
Tic.hWnd = pct.hWnd
Tic.uID = 1&
Tic.uFlags = NIF_DOALL
Tic.uCallbackMessage = WM_MOUSEMOVE
Tic.hIcon = pct.Picture
Tic.szTip = Tip & Chr$(0)
Shell_NotifyIcon NIM_ADD, Tic
End Sub
Public Sub DeleteIcon()
Dim Tic As NOTIFYICONDATA
If pct Is Nothing Then Exit Sub

Tic.cbSize = Len(Tic)
Tic.hWnd = pct.hWnd
Tic.uID = 1&
Shell_NotifyIcon NIM_DELETE, Tic
End Sub
Public Sub ModifyIcon(Optional Tip As String = "")
Dim Tic As NOTIFYICONDATA

Tic.cbSize = Len(Tic)
Tic.hWnd = pct.hWnd
Tic.uID = 1&
Tic.uFlags = NIF_DOALL
Tic.uCallbackMessage = WM_MOUSEMOVE
Tic.hIcon = pct.Picture
Tic.szTip = Tip & Chr$(0)
Shell_NotifyIcon NIM_MODIFY, Tic
End Sub
Public Sub SetPct(nPct As PictureBox)
Set pct = nPct
End Sub
Private Sub pct_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
X = X / Screen.TwipsPerPixelX
Select Case X
Case WM_LBUTTONDOWN
'Caption = "Left Click"
RaiseEvent TrayIconLeftCLick
Case WM_RBUTTONDOWN
'Caption = "Right Click"
RaiseEvent TrayIconRightCLick
Case WM_MOUSEMOVE
'Caption = "Move"
RaiseEvent TrayIconMove
Case WM_LBUTTONDBLCLK
'Caption = "Double Click"
RaiseEvent TrayIconDoubleCLick
End Select
End Sub

*****************************************
Form1
*******************************************
Option Explicit

Private Sub Form_Load()
Dim WithEvents TI As cTrayIcon
Label1.Caption = "Current Date/Time:" & Date
Set TI = New cTrayIcon
TI.SetPct Picture1
TI.CreateIcon
TI.ModifyIcon
End Sub

Private Sub Form_Unload(Cancel As Integer)
TI.DeleteIcon
Set TI = Nothing
frmLogin.Show
Me.Hide
End Sub

Private Sub Timer1_Timer()
Label2.Caption = Time
Label2.Refresh
End Sub
**************************
##########################

Thanks in advance!
[4136 byte] By [vchatlive] at [2007-11-11 10:03:54]
# 1 Re: System Tray Icon (code included)
-if the class name is class1 then what is this : "Dim WithEvents TI As cTrayIcon"

U must make the class name : "cTrayIcon"

-puting this : "Dim WithEvents TI As cTrayIcon" in a sub will not make it work for the whole app as global or public , u must put it in the declaraions section .
Amahdy at 2007-11-11 17:23:24 >
# 2 Re: System Tray Icon (code included)
Up and runnin no problems!! Thanks
vchatlive at 2007-11-11 17:24:24 >
# 3 Re: System Tray Icon (code included)
ok now i have this icon working it shows up blah blah blah, now how do i make it when its clicked on show a menu? i already used the menu editor and created a menu called systray and made it non visible, i want it to show up when i click on the tray icon, or right click... Thanks!
vchatlive at 2007-11-11 17:25:22 >
# 4 Re: System Tray Icon (code included)
I have not done much with the system tray; but I found this sample Project in FreeVBCode.com
http://www.freevbcode.com/ShowCode.Asp?ID=2055
Ron Weller at 2007-11-11 17:26:17 >
# 5 Re: System Tray Icon (code included)
tried using info out of that zip, didnt seem to work, all i need it to do is when its clicked on, popup a menu called systray. Thanks
vchatlive at 2007-11-11 17:27:23 >
# 6 Re: System Tray Icon (code included)
ok in case anyone ever needs info on this.....

im using a classmodule for the system tray icon, it shows up fine. in the class module, i had to setup the popupmenu systray, but since that menu was located on the main form (frmMain for the example) it has to be frmMain.popupmenu frmmain.systray everything works great! Thanks guys and gals!
vchatlive at 2007-11-11 17:28:22 >