Jalaj P. Jha Technical & Miscellaneous Ramblings

18Jan/070

Accessing System Tray from Application

MS Outlook has a feature using which you can minimise the Outlook window to System Tray. That is, it no longer shows up in the task bar and if required can be opened by double clicking the icon on the System Tray. I find it very useful as it doesnot unnecessarily clutters the task bar and secondly this also prevents it from getting accidently closed when I resort to close all open apps by repeatedly pressing Alt+F4.

If this feature is useful for me then why not build it in my applications too...

Manipulating the System Tray icons require calling the Shell_NotifyIcon function declared in Shell32.dll. First of all we will require to declare this function which we will do in the code section of the form.

Private Declare Function Shell_NotifyIcon Lib "shell32" Alias "Shell_NotifyIconA" _
    (ByVal dwMessage As Long, pnid As NOTIFYICONDATA) As Boolean

This function can be used to add icon to the system tray, or to modify or delete it. This is made possible using the first parameter dwMessage which can be either of the three as defined below.

Private Const NIM_ADD = &H0
Private Const NIM_MODIFY = &H1
Private Const NIM_DELETE = &H2

The second parameter is of data type NOTIFYICONDATA which is defined as below :

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

cbsize above is the aggregate size of data of NOTIFYICONDATA type
hwnd is the form's handle which is passed so that the callback messages can be sent to this form.
uId is any number of type long
uFlags is the number shich shows which of the other members below will be considered when placing the icon on the system tray.
uCallBackMessage is the windows message which should be sent back to the form.
hIcon is the handle to the icon which is to be shown on the system tray.
szTip is the string which is to be shown on tooltip when mouse is brought over the icon.

uFlags as defined above contain either or all of the values defined below. It is these values that will detemine if any callback message is to be sent or not, Icon is to be shown or not (In other case the location is left blank) and the tooltip string is to be shown or not.

Private Const NIF_MESSAGE = &H1
Private Const NIF_ICON = &H2
Private Const NIF_TIP = &H4

Callback message is the Windows Message which is sent to the form as the mouse moves over the area contained by the icon. We would use the mousemove message which will trigger the MouseMove event of the form. the constant for MouseMove sendmessage is defined as

Private Const WM_MOUSEMOVE = &H200

The windows message sends keypresses for the lparam parameter (For detail on SendMessage please check More on SendMessage) values for which are as below

Private Const WM_LBUTTONDBLCLK = &H203   'Double-click
Private Const WM_LBUTTONDOWN = &H201     'Button down
Private Const WM_LBUTTONUP = &H202       'Button up

Private Const WM_RBUTTONDBLCLK = &H206   'Double-click
Private Const WM_RBUTTONDOWN = &H204     'Button down
Private Const WM_RBUTTONUP = &H205       'Button up

Now that we are ready with all our requirements, Let's start with our project. Create a new project and in the form place a CheckBox chkSysTray with caption "Minimize to SystemTray. Change the icon Property of the form to add your custom icon for the form, which would also be shown on the system tray.

systray.jpg

Our aim here is that if we have checked the checkbox as described above, the form on being minimised hides itself from the taskbar and is visible on the System Tray as an icon. When this icon is double-clicked the form shows up again.

The Resize event of the form will trigger when the form is minimized, where we would check the WindowState property of the form to ensure that the event triggered was actually due to it getting minimised and not due to manual resizing or maximizing. Once confirmed, and that the checkbox is checked, it will call the hide method of the form and call the subroutine ShowAppInSysTray which will create a System Tray Icon.

Private Const WINDOW_NORMAL = 0
Private Const WINDOW_MINIMIZED = 1
Private Const WINDOW_MAXIMIZED = 2

Private Sub Form_Resize()

    If Form1.WindowState = WINDOW_MINIMIZED Then
        If chkSysTray Then
            Form1.Hide
            Call ShowAppInSysTray
        End If
    End If

End Sub

The ShowAppInSysTray requires an object of data type NOTIFYICONDATA which is declared as under.

Dim nid As NOTIFYICONDATA

The ShowAppInSysTray subroutine cells Shell_NotifyIcon function passing appropriate paameters and the Icon is created on the System Tray.

Private Sub ShowAppInSysTray()

         nid.cbSize = Len(nid)
         nid.hWnd = Form1.hWnd
         nid.uId = vbNull
         nid.uFlags = NIF_ICON Or NIF_TIP Or NIF_MESSAGE
         nid.uCallBackMessage = WM_MOUSEMOVE
         nid.hIcon = Form1.Icon
         nid.szTip = "Double-Click to Open " & Form1.Caption & vbNullChar

         Shell_NotifyIcon NIM_ADD, nid

End Sub

Now, once the icon is placed on system tray, whenever the mouse is moved over the icon SendMessage occurs raising the MouseMove event of the form. The lparam parameter carries the value of Mouse Clicks which is available in the event in parameter X, converted to Twips (i.e. multiplied by 15). The actual value need to be calculated by dividing it with Screen.TwipsPerPixelX.

If the click is found to be a left-double-click the WindowState of the form is restored to normal and the show method called to make it visible again. Shell_NotifyIcon function is called again to delete the icon created on the system tray.

Private Sub Form_MouseMove(Button As Integer, Shift As Integer, _
        X As Single, Y As Single)

    Dim msg As Long
    msg = X / Screen.TwipsPerPixelX

    If msg = WM_LBUTTONDBLCLK Then
        Form1.WindowState = 0
        Form1.Show

        Shell_NotifyIcon NIM_DELETE, nid

    End If

End Sub

Reference : Microsoft Help & Support

Comments (0) Trackbacks (0)

No comments yet.


Leave a comment

(required)

Trackbacks are disabled.

  • Twitter
  • Buzz
  • Facebook
  • Orkut
  • Picasa
  • YouTube