The new vbAccelerator Site - more VB and .NET Code and Controls
Source Code
3 Code Libraries &nbsp
&nbsp

Add Your App to the Systray the Easy Way

 
 

This form class encapsulates all the SysTray functionality you'll need!

 


 NOTE: this code has been superceded by the version at the new site.



&nbsp

SysTray Demo Application

Download frmSysTray (2kb)
Download the SysTray demonstration project (23kb)

&nbsp UpdatedUpdated! 21 March 2000 &nbsp
&nbsp Added the Win32 API call SetForegroundWindow before showing the SysTray menu - this ensures that the menu dismisses when the user clicks off the menu onto (for example) the desktop, whereas before it used to stick. &nbsp
&nbsp Fixed problem on systems with Large Fonts set. The code hardcoded a twips/pixel value of 15 into the message values responded to from the SysTray. Now the code uses VB's ScaleX method to get the correct message value so it works on all systems. &nbsp

This sample application presents a small form which you can drop into your project to get immediate SysTray support. A lot of the source code I've seen for VB SysTray interfaces has used an OCX or DLL and subclassed for a SysTray message, however, it turns out you don't need to do this, because you can specify what message Windows will use to notify you of events in the tray. As a consequence, you just need to choose a message that corresponds to a Visual Basic event - in this case the sample code uses the WM_MOUSEMOVE event. This fires a mouse move event on the Visual Basic form, with the x parameter set to the corresponding notification flag.

To avoid any confusion with actual MouseMove events due to the user moving the mouse over the form, the form is set to be invisible. Since the form can act as a class, it is easy to provide it with methods to set what menu options to show, what icon will be shown in the System Tray and also what tool tip to show in the SysTray. I've also provided events which are fired whenever there is a Mouse Down, Mouse Up or Double Click on your SysTray icon, and a method to show the menu created in the form which raises a MenuClick event when a menu is selected.

Here is all the code you need in a project which has frmSysTray included:

Private WithEvents m_frmSysTray As frmSysTray

Private Sub Form_Load()
&nbsp &nbsp Set m_frmSysTray = New frmSysTray
&nbsp &nbsp With m_frmSysTray
&nbsp &nbsp &nbsp &nbsp .AddMenuItem "&Open SysTray Sample", "open", True
&nbsp &nbsp &nbsp &nbsp .AddMenuItem "-"
&nbsp &nbsp &nbsp &nbsp .AddMenuItem "&Close", "close"
&nbsp &nbsp &nbsp &nbsp .ToolTip = "SysTray Sample!"
&nbsp &nbsp &nbsp &nbsp .IconHandle = Me.Icon.Handle
&nbsp &nbsp End With
End Sub

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
&nbsp &nbsp Unload m_frmSysTray
&nbsp &nbsp Set m_frmSysTray = Nothing
End Sub

Private Sub m_frmSysTray_MenuClick(ByVal lIndex As Long, ByVal sKey As String)
&nbsp &nbsp Select Case sKey
&nbsp &nbsp Case "open"
&nbsp &nbsp &nbsp &nbsp Me.Show
&nbsp &nbsp &nbsp &nbsp Me.ZOrder
&nbsp &nbsp Case "close"
&nbsp &nbsp &nbsp &nbsp Unload Me
&nbsp &nbsp End Select
&nbsp &nbsp
End Sub

Private Sub m_frmSysTray_SysTrayDoubleClick(ByVal eButton As MouseButtonConstants)
&nbsp &nbsp Me.Show
&nbsp &nbsp Me.ZOrder
End Sub

Private Sub m_frmSysTray_SysTrayMouseDown(ByVal eButton As MouseButtonConstants)
&nbsp &nbsp If (eButton = vbRightButton) Then
&nbsp &nbsp &nbsp &nbsp m_frmSysTray.ShowMenu
&nbsp &nbsp End If
End Sub



Back to top

Back to Source Code Overview

&nbsp
 

About  Contribute  Send Feedback  Privacy

Copyright © 1998-2000, Steve McMahon ( steve@vbaccelerator.com). All Rights Reserved.
Last updated: 21 March 2000