This article was previously published under Q149276
This article has been archived. It is offered "as is" and will no longer be updated.
The Windows API provides the ability to add, modify, and remove icons fromthe System Tray, or Taskbar Notification Area, available in the Windows 95, Windows 98, Windows Me, Windows NT 4.0, and Windows 2000 shell. Thisfunctionality can be provided using only the Shell_NotifyIcon function thatis exported by Shell32.dll. This API function also provides the ability tospecify a text string for the ToolTip that is displayed when a user pauseswith the mouse pointer over the icon. The step-by-step example in thisarticle creates a Visual Basic program which demonstrates how to use thisAPI function.
The ability to take some action if the icon in the Taskbar Notification Area is clicked depends on a callback function. Because Visual Basic 4.0 does not support callback functions directly, there is no way to show a form or execute some code using solely Visual Basic 4.0. This functionality was added in Visual Basic 5.0 (and later) with the addition of the AddressOf operator. A number of options are available to Visual Basic 4.0 developers that can provide callback functionality, including the Message Blaster OCX, the OCX mentioned in the Microsoft Systems Journal article noted in the "References" section of this article, or the Callback OLE server that is detailed in Bruce McKinney's book, Hardcore Visual Basic. For more information on any of these options, see the "References" section. Because these products do not include Visual Basic, Microsoft Product Support Services does not support their use.
The following example creates a one-form Visual Basic project that showshow to use the Shell_NotifyIcon API function.
Start Visual Basic. Form1 is created by default.
Change the form's icon property to the icon that should be shown in the Taskbar Notification Area.
Draw three CommandButtons onto the form.
Select Module from the Insert menu to add a single code module to the project.
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
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
The three procedures in this block of code call the function created in step 5 to add, modify, and remove Taskbar Notification Area 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
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.
For additional information, click the article number below to view the article in the Microsoft Knowledge Base:
162613 How To Manipulate Icons in the System Tray with Visual Basic