-
This is the code im using for my system tray icon (got from the forum a few days ago)
Code:
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
Private Const WM_MOUSEMOVE = &H200
Private Const WM_LBUTTONDBLCLK = &H203
Private Const WM_LBUTTONDOWN = &H201
Private Const WM_LBUTTONUP = &H202
Private Const WM_RBUTTONDBLCLK = &H206
Private Const WM_RBUTTONDOWN = &H204
Private Const WM_RBUTTONUP = &H205
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
Dim mtIconData As NOTIFYICONDATA
Dim mnLight As Integer
Private Declare Function Shell_NotifyIcon Lib "shell32" Alias "Shell_NotifyIconA" (ByVal dwMessage As Long, pnid As NOTIFYICONDATA) As Long
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Static bBusy As Boolean
If bBusy = False Then
bBusy = True
Select Case CLng(X)
Case WM_NCLBUTTONDBLCLK
frmBrowse.WindowState = vbMaximized
frmBrowse.Visible = True
Case WM_LBUTTONDOWN
If Shell_NotifyIcon(NIM_MODIFY, mtIconData) = 0 Then
MsgBox "Unable to change icon in system tray!"
End If
Case WM_LBUTTONUP
Case WM_RBUTTONDBLCLK
Case WM_RBUTTONDOWN
End Select
bBusy = False
End If
End Sub
The icon appears in the system tray fine but none of the click events work at all.
can anyone help?
-
I got this from RCALLAN and HeSaidJoe (thanks guys):
'API function for adding icon to system tray
Declare Function Shell_NotifyIcon Lib "shell32.dll" Alias _
"Shell_NotifyIconA" (ByVal dwMessage As Long, lpData As _
NOTIFYICONDATA) As Long
Public 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
Public Const NIM_ADD = &H0
Public Const NIM_MODIFY = &H1
Public Const NIM_DELETE = &H2
Public Const NIF_MESSAGE = &H1
Public Const NIF_ICON = &H2
Public Const NIF_TIP = &H4
Public Const NIF_DOALL = NIF_MESSAGE Or NIF_ICON Or NIF_TIP
Public Const WM_MOUSEMOVE = &H200
Public Const WM_LBUTTONDBLCLK = &H203
Public Const WM_LBUTTONDOWN = &H201
Public Const WM_RBUTTONDOWN = &H204
'Add the icon to the system tray
Dim TIC As NOTIFYICONDATA
Dim erg As Variant
TIC.cbSize = Len(TIC)
TIC.hWnd = Picture1(iIndex).hWnd
TIC.uID = 1&
TIC.uFlags = NIF_DOALL
TIC.uCallbackMessage = WM_MOUSEMOVE
TIC.hIcon = Picture1.Picture
erg = Shell_NotifyIcon(NIM_ADD, TIC)
'Remove the icon when you close down...
Dim erg As Variant
Dim TIC As NOTIFYICONDATA
TIC.cbSize = Len(TIC)
TIC.hWnd = Picture1.hWnd
TIC.uID = 1&
erg = Shell_NotifyIcon(NIM_DELETE, TIC)
'This function shows a popup menu when the user
'clicks over the tray icon:
Private Sub Picture1_MouseMove(Index As Integer, Button As _ Integer, Shift As Integer, X As Single, Y As Single)
Select Case ScaleX(X, vbTwips, vbPixels)
Case WM_LBUTTONDBLCLK
'Do something here if you wish
Case WM_RBUTTONDOWN
'Usually better to do it with a right click
PopupMenu mnuPopup
End Select
End Sub
-
-
Errors
Rod-
When I put that code in a module it gave me errors about TIC and I was wondering how would you install the code?
-
Try putting this code in the form load of your first form
'the form must be fully visible before calling Shell_NotifyIcon
Me.Show
Me.Refresh
With nid
.cbSize = Len(nid)
.hwnd = Me.hwnd
.uId = vbNull
.uFlags = NIF_ICON Or NIF_TIP Or NIF_MESSAGE
.uCallBackMessage = WM_MOUSEMOVE
.hIcon = Me.Icon
.szTip = "Send a Message" & vbNullChar
End With
Shell_NotifyIcon NIM_ADD, nid
As well as the following code
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
'this procedure receives the callbacks from the System Tray icon.
Dim Result As Long
Dim msg As Long
'the value of X will vary depending upon the scalemode setting
If Me.ScaleMode = vbPixels Then
msg = X
Else
msg = X / Screen.TwipsPerPixelX
End If
Select Case msg
Case WM_LBUTTONUP '514 restore form window
Me.WindowState = vbNormal
Result = SetForegroundWindow(Me.hwnd)
Me.Show
Case WM_LBUTTONDBLCLK '515 restore form window
Me.WindowState = vbNormal
Result = SetForegroundWindow(Me.hwnd)
Me.Show
Case WM_RBUTTONUP '517 display popup menu
Result = SetForegroundWindow(Me.hwnd)
'Me.PopupMenu Me.mPopupSys
End Select
End Sub
Private Sub Form_Resize()
'this is necessary to assure that the minimized window is hidden
If Me.WindowState = vbMinimized Then Me.Hide
End Sub
Private Sub Form_Unload(Cancel As Integer)
'this removes the icon from the system tray
Shell_NotifyIcon NIM_DELETE, nid
End Sub