Create a System Tray icon

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 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

Share the Post:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes

ransomware cyber attack

Why Is Ransomware Such a Major Threat?

One of the most significant cyber threats faced by modern organizations is a ransomware attack. Ransomware attacks have grown in both sophistication and frequency over the past few years, forcing