|
Download frmSysTray (2kb) Download the SysTray demonstration project (23kb)
  |
Updated! 21 March 2000
|
  |
  |
|
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.
|
  |
  |
|
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.
|
  |
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()
    Set m_frmSysTray = New frmSysTray
    With m_frmSysTray
        .AddMenuItem "&Open SysTray Sample", "open", True
        .AddMenuItem "-"
        .AddMenuItem "&Close", "close"
        .ToolTip = "SysTray Sample!"
        .IconHandle = Me.Icon.Handle
    End With
End Sub
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    Unload m_frmSysTray
    Set m_frmSysTray = Nothing
End Sub
Private Sub m_frmSysTray_MenuClick(ByVal lIndex As Long, ByVal sKey As String)
    Select Case sKey
    Case "open"
        Me.Show
        Me.ZOrder
    Case "close"
        Unload Me
    End Select
   
End Sub
Private Sub m_frmSysTray_SysTrayDoubleClick(ByVal eButton As MouseButtonConstants)
    Me.Show
    Me.ZOrder
End Sub
Private Sub m_frmSysTray_SysTrayMouseDown(ByVal eButton As MouseButtonConstants)
    If (eButton = vbRightButton) Then
        m_frmSysTray.ShowMenu
    End If
End Sub
Back to top
Back to Source Code Overview
|
  |