How to Minimize/Send an App Icon to System Tray in Visual Basic 6

The following code snippet allows to manipulate the system tray by adding your apps icon when you minimize the form and restore when you double click on the app's icon on the system tray area.

Option Explicit

Public 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

Public Const NIM_ADD = &H0
Public Const NIM_MODIFY = &H1
Public Const NIM_DELETE = &H2
Public Const WM_MOUSEMOVE = &H200
Public Const NIF_MESSAGE = &H1
Public Const NIF_ICON = &H2
Public Const NIF_TIP = &H4
Public Const WM_LBUTTONDBLCLK = &H203 'Double-click
Public Const WM_LBUTTONDOWN = &H201 'Button down
Public Const WM_LBUTTONUP = &H202 'Button up
Public Const WM_RBUTTONDBLCLK = &H206 'Double-click
Public Const WM_RBUTTONDOWN = &H204 'Button down
Public Const WM_RBUTTONUP = &H205 'Button up
Public Declare Function Shell_NotifyIcon Lib "shell32" Alias "Shell_NotifyIconA" (ByVal dwMessage As Long, pnid As NOTIFYICONDATA) As Boolean

'Dimension a variable as the user-defined data type.
Dim nid As NOTIFYICONDATA

This sub sends the icon to system tray.
Sub minToTray()
    '------------------------
    '--- create tray icon ---
    '------------------------

    nid.cbSize = Len(nid)
    nid.hWnd = Me.hWnd
    nid.uId = vbNull
    nid.uFlags = NIF_ICON Or NIF_TIP Or NIF_MESSAGE
    nid.uCallBackMessage = WM_MOUSEMOVE
    nid.hIcon = Main.Icon  'the icon will be your Form1 project icon
    nid.szTip = TrayName   'tip text
    
    'adds the icon to the taskbar area
    Shell_NotifyIcon NIM_ADD, nid
    
End Sub

'This is Tip text when hovering on the system tray icon 
Function TrayName() as String
    TrayName = App.EXEName & " Ver." & App.Major & "." & App.Minor & vbCrLf & App.CompanyName & vbNullChar
End Function

When the minimize button is clicked it will hide the form and send icon to system tray.
Private Sub imgMin_Click()       
    'Hides main form and shows mini mode
    Me.Hide
    minToTray   'send icon to system tray   
End Sub

When the app icon on the system tray is doubled click it will show the form and delete the icon from the system tray.
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single)
    Dim Msg As Long
    Dim sFilter As String
    Msg = x / Screen.TwipsPerPixelX

        Select Case Msg
            Case WM_LBUTTONDOWN 
            Case WM_LBUTTONUP
            Case WM_LBUTTONDBLCLK
                Me.Show
                Me.WindowState = vbNormal
              
                'Deletes the tray icon when the form is shown
                Shell_NotifyIcon NIM_DELETE, nid ' del tray icon
            Case WM_RBUTTONDOWN
                'Show popup menu
                PopupMenu mExit
            Case WM_RBUTTONUP
            Case WM_RBUTTONDBLCLK
        End Select
  
End Sub

Comments

Post a Comment

Popular posts from this blog

How to Create a Configuration.INI Files in VB6

How to Make Windows Form Transparent using Visual Basic 6/VB6

How to Set Windows Form Always on Top of Other Applications in VB6