Task Tray

Learn how to put an application in the Task Tray using Visual Basic 6 with this Tutorial that Celebrates the CESPage 25th Anniversary

This will show you how to put an Icon in the Task Tray and make a Popup Menu appear when you Right Click on the Icon within the Task Tray, this Tutorial is presenting using the original Images and Code.

Step 1

Load Microsoft Visual Basic, then select Standard EXE Project if VB5/6, click on the option then click Open
New Project

Step 2

A Blank Form named Form1 should then appear
Blank Form

Step 3

Then from the Visual Basic Components Menu Select the Picture Box Control:
Picture Box Control

Step 4

Draw a Picture Box on the Form
Form with Picture Box

Step 5

Then goto the Properties Box and change the Name property to picHook
Picture Box Name Property

Step 6

Click on the Form of Form1 then got the Properties Box and change the Visible property to False and the ShowInTaskBar property to False
Form 1 Properties

Step 7

Create a Menu set on the Form by clicking on the Menu Editor Button on the toolbar
Menu Editor Toolbar Button
In the Menu Editor Dialog type in Popup in the Caption box and mnuPopup in the Name box, Click on Next then Click on the Arrow pointing Right and type in the Caption box Exit then in the Name box mnuPopupExit
Menu Editor

Step 8

Once the Menu is complete you must now do the coding. Double Click on Form1 then select the General Declarations from the Left Combo Box then type in the following:


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 = &H0
Private Const NIM_MODIFY = &H1
Private Const NIM_DELETE = &H2
Private Const WM_MOUSEMOVE = &H200
Private Const NIF_MESSAGE = &H1
Private Const NIF_ICON = &H2
Private Const NIF_TIP = &H4

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 Declare Function Shell_NotifyIcon Lib "shell32" Alias "Shell_NotifyIconA" (ByVal dwMessage As Long, pnid As NOTIFYICONDATA) As Boolean
Dim t As NOTIFYICONDATA 
                                    

Step 9

Double Click on the Form of Form1 and type in the Form_Load() Sub:


t.cbSize = Len(t)
t.hWnd = Me.picHook.hWnd
t.uId = 1&
t.uFlags = NIF_ICON Or NIF_TIP Or NIF_MESSAGE
t.ucallbackMessage = WM_MOUSEMOVE
t.hIcon = Me.Icon
t.szTip = "TaskTray" & Chr$(0)
Shell_NotifyIcon NIM_ADD, t
Me.Hide
App.TaskVisible = False  
                                    

Step 10

Double Click on the Picture Box of picHook and type in the picHook_MouseMove() Sub:


Static rec As Boolean, msg As Long
msg = X / Screen.TwipsPerPixelX
If rec = False Then
    rec = True
    Select Case msg
    Case WM_LBUTTONDBLCLK:
    Case WM_LBUTTONDOWN:
    Case WM_LBUTTONUP:
    Case WM_RBUTTONDBLCLK:
    Case WM_RBUTTONDOWN:
    Case WM_RBUTTONUP:
        Me.PopupMenu mnuPopup 
    End Select
    rec = False
End If 
                                    

Step 11

Select Popup from the Form of Form1 and select Exit from the Menu, and type in the mnuPopupExit_Click() Sub:


t.cbSize = Len(t)
t.hWnd = picHook.hWnd
t.uId = 1&
Shell_NotifyIcon NIM_DELETE, t
End 
                                    

Step 12

Save the Project, for example prjTaskTray, into a vacant folder as you have finished the application. Click on Start / Run
Start / Run
This should Start the application in the Task Tray
Task Tray Running

Step 13

Now Right Click with the Mouse on the Icon nearest the Clock (the Icon will be the same Icon that was the Top Left Corner of the Form) and this will appear:

Task Tray Menu

Step 14

Click on Exit to End the Program. Try Changing the Icon or add other Sub Menus and Menus to the Popup Menu and see what happens!