Quantcast
Viewing all articles
Browse latest Browse all 1449

How to add a System Tray icon to your program

This won't let you click (or double click) on the system tray (aka notification area) icon to restore your program from a minimized state. It won't let you right click on it to bring up a context menu to access various features of your program. Those are things that I haven't figured out how to implement yet. But it will let you cause an icon to appear down there, and a bit more. That bit more is that it will display a notification bubble with a customizable title and content (which you can click on to make the bubble go away), when the icon first appears, and will also display a "tool tip" when you hover your cursor over the icon.

Here's the code for this:
Code:

Private Declare Function Shell_NotifyIcon Lib "shell32" Alias "Shell_NotifyIconA" (ByVal dwMessage As Long, lpData As NOTIFYICONDATA) As Long
Private Declare Function LoadImage Lib "user32" Alias "LoadImageA" (ByVal hInst As Long, ByVal lpszName As String, ByVal uType As Long, ByVal cxDesired As Long, ByVal cyDesired As Long, ByVal fuLoad As Long) As Long
Private Declare Function DestroyIcon Lib "user32" (ByVal hIcon As Long) As Long

Private Type NOTIFYICONDATA
    cbSize As Long
    hWnd As Long
    uID As Long
    uFlags As Long
    uCallbackMessage As Long
    hIcon As Long
    szTip As String * 128
    dwState As Long
    dwStateMask As Long
    szInfo As String * 256
    uVersion As Long
    szInfoTitle As String * 64
    dwInfoFlags As Long
    guidItem(3) As Long
    hBalloonIcon As Long
End Type

Private Const NOTIFYICON_VERSION As Long = 3
Dim hIcon As Long
Dim a As NOTIFYICONDATA

Private Const IcoFile As String = "Path To Icon File Goes Here"

Private Sub Form_Load()
hIcon = LoadImage(0, IcoFile, 1, 16, 16, 16)
a.cbSize = Len(a)
a.uFlags = 2 + 4 + 16
a.uVersion = NOTIFYICON_VERSION
a.szInfo = "Test Message"
a.szInfoTitle = "Test"
a.hIcon = hIcon
a.hBalloonIcon = hIcon
a.dwInfoFlags = 4
a.hWnd = Me.hWnd
a.szTip = "Test Tip"
GetNullPaddedString a.szInfo
GetNullPaddedString a.szInfoTitle
GetNullPaddedString a.szTip
Shell_NotifyIcon 0, a
End Sub

Private Sub Form_Unload(Cancel As Integer)
Shell_NotifyIcon 2, a
DestroyIcon hIcon
End Sub

Private Sub GetNullPaddedString(ByRef Text As String)
Dim Text2 As String
Text2 = RTrim$(Text)
Text = Text2 & String$(Len(Text) - Len(Text2), vbNullChar)
End Sub

Note that where it says Private Const IcoFile As String = "Path To Icon File Goes Here" you will have to change the text "Path To Icon File Goes Here" to an actual path to an ICO file.

Viewing all articles
Browse latest Browse all 1449

Trending Articles