22 | 02 | 2017

Minimize to systray including right click pop up menu

A VB created application will open in the taskbar. It is possible to overrule this and to place a icon in the systray. If that is not enough you can add a right click menu to this icon.

First create a new form and add the following code:

Option Explicit
 
' Type passed to Shell_NotifyIcon
Private Type NotifyIconData
Size As Long
Handle As Long
ID As Long
Flags As Long
CallBackMessage As Long
Icon As Long
Tip As String * 64
End Type
 
' Constants for managing System Tray tasks, found in shellapi
Private Const AddIcon = &H0
Private Const ModifyIcon = &H1
Private Const DeleteIcon = &H2
 
Private Const WM_MOUSEMOVE = &H200
Private Const WM_LBUTTONDBLCLK = &H203
Private Const WM_LBUTTONDOWN = &H201
Private Const WM_LBUTTONUP = &H202
Private Const WM_RBUTTONDBLCLK = &H206
Private Const WM_RBUTTONDOWN = &H204
Private Const WM_RBUTTONUP = &H205
 
Private Const MessageFlag = &H1
Private Const IconFlag = &H2
Private Const TipFlag = &H4
 
Private Declare Function Shell_NotifyIcon _
Lib "shell32" Alias "Shell_NotifyIconA" ( _
ByVal Message As Long, Data As NotifyIconData) As Boolean
 
Private Data As NotifyIconData
 
Private Sub Form_Load()
IcontoTray
Visible = False
Me.WindowState = vbMinimized
Me.Hide
End Sub
 
Private Sub IcontoTray()
 
Data.Size = Len(Data)
Data.Handle = Hwnd
Data.ID = vbNull
Data.Flags = IconFlag Or TipFlag Or MessageFlag
Data.CallBackMessage = WM_MOUSEMOVE
Data.Icon = Icon
Data.Tip = "VisibleVisual Sample TrayIcon...." & vbNullChar
Call Shell_NotifyIcon(AddIcon, Data)
 
End Sub
 
Private Sub DeleteIconFromTray()
Call Shell_NotifyIcon(DeleteIcon, Data)
End Sub
 
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
 
Dim msg As Long
 
On Error GoTo ErrorHandler
 
'Respond to user interaction
msg = X / Screen.TwipsPerPixelX
Select Case msg
 
Case WM_LBUTTONDBLCLK
      Visible = Not Visible
      WindowState = Abs(Not Visible)
Case WM_LBUTTONDOWN
Case WM_LBUTTONUP
Case WM_RBUTTONDBLCLK
Case WM_RBUTTONDOWN
Case WM_RBUTTONUP
Call PopupMenu(mnufile, vbPopupMenuRightAlign)
End Select
 
Exit Sub
ErrorHandler:
 
'Errorhandler code
 
End Sub
 
'Delete the icon when exiting the form
Private Sub Form_Terminate()
DeleteIconFromTray
End Sub
 
Private Sub mnuItem1_Click(Index As Integer)
MsgBox "MenuItem 1 has been pressed", vbInformation, "VVinfo"
End Sub
 
Private Sub mnuItem2_Click(Index As Integer)
MsgBox "MenuItem 2 has been pressed", vbInformation, "VVinfo"
End Sub
 
Private Sub mnuItem3_Click(Index As Integer)
MsgBox "MenuItem 3 has been pressed", vbInformation, "VVinfo"
End Sub
 
When the code is pasted into the form goto the menu editor (CTRL-E) and add a menu called mnufile with three submenu items.
 
 
 
'Create a menuitem calle mnufile'
 Below a sample of the result.
 
 
 
 
Login

Sign up now and upload your code to the website.

Help us to continue.....
Statistics
Articles View Hits
2174940
Latest Articles