VERSION 5.00
Begin VB.Form frmSysTray 
   Caption         =   "Systray"
   ClientHeight    =   765
   ClientLeft      =   60
   ClientTop       =   345
   ClientWidth     =   3450
   BeginProperty Font 
      Name            =   "Tahoma"
      Size            =   8.25
      Charset         =   0
      Weight          =   400
      Underline       =   0   'False
      Italic          =   0   'False
      Strikethrough   =   0   'False
   EndProperty
   LinkTopic       =   "Form1"
   ScaleHeight     =   765
   ScaleWidth      =   3450
   StartUpPosition =   3  'Windows Default
   Begin VB.PictureBox picTrayIcon 
      AutoRedraw      =   -1  'True
      AutoSize        =   -1  'True
      BorderStyle     =   0  'None
      BeginProperty Font 
         Name            =   "MS Sans Serif"
         Size            =   8.25
         Charset         =   0
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   480
      Left            =   120
      ScaleHeight     =   480
      ScaleWidth      =   480
      TabIndex        =   0
      Top             =   120
      Visible         =   0   'False
      Width           =   480
   End
   Begin VB.Label lblX 
      Caption         =   "Press the min button to minimize to the tray, left double click the icon to restore, or right click for menu options."
      Height          =   615
      Left            =   120
      TabIndex        =   1
      Top             =   120
      Width           =   3135
   End
   Begin VB.Menu mnuMain 
      Caption         =   ""
      Visible         =   0   'False
      Begin VB.Menu mnuRestoreWindow 
         Caption         =   "Restore Window"
      End
      Begin VB.Menu mnuExit 
         Caption         =   "Exit"
      End
   End
End
Attribute VB_Name = "frmSysTray"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit

Private Declare Function Shell_NotifyIcon Lib "shell32" Alias "Shell_NotifyIconA" (ByVal dwMessage As Long, lpData As NOTIFYICONDATA) As Long

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 As Long = &H0
Private Const NIM_DELETE As Long = &H2
Private Const NIM_MODIFY As Long = &H1
Private Const NIF_MESSAGE As Long = &H1
Private Const NIF_ICON As Long = &H2
Private Const NIF_TIP As Long = &H4
Private Const WM_LBUTTONDOWN As Long = &H201
Private Const WM_LBUTTONDBLCLK As Long = &H203
Private Const WM_RBUTTONUP As Long = &H205

Private nidIcon As NOTIFYICONDATA

Private Sub Form_Load()
   With nidIcon
      'add the size of the struct
      .cbSize = Len(nidIcon)
      'add the handle to the window that the function will send messages to
      .hWnd = picTrayIcon.hWnd
      'set up the valid members of the struct
      .uFlags = NIF_ICON Or NIF_TIP Or NIF_MESSAGE
      'this is the message that will be sent to the hWnd above
      .uCallbackMessage = WM_LBUTTONDOWN
      'handle to the icon to add to the tray
      .hIcon = picTrayIcon.Picture.Handle
      'tool tip (always add a vbNullChar to the end)
      .szTip = Me.Caption & vbNullChar
   End With
End Sub

Private Sub Form_Resize()
   If Me.WindowState = vbMinimized Then
      'if it's minimizing, hide
      Me.Hide
      'add the icon to the tray
      Shell_NotifyIcon NIM_ADD, nidIcon
   Else
      'otherwise, it's coming back, so delete the icon
      Shell_NotifyIcon NIM_DELETE, nidIcon
   End If
End Sub

Private Sub Form_Unload(Cancel As Integer)
   'prog is closing, so make sure the icon is gone
   Shell_NotifyIcon NIM_DELETE, nidIcon
End Sub

Private Sub mnuExit_Click()
   'end the prog properly
   Unload Me
End Sub

Private Sub mnuRestoreWindow_Click()
   'restore the window
   Me.WindowState = vbNormal
   'show the window
   Me.Show
   'remove the icon from the tray
   Shell_NotifyIcon NIM_DELETE, nidIcon
End Sub

Private Sub picTrayIcon_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
   Select Case CLng(X \ Screen.TwipsPerPixelX)
      Case WM_LBUTTONDBLCLK
         'left button double click restores window
         mnuRestoreWindow_Click
      Case WM_RBUTTONUP
         'right button up, so show the popup menu with a default
         PopupMenu mnuMain, , , , mnuRestoreWindow
   End Select
End Sub
