ID: Q149276
The information in this article applies to:
The Windows API provides the ability to add, modify, and remove icons from the system tray available in the Windows 95 and Windows 98 shell. This functionality can be provided using only the Shell_NotifyIcon function that is exported by Shell32.dll. This API function also provides the ability to specify a text string for the ToolTip that is displayed when a user pauses with the mouse pointer over the icon. The step-by-step example in this article creates a Visual Basic program which demonstrates how to use this API function.
The ability to take some action if the icon in the systray is clicked depends on a callback function. Because Visual Basic does not support callback functions directly, there is no way to show a form or execute some code using solely Visual Basic. A number of options are available that can provide callback functionality, including the Message Blaster OCX, the OCX mentioned in the MSJ article referenced below, or the Callback OLE server detailed in Bruce McKinney's book, "Hardcore Visual Basic," published by Microsoft Press. For more information on any of these options, see the REFERENCES section at the end of this article. Because these products do not ship with Visual Basic, Microsoft Technical Support does not support their use.
The following example creates a one-form Visual Basic project that shows how to use the Shell_NotifyIcon API function.
1. Start Visual Basic. Form1 is created by default.
2. Change the form's icon property to the icon that should be shown in the
systray.
3. Draw three CommandButtons onto the form.
4. Select Module from the Insert menu to add a single code module to the
project.
5. Add the following code, consisting of function, type, and constant
declarations, to Module1:
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
Global Const NIM_ADD = 0
Global Const NIM_MODIFY = 1
Global Const NIM_DELETE = 2
Global Const NIF_MESSAGE = 1
Global Const NIF_ICON = 2
Global Const NIF_TIP = 4
Declare Function Shell_NotifyIconA Lib "SHELL32" _
(ByVal dwMessage As Long, lpData As NOTIFYICONDATA) As Integer
5. The following code is a function that takes the parameters that need to
be set for the NOTIFYICONDATA type and returns a variable of this type.
Add to Form1:
Private Function setNOTIFYICONDATA(hWnd As Long, ID As Long, _
Flags As Long, CallbackMessage As Long, Icon As Long, _
Tip As String) As NOTIFYICONDATA
Dim nidTemp As NOTIFYICONDATA
nidTemp.cbSize = Len(nidTemp)
nidTemp.hWnd = hWnd
nidTemp.uID = ID
nidTemp.uFlags = Flags
nidTemp.uCallbackMessage = CallbackMessage
nidTemp.hIcon = Icon
nidTemp.szTip = Tip & Chr$(0)
setNOTIFYICONDATA = nidTemp
End Function
6. The three procedures in this block of code call the function created in
step 5 to add, modify, and remove systray icons. Add this code to Form1
also:
Private Sub Command1_Click()
'Add an icon. This procedure uses the icon specified in
'the Icon property of Form1. This can be modified as desired.
Dim i As Integer
Dim s As String
Dim nid As NOTIFYICONDATA
s = InputBox("Enter string:")
nid = setNOTIFYICONDATA(hWnd:=Form1.hWnd, _
ID:=vbNull, _
Flags:=NIF_MESSAGE Or NIF_ICON _
Or NIF_TIP, _
CallbackMessage:=vbNull, _
Icon:=Form1.Icon, _
Tip:=s)
i = Shell_NotifyIconA(NIM_ADD, nid)
End Sub
Private Sub Command2_Click()
'Modify an existing icon. This procedure uses the icon
'specified in the Icon property of Form1. This can be modified
'as desired.
Dim i As Integer
Dim s As String
Dim nid As NOTIFYICONDATA
s = InputBox("Enter string:")
nid = setNOTIFYICONDATA(hWnd:=Form1.hWnd, _
ID:=vbNull, _
Flags:=NIF_MESSAGE Or NIF_ICON _
Or NIF_TIP, _
CallbackMessage:=vbNull, _
Icon:=Form1.Icon, _
Tip:=s)
i = Shell_NotifyIconA(NIM_MODIFY, nid)
End Sub
Private Sub Command3_Click()
'Delete an existing icon.
Dim i As Integer
Dim nid As NOTIFYICONDATA
nid = setNOTIFYICONDATA(hWnd:=Form1.hWnd, _
ID:=vbNull, _
Flags:=NIF_MESSAGE Or NIF_ICON _
Or NIF_TIP, _
CallbackMessage:=vbNull, _
Icon:=Form1.Icon, _
Tip:="")
i = Shell_NotifyIconA(NIM_DELETE, nid)
End Sub
7. Press the F5 key or select Start from the Run menu to run the
application. Click the first button and enter a text string to add an
icon. Click the second button to modify an existing icon, and the third
to delete the icon.
Hardcore Visual Basic, Bruce McKinney, Microsoft Press 1995. Microsoft Win32 SDK, Shell_NotifyIcon and NOTIFYICONDATA. Microsoft Systems Journal, February 1996, The Visual Programmer, page 93. Visual Basic Programmer's Journal, March 1996, Q&A, page 136.
Additional query words: kbVBp kbVBp400 kbAPI kbDSupport kbdsd
Keywords : kbVBp400
Version : 4.0
Platform : NT WINDOWS
Issue type : kbhowto
Last Reviewed: November 11, 1998