Sometimes is useful to add an icon in the Window taskbar’s status area (a.k.a. System Tray), to gain a better interaction between the user and your application. The only API you have to use is Shell_NotifyIcon:
Declare Function Shell_NotifyIcon Lib "shell32" Alias "Shell_NotifyIconA" _ (ByVal dwMessage As Long, pnid As NOTIFYICONDATA) As Boolean
The first parameter specifies the action to be taken and can be assigned one of the following values:
Const NIM_ADD = &H0 ' Add an iconConst NIM_MODIFY = &H1 ' Modify an iconConst NIM_DELETE = &H2 ' Delete an icon
The second parameter is a NOTIFYICONDATA structure that contains all the information about the icon to be created or modified:Type NOTIFYICONDATA cbSize As Long ‘ size of this structure hwnd As Long ‘ handle of parent window uId As Long ‘ Used only with multiple icons associated ‘ with the same application uFlags As Long ‘ Flags uCallBackMessage As Long ‘ Notifications handler hIcon As Long ‘ Icon ‘ handle of the icon szTip As String * 64 ‘ Tooltip textEnd TypeuFlags indicates which properties you want to modify (for instance the tooltip text) and can be one of the following values:
Const NIF_MESSAGE = &H1 ' To change uCallBackMessage memberConst NIF_ICON = &H2 ' To change the iconConst NIF_TIP = &H4 ' To change the tooltip text
You can also combine more than one flag with Or operator:
nid.uFlags = NIF_ICON Or NIF_TIP Or NIF_MESSAGE
uCallBackMessage specifies the message the system will send to your form for notifying mouse actions on the icon in the System Tray; the value of wParam associated to this message is the value of the original mouse message. You can subclass the form to trap this message and use wParam to determine which mouse action occurred, but there is a better way.
The trick is to set the callback message equal to WM_MOUSEMOVE, which causes a Form_MouseMove event to be automatically raised in yout form. If the form’s ScaleMode is set to vbPixels, the value of the X argument for this event is equal to the original value of the wParam argument of the callback message, and therefore you can use it to determine which mouse action was performed on the System Tray icon. If the ScaleMode is different from vbPixel you can use the ScaleX method to convert it:
Const WM_MOUSEMOVE = &H200Const WM_LBUTTONDOWN = &H201 ' Left clickConst WM_LBUTTONDBLCLK = &H203 ' Left double clickConst WM_RBUTTONDOWN = &H204 ' Right clickConst WM_RBUTTONDBLCLK = &H206 ' Right double clickDim nid As NOTIFYICONDATAPrivate Sub Form_Load() nid.cbSize = Len(nid) nid.hwnd = Form1.hwnd nid.uId = 0 nid.uFlags = NIF_ICON Or NIF_TIP Or NIF_MESSAGE nid.uCallBackMessage = WM_MOUSEMOVE nid.hIcon = Form1.Icon ' Chr$(0) is required at the end of the string nid.szTip = "Hello World" + vbNullChar Shell_NotifyIcon NIM_ADD, nidEnd SubPrivate Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, _ Y As Single) Dim Msg As Long Msg = ScaleX(X, ScaleMode, vbPixels) Select Case Msg Case WM_LBUTTONDOWN MsgBox "Left click!" Case WM_LBUTTONDBLCLK MsgBox "Left double-click!" Case WM_RBUTTONDOWN MsgBox "Right click!" Case WM_RBUTTONDBLCLK MsgBox "Right double-click!" End SelectEnd Sub
Once you’ve created an icon you can modify it by setting new values in the NID structure and invoking Shell_NotifyIcon once again:
Private Sub cmdChangeTooltip() nid.szTip = "A new tooltip text" & vbNullChar Shell_NotifyIcon NIM_MODIFY, nidEnd Sub
Finally, you should delete the icon before unloading the form:
Private Sub Form_Unload(Cancel As Integer) Shell_NotifyIcon NIM_DELETE, nidEnd Sub