Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: VB4/32,VB5,VB6
Expertise: Advanced
Jan 22, 2000

Create a System Tray icon

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 icon
Const NIM_MODIFY = &H1 ' Modify an icon
Const 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 text End Type uFlags 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 member
Const NIF_ICON = &H2          ' To change the icon
Const 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 = &H200
Const WM_LBUTTONDOWN = &H201   ' Left click
Const WM_LBUTTONDBLCLK = &H203 ' Left double click
Const WM_RBUTTONDOWN = &H204   ' Right click
Const WM_RBUTTONDBLCLK = &H206 ' Right double click

Dim nid As NOTIFYICONDATA

Private 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, nid
End Sub

Private 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 Select
End 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, nid
End Sub
Finally, you should delete the icon before unloading the form:

Private Sub Form_Unload(Cancel As Integer)
    Shell_NotifyIcon NIM_DELETE, nid
End Sub
Alberto Falossi
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date