Vikasumit

How To: Use the System Tray Directly from VB (ID: Q176085) from MSDN Jan 2000
Author : Sumit Gupta

This article is taken from MSDN and rewritten by Sumit Gupta for easy understanding

Functions Used

API FUNCTION


1) Declare Function SetForeGroundWindow lib "User32" (ByVal hwnd as Long) as Long
2) Declare Function Shell_NotifyIcon lib "Shell32" Allias "Shell_NotifyIconA" (ByVal dwMessage as long, pnid as NOTIFYICONDATA) as Boolean

DATA TYPE USED

Public Type NOTIFYICONDATA
   cbsize as long
   hwnd as long
   uId as long
   uFlags as long
   uCallBack as long
   hIcon as long
   szTip as long
End Type

CONSTANT USED

Public Const NIM_ADD= &H0
Public Const NIM_MODIFY= &H1
Public Const NIM_DELETE = &H2

Public Const NIF_MESSAGE = &H1
Public Const NIF_ICON=&H2
Public Const NIF_TIP= &H4

Public Const WM_MOUSEMOVE= &H200
Public Const WM_LBUTTONDOWN=&H201
Public Const WM_LBUTTONUP=&H202
Public Const WM_LBUTTONDBLCLK=&H203
Public Const WM_RBUTTONDOWN=&H204
Public Const WM_RBUTTONUP=&H205
Public Const WM_RBUTTONDBLCLK=&H206


WORKING

1) First Define one global variable of type NOTIFYICONDATA
   Public nid as NOTIFYICONDATA
2) set all attributes of nid
  With nid
     .cbsize=len(nid)
     .hwnd=me.hwnd
     .uid=vbNULL
     .uFlags=NIF_ICON or NIF_TIP or NIF_MESSAGE
     .uCallBackMessage =WM_MOUSEMOVE
     .hICON= 'An Icon File path or source from form
     .szTip= 'Tip to display in the system tray
  End with
3) Now Add Icon in the system tray by calling Shell_NotifyICon() function.
   Shell_NotifyIcon (NIM_ADD,nid)
4) In MouseMove event of form check the X-Axis of form icon in system tray using the parameter of sub MouseMove. (Determine if its present in system tray)
  Ex.
     Msg as long
     Msg=X/screen.twipsperpixelX
     Now check the case using Msg value
     Select case
      Case WM_LBUTTONDOWN
      Case WM_LBUTTONUP
       Case WM_LBUTTONDBLCLK
       Case WM_RBUTTONDOWN
       Case WM_RBUTTONUP
       Case WM_RBUTTONDBLCLK
     End select

NOTES:

1) To modify any of System Tray Icon attribute change to nid and than call Shell_NotifyIcon() with NIM_MODIFY value.
2) To Delete the Icon call Shell_NotifyIcon() with NIM_DELETE.
3) To make another System tray Icon use another NOTIFYICONDATA variable and than NIM_ADD const with Shell_NotifyIcon
4) Use NIM_DELETE or NIM_MODIFY only NIM_ADD has called before atleast once.
5) Do not call out-of-process(ActiveX) exe function from System tray Icon. It might give an Runtime error.