Today is Friday, 12th July 2024

Minimizing Into the SystemTray

Author: DarkKnightH20
Language: Visual Basic 6.0

For one reason or another, a lot of people have issues minimizing programs into the systemtray. It’s actually very simple in VB.

Code Reqs: You need to make a form called “Form1”. Also add a module called “Module1”.

Private Sub Form_Load()
With nid
.cbSize = Len(nid)
.hwnd = Me.hwnd
.uId = vbNull
.uCallBackMessage = WM_MOUSEMOVE
.hIcon = Me.Icon
.szTip = "DarkKnightH20 Rox My Sox" & vbNullChar
End With
End Sub

When the program loads, nid (which is all the goodies for the system tray) goes into effect. Cbsize is the length of the entire data pertaining to nid. Hwnd tells what window will be the one disappearing (in this case, me/form1). uId I never found much use for, but it’s an identifier. Keep it nulled. uFlags tell the program what we will be using (the icon, tooltip, as well as the retrieval of the app after clicking). uCallBackMessage tells what sort of stuff to detect, such as mouse movement. hIcon specifies the icon to use. szTip contains the text of the tooltip (and must end with a nullchar, otherwise the tooltip becomes wide).

Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim Result As Long, msg As Long
If Me.ScaleMode = vbPixels Then: msg = X: Else msg = X / Screen.TwipsPerPixelX
Exit Sub
Me.WindowState = vbNormal
If Me.WindowState = vbMinimized Then: Me.Show
Shell_NotifyIcon NIM_DELETE, nid
End Sub

It’s more practical to sum up what’s actually occuring here. The program is checking to see if the mouse is over the icon in the system tray. If it is, then it waits to be left clicked, double clicked, right clicked, etc. After it is clicked, it removes the system tray icon (NIM_Delete) and shows the form again.

Private Sub Form_Resize()
On Error Resume Next
If Me.WindowState = vbMinimized Then
Shell_NotifyIcon NIM_ADD, nid
End If
End Sub

This detects to see if the form was minimized by verifying the windowstate. If so, it’ll make it invisible (visually as well as not show up in the taskbar). It will then add the icon via NIM_Add, while specifying the stats for the icon (nid).

Module1’s Code

Public Declare Function Shell_NotifyIcon Lib "shell32" Alias "Shell_NotifyIconA" _
(ByVal dwMessage As Long, pnid As NOTIFYICONDATA) As Boolean
Public Const NIM_ADD = &H0
Public Const NIM_MODIFY = &H1
Public Const NIM_DELETE = &H2
Public Const NIF_MESSAGE = &H1
Public Const NIF_ICON = &H2
Public Const NIF_TIP = &H4
Public Const WM_MOUSEMOVE = &H200
Public Const WM_LBUTTONDOWN = &H201
Public Const WM_LBUTTONUP = &H202
Public Const WM_LBUTTONDBLCLK = &H203
Public Const WM_RBUTTONDOWN = &H204
Public Const WM_RBUTTONUP = &H205
Public Const WM_RBUTTONDBLCLK = &H206
cbSize As Long
hwnd As Long
uId As Long
uFlags As Long
uCallBackMessage As Long
hIcon As Long
szTip As String * 64
End Type

To sum up the above, it just tells the program what functions are going to be used. Shell_NotifyIcon simply is a method of talking to the systemtray icon so that it can tell it what it needs to do. Add, Modify, Delete, and so forth — do what they say they do. They’ll add the icon, modify it, or delete it. As you can see, nid is given the NOTIFYICONDATA abilities, which are later specified to hold data such as the flags that will be used. All the WM constants are listed to detect how the icon is clicked, as well as mouse movement.

I wrote this pretty fast since I’m in a rush so it’s not as good of a tutorial as I’d like it to be. But that’s minimizing into the system tray in a nutshell.

There are also similar versions put together that run on the XDA mobile froms o2. They run great on mobile phones. I can’t be bothered to post it right now, but anyone who is savvy at coding will notice the similarities.

Leave a Reply


Affiliate Articles:

Amazon Deals