Display your program in the system tray by the clock using APIs

Level:
Level4

Many applications choose to display a small icon in the taskbar notification area. This is the area beside the clock - a lot of times you will have icons there for changing the volume or your antivirus program will many times be in this area. Visual Basic 6 does not have an easy way for us to put our application in the taskbar notification area. Infact many companies have created Active X controls to make this easier. However, instead of spending money on one of these controls, this VB6 tutorial will show you how you can do this using just Windows API calls.

You can download the sample code for this tutorial and follow along.

First, start Visual Basic 6 and create a new project (Standard EXE). Next add these controls to Form1

Control Type: CommandButton
Name: cmdAdd
Caption: Add the Icon

Control Type: CommandButton
Name: cmdModify
Caption: Modify the Icon

Control Type: CommandButton
Name: cmdDelete
Caption: Delete the Icon

Control Type: TextBox
Name: txtTip
Text: Hello, from the tray

When you are done your form should look like this in the Visual Basic 6 design window.

Visual Basic Tutorial Screen 1

Now in Form1's code area lets declare the API functions, types, and constants we will need.

Option Explicit
   
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 = 0
Private Const NIM_MODIFY = 1
Private Const NIM_DELETE = 2
Private Const NIF_MESSAGE = 1
Private Const NIF_ICON = 2
Private Const NIF_TIP = 4
   
Private Declare Function Shell_NotifyIconA Lib "SHELL32" _
   (ByVal dwMessage As Long, lpData As NOTIFYICONDATA) As Integer
Next lets create a helper function. We will call this function anytime we want to do anything with the tray icon. This function was a function Microsoft recommends in their Visual Basic 6 Knowledge Base article. It makes our life a lot easier, because many settings are the same everytime we call the Shell_NotifyIconA API. This function creates the variable that we will pass to this API.
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
Now lets write the code to put our Form1 icon in the system tray. We will have this run when we click on the cmdStart button.

Private Sub cmdAdd_Click()
   ' This will add the Form1 icon to the system tray
   
   Dim retVal As Integer
   Dim nid As NOTIFYICONDATA
   
   ' This creates our NotifyIconData variable
   nid = setNOTIFYICONDATA( _
      Form1.hWnd, vbNull, _
      NIF_MESSAGE Or NIF_ICON Or NIF_TIP, _
      vbNull, Form1.Icon, txtTip)
   
   ' Now we call our API with this variable
   retVal = Shell_NotifyIconA(NIM_ADD, nid)
End Sub
If you run this Visual Basic 6 program now and click on the Add the Icon button you will see Form1's icon appears in the taskbar notification area. Also if you hold your mouse pointer over the icon you will see a tooltip pop up that displays the text in txtTip. Now lets see how easy it is to modify this text by adding the code to our cmdModify button.

Private Sub cmdModify_Click()
   ' This will modify an existing tray icon
   Dim retVal As Integer
   Dim nid As NOTIFYICONDATA
   
   nid = setNOTIFYICONDATA( _
      Form1.hWnd, vbNull, _
      NIF_MESSAGE Or NIF_ICON Or NIF_TIP, _
      vbNull, Form1.Icon, txtTip)
   
   retVal = Shell_NotifyIconA(NIM_MODIFY, nid)
End Sub
If you notice this code is almost identical to our code to add the icon. However, now we are calling the Shell_NotifyIconA function with the first parameter set to NIM_MODIFY instead of NIM_ADD. If you run the program again you can click the Add button to have your icon appear in the tray. If you mouse over the icon you will see the usually text displayed. Now enter some new text in txtTip and press the Modify Icon button. If you mouse over the icon again you will now see the new text appear. Also if in your code you had assigned a new icon to Form1 you would also see that new icon appear. This allows you to display different icons and text to your user based on what your application is currently doing. For example many times you will see an antivirus program display a different icon while it is checking for viruses compared to when it is just sitting idle.

Lastly lets wire up our cmdDelete button so that it will remove the icon from the tray.
Private Sub cmdDelete_Click()
   ' This will delete an existing tray icon
   Dim retVal As Integer
   Dim nid As NOTIFYICONDATA
   
   nid = setNOTIFYICONDATA( _
      Form1.hWnd, vbNull, _
      NIF_MESSAGE Or NIF_ICON Or NIF_TIP, _
      vbNull, Form1.Icon, txtTip)
      
   retVal = Shell_NotifyIconA(NIM_DELETE, nid)
End Sub

Again this code should look very familiar all we are doing differently is passing the NIM_DELETE command to Shell_NotifyIconA. If you again run your program you should now be able to add the icon to the system tray then delete it away. This is similar to what many email programs do. They show an icon when you have email and then delete the icon once you read the new mail. Now that you have completed this sample Visual Basic tutorial, I encourage you to wrap this in an easy to use class or module so that whenever you have a program that in VB6 that needs to display an icon in the system tray it can do so.

Display VB program in system tray screen shot

 

If you enjoyed this post, subscribe for updates (it's free)

Minimize To Systray With Menu

thanks
its nice and work 100 %
but ...
how i can add Menu when i click on system tray icon ???

thanks! it worked.

thanks! it worked.

Thank you

Thank you very much!!!!!!!!!!!!!

Windows 7 Notifications Don't Update

The notification box when you hover the mouse over the icon in the system tray always shows the first notification when run with Windows 7 (subsequent notifcations do not replace the first one). However the same vb6 program running of XP correctly shows the subsequent notifications (they replace the prior ones).

If you know what is causing this or can point me in the right direction, I would very much appreciate it!

Thank You!

How to add icons?

H> Hi, Can you please assist me, how to add an icon to the application. Right now I am getting a VB default application icon. I tried some methods, but its not working :(
H

how do we add context menu

how do we add context menu

REQUEST

please send it as proper vb file

Option Explicit

Option Explicit

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 = 0
Private Const NIM_MODIFY = 1
Private Const NIM_DELETE = 2
Private Const NIF_MESSAGE = 1
Private Const NIF_ICON = 2
Private Const NIF_TIP = 4

Private Declare Function Shell_NotifyIconA Lib "SHELL32" (ByVal dwMessage As Long, lpData As NOTIFYICONDATA) As Integer

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
Private Sub cmdAdd_Click()
' This will add the Form1 icon to the system tray
Dim retVal As Integer
Dim nid As NOTIFYICONDATA
' This creates our NotifyIconData variable
nid = setNOTIFYICONDATA(Form1.hWnd, vbNull, NIF_MESSAGE Or NIF_ICON Or NIF_TIP, vbNull, Form1.Icon, Text1.Text)

' Now we call our API with this variable
retVal = Shell_NotifyIconA(NIM_ADD, nid)
End Sub

Private Sub cmdModify_Click()
' This will modify an existing tray icon
Dim retVal As Integer
Dim nid As NOTIFYICONDATA
nid = setNOTIFYICONDATA(Form1.hWnd, vbNull, NIF_MESSAGE Or NIF_ICON Or NIF_TIP, vbNull, Form1.Icon, Text1.Text)
retVal = Shell_NotifyIconA(NIM_MODIFY, nid)

End Sub

Private Sub cmdDelete_Click()
' This will delete an existing tray icon
Dim retVal As Integer
Dim nid As NOTIFYICONDATA

nid = setNOTIFYICONDATA(Form1.hWnd, vbNull, NIF_MESSAGE Or NIF_ICON Or NIF_TIP, vbNull, Form1.Icon, Text1.Text)

retVal = Shell_NotifyIconA(NIM_DELETE, nid)
End Sub

Great Piece! How about

Great Piece! How about adding a context menu?

Class Module

How would this go into a module?
PLEASE HELP!

Great code, thank you

Great code, thank you

Nice!

Nice done!

Works 100% :=)

deleting icon

Deleting the icon only works if the text in the delete command is exactly the same as the mouseover of the icon.

Atm i'm finding out how to make it work with a menu.

Can you stop it from

Can you stop it from dissipearing , though? I am trying to create an icon for my Operating System.

Help!

The icon keeps dissipearing!

Exiting the application

Exiting the application leaves the icon behind until it gets a mouseover. I can fix that in a second.

Deleting the icon is not working, though. That is going to take longer to figure out.

Can you fix it for me?

Can you fix it for me?