Atlanta Custom Software Development 

 
   Search        Code/Page
 

User Login
Email

Password

 

Forgot the Password?
Services
» Web Development
» Maintenance
» Data Integration/BI
» Information Management
Programming
  Database
Automation
OS/Networking
Graphics
Links
Tools
» Regular Expr Tester
» Free Tools

Create a System Tray icon
[ All Languages » VB »  Windows]

Total Hit ( 4895)

Rate this article:     Poor     Excellent 

 Submit Your Question/Comment about this article

Rating


 


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:

Click here to copy the following block
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:

Click here to copy the following block
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:

Click here to copy the following block
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:

Click here to copy the following block
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:

Click here to copy the following block
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:

Click here to copy the following block
Private Sub Form_Unload(Cancel As Integer)
  Shell_NotifyIcon NIM_DELETE, nid
End Sub


Submitted By : Nayan Patel  (Member Since : 5/26/2004 12:23:06 PM)

Job Description : He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting.
View all (893) submissions by this author  (Birth Date : 7/14/1981 )


Home   |  Comment   |  Contact Us   |  Privacy Policy   |  Terms & Conditions   |  BlogsZappySys

© 2008 BinaryWorld LLC. All rights reserved.