Task Tray

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.

Printer Friendly Download Tutorial (43.9KB) Download Source Code (7KB)

Step 1

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

New Project

Step 2

A Blank Form named Form1 should then appear, see below:

Form 1

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, see below:

Form1 with Picture Box

Step 5

Then goto the Properties Box and change the Name Property to picHook, see below:

Picture Box Properties

Step 6

Click on the Form (Form1) then got the Properties Box and change the Visible Property to False and the ShowInTaskBar Property to False, see below:

Form1 Properties

Step 7

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

Menu Editor Window

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 (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 PictureBox (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 (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 or Press F5 and the below will appear:

Application 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:

Icon Right Clicked

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!

Copyright Comentsys © 1997 - 2009, All rights reserved. About | Contact | Link to Page
Valid XHTML 1.1! Valid CSS Level Double-A conformance icon, W3C-WAI Web Content Accessibility Guidelines 1.0 This website is ICRA rated