Monday, May 30, 2005

vbcity.com: icon in system tray

vbCity/DevCity.NET Forums :: FAQ :: Visual Basic :: VB General :: Windows:


Option Explicit

'Declare a user-defined variable
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

'Declare constants for the API function.
Private Const NIM_ADD = &H0 ' Add an Icon
Private Const NIM_MODIFY = &H1 ' Edit/Change an Icon
Private Const NIM_DELETE = &H2 ' Remove Icon from Tray

'Declare constant for mouse event
Private Const WM_MOUSEMOVE = &H200

'Flags that indicate the members of NOTIFYICONDATA.
Private Const NIF_MESSAGE = &H1
Private Const NIF_ICON = &H2
Private Const NIF_TIP = &H4

' Constants for the mouse clicks on the icon
' These allow you to add actions based on these
' events being raised.
'Left-click
Private Const WM_LBUTTONDBLCLK = &H203 'Double-click
Private Const WM_LBUTTONDOWN = &H201 'Button down

'Right-click
Private Const WM_RBUTTONDBLCLK = &H206 'Double-click
Private Const WM_RBUTTONDOWN = &H204 'Button down

'Declare the API function
Private Declare Function Shell_NotifyIcon Lib "shell32" Alias "Shell_NotifyIconA" (ByVal dwMessage As Long, pnid As NOTIFYICONDATA) As Boolean

'Declare a variable as the UDT NOTIFYICONDATA
Dim nid As NOTIFYICONDATA

Private Sub Form_Load()

'Set the values for the NOTIFYICONDATA data type.
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 ' <== You can change this to another icon
.szTip = "SysTray ToolTip Goes Here" & vbNullChar ' <== You can change this also.
End With
End Sub

Private Sub Form_Terminate()
'Remove the icon from the taskbar when your program ends.
Shell_NotifyIcon NIM_DELETE, nid
End Sub

Now all you have to decide is what event will be fired in order for the icon to appear in the system tray. There are various scenarios. Here are just a few:

1. You can do it via the click of a CommandButton. Paste this code into your Form:

Code:
Private Sub Command1_Click()
Shell_NotifyIcon NIM_ADD, nid
End Sub

2. Or – more likely the way you might want it to work – you can display the icon whenever the form is minimized. Paste this code into your form as well as, or instead of, the above:

Code:
Private Sub Form_Resize()
' If form minimized then show the icon in systray

If Me.WindowState = 1 Then
Shell_NotifyIcon NIM_ADD, nid
Else ' otherwise don't show it
Shell_NotifyIcon NIM_DELETE, nid ' ç This removes the icon
End If

End Sub

3. Sometimes you will want to hide your app from the user and put the icon in the system tray for when they want to access the app again. Paste this code into your form, run the project and click on the CommandButton(Command1). As you will see, it does just that.

Code:
Private Sub Command1_Click()
' Hide form
Me.Hide
' Show icon
Shell_NotifyIcon NIM_ADD, nid
End Sub

Once the icon is in your systray, you probably need it to do something when the user clicks it. There are loads of things you can do, but for this FAQ we will just stick with restoring the application to normal size when the user double clicks the icon with the left mouse button. This code will work both when your app is minimized and also if you have hidden the form.

Paste this code into your form:

Code:
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, _
X As Single, Y As Single)
Dim lngButtonAction As Long

lngButtonAction = X / Screen.TwipsPerPixelX

Select Case lngButtonAction

Case WM_LBUTTONDBLCLK
' Left mouse button has been double clicked
' If app is currently minimized ….
If Me.WindowState = 1 Then ' … then restore it to normal size
Me.WindowState = 0
Shell_NotifyIcon NIM_DELETE, nid ' and remove the icon
ElseIf Me.Visible = False Then ' Else, if the app is hidden …
Me.Show ' then show the form again
Shell_NotifyIcon NIM_DELETE, nid ' and remove the icon
End If

Case WM_LBUTTONDOWN
' You could put code in here to make something happen when
' left mouse button is single clicked on the icon

Case WM_RBUTTONDOWN
' You could put code in here to make something happen when
' right mouse button is single clicked on the icon

End Select

End Sub

0 Comments:

Post a Comment

<< Home