How can my application be displayed in the system tray?


I have a VB Application. I want to know, how can I put it in the system tray, just as we see WinAmp and other applications?
rajivrajAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
mhdhallakConnect With a Mentor Commented:
There is a freeware control that makes putting your icon in the system tray easier than ever. If you want this control, send me your email at:
mhdhallak@hotmail.com
0
 
nIcebearCommented:
Check the net for Custom Controls, that can put your form into the SysTray. I'm sure, there are some (I had one, but I can't find). There's also an API-call to do this, but I can't remember. Maybe someone else knows.
0
 
cquinnCommented:
There is a Systray example project in the VB sample programs - it will create Systray.ocx - have a look - it works well for me
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
nIcebearCommented:
I found another sample using API-calls. If you want to I can email it to you (sorry, too big to post here)
0
 
rajivrajAuthor Commented:
yeah, mail me buddy:). I am at rajivraj007@yahoo.com
0
 
VbmasterCommented:
Here's the way to do it with API's, easy and quick. ;)

Add a picturebox to your form, name it 'picTray'. Add this to the form..


'Tray Icon
Type NOTIFYICONDATA
  cbSize    As Long
  hWnd      As Long
  uId       As Long
  uFlags    As Long
  uCallback As Long
  hIcon     As Long
  szTip     As String * 64
End Type
Private TrayIcon As NOTIFYICONDATA
Private Declare Function Shell_NotifyIcon Lib "shell32" Alias "Shell_NotifyIconA" (ByVal dwMessage As Long, pnid As NOTIFYICONDATA) As Boolean

Public Sub ShowIcon(Optional Show As Boolean = True, Optional Text As Variant, Optional ChangeText As Boolean, Optional ChangeIcon As Boolean, Optional ResourceIcon As Integer)

  Const NIM_ADD = &H0
  Const NIM_MODIFY = &H1
  Const NIM_DELETE = &H2
  Const WM_MOUSEMOVE = &H200
  Const NIF_MESSAGE = &H1
  Const NIF_ICON = &H2
  Const NIF_TIP = &H4
 
  If ChangeText Then
    If IsMissing(Text) Then
      TrayIcon.szTip = App.ProductName & Chr$(0)
    Else
      TrayIcon.szTip = Text & Chr$(0)
    End If
  End If
 
  If ChangeIcon Then
    If (ResourceIcon > 0) Then
      frmMain.picTray.MouseIcon = LoadResPicture(ResourceIcon, vbResIcon)
      TrayIcon.hIcon = frmMain.picTray.MouseIcon
    Else
      TrayIcon.hIcon = frmMain.Icon
    End If
    Shell_NotifyIcon NIM_MODIFY, TrayIcon
  End If
 
  If ChangeText Or ChangeIcon Then
    Shell_NotifyIcon NIM_MODIFY, TrayIcon
  ElseIf Show Then
    TrayIcon.cbSize = Len(TrayIcon)
    If IsMissing(Text) Then
      TrayIcon.szTip = App.ProductName & Chr$(0)
    Else
      TrayIcon.szTip = Text & Chr$(0)
    End If
    TrayIcon.hWnd = frmMain.picTray.hWnd
    TrayIcon.hIcon = frmMain.Icon
    TrayIcon.uId = 1&
    TrayIcon.uCallback = WM_MOUSEMOVE
    TrayIcon.uFlags = NIF_ICON Or NIF_TIP Or NIF_MESSAGE
    Shell_NotifyIcon NIM_ADD, TrayIcon
  Else
    Shell_NotifyIcon NIM_DELETE, TrayIcon
  End If

End Sub


Now you can use the picTray MouseMove event to get information about what the user does with the tray icon..

Private Sub picTray_MouseMove(Button As Integer, Shift As Integer, X As Single, y As Single)

  Const WM_LBUTTONDBLCLK = &H203
  Const WM_LBUTTONDOWN = &H201
  Const WM_LBUTTONUP = &H202
  Const WM_RBUTTONDBLCLK = &H206
  Const WM_RBUTTONDOWN = &H204
  Const WM_RBUTTONUP = &H205
 
  Static rec As Boolean
  Static msg As Long
 
  msg = X / Screen.TwipsPerPixelX
  If rec = False Then
    rec = True
    Select Case msg
    Case WM_LBUTTONDBLCLK
      'User doubleclicked on the icon with the left mouse button
    Case WM_LBUTTONDOWN
      'User clicked on the icon with the left mouse button (MouseDown)
    Case WM_LBUTTONUP
      'User clicked on the icon with the left mouse button (MouseUp)
    Case WM_RBUTTONDBLCLK
      'User doubleclicked on the icon with the right mouse button
    Case WM_RBUTTONDOWN
      'User clicked on the icon with the right mouse button (MouseDown)
    Case WM_RBUTTONUP
      'User clicked on the icon with the right mouse button (MouseUp)
    End Select
    rec = False
  End If

End Sub


To show the tray icon you call the ShowIcon function, the parameters are optional, you can easily see what they do in the code (or by testing with different values. ;)).
0
 
VbmasterCommented:
Oops, I forgot to change it from 'frmMain' to 'Me'.
0
 
VbmasterCommented:
Can't believe anything can be easier than the code I submitted. ;)
0
 
rajivrajAuthor Commented:
hey vbmaster, i will try out the code you gave me..........
0
 
VbmasterCommented:
Okay, I made some improvements to the code the last couple of days, if you for some reason need to create more than one icon for the same program let me know and you'll get the updated code.
0
All Courses

From novice to tech pro — start learning today.