Visual Basic/Minimize app to tray
Expert: Ravindra - 1/23/2006
QuestionIn VB6, is there a way to minimize the app into tray instead of to the bottom bar? or alternatively by clicking a command button on the form to do that?
Thank you in advance if you have any ideas on coding in VB6 or using API or any third party plugins.
I write apps using VB6 (VS6). I want to write an application which should be running all the time (some machines are 98 and some are XP). Preferably, it can be minimized to the system tray when it is started (as an option).
Many thanks!
Answerthis is for advanced programmers only. do not attempt it if you are new.
in a .bas module
'------------------------------------------------------
' VB CENTER
'
' Tutorial: Using the system tray
' 09/26/98
'
' You may distribute this file as long as all the credits
' Are given to VB Center
'
' For more tutorials and a detailed explanation of this
' tutorial, visit our page:
http://vbcenter.cjb.net
'
'------------------------------------------------------
Declare Function Shell_NotifyIcon Lib "shell32.dll" (ByVal dwMessage As Long, lpData As NOTIFYICONDATA) As Long
'If you get an error message at the beginning of the program,
'use the declaration below:
'Declare Function Shell_NotifyIcon Lib "shell32.dll" Alias " Shell_NotifyIconA" (ByVal dwMessage As Long, lpData As NOTIFYICONDATA) As Long
'These three constants specify what you want to do
Public Const NIM_ADD = &H0
Public Const NIM_DELETE = &H2
Public Const NIM_MODIFY = &H1
Public Const NIF_ICON = &H2
Public Const NIF_MESSAGE = &H1
Public Const NIF_TIP = &H4
Public Const WM_LBUTTONDBLCLK = &H203
Public Const WM_LBUTTONDOWN = &H201
Public Const WM_LBUTTONUP = &H202
Public Const WM_MOUSEMOVE = &H200
Public Const WM_RBUTTONDBLCLK = &H206
Public Const WM_RBUTTONDOWN = &H204
Public Const WM_RBUTTONUP = &H205
Type NOTIFYICONDATA
cbSize As Long
hwnd As Long
uID As Long
uFlags As Long
uCallbackMessage As Long
hIcon As Long
szTip As String * 64
End Type
Public IconData As NOTIFYICONDATA
_______
in the form
Private Sub Form_Activate()
Call Shell_NotifyIcon(NIM_ADD, IconData)
'this is an option to show icon without minimizing the form
End Sub
Private Sub Form_Resize()
'this is the option to show icon on minimizing the form
If Me.WindowState = 1 Then
'The user has minimized his window
'Call Shell_NotifyIcon(NIM_ADD, IconData)
' Add the form's icon to the tray
Me.Hide
' Hide the button at the taskbar
End If
End Sub
Private Sub Form_Load()
With IconData
.cbSize = Len(IconData)
' The length of the NOTIFYICONDATA type
.hIcon = Me.Icon
' A reference to the form's icon
.hwnd = Me.hwnd
' hWnd of the form
.szTip = "My Tooltip" & Chr(0)
' Tooltip string delimited with a null character
.uCallbackMessage = WM_MOUSEMOVE
' The icon we're placing will send messages to the MouseMove event
.uFlags = NIF_ICON Or NIF_TIP Or NIF_MESSAGE
' It will have message handling and a tooltip
.uID = vbNull
' uID is not used by VB, so it's set to a Null value
End With
End Sub
Private Sub mnuExit_Click()
Unload Me
' Unload the form
End
' Just to be sure the program has ended
End Sub
Private Sub mnuShow_Click()
Me.WindowState = vbNormal
Shell_NotifyIcon NIM_DELETE, IconData
Me.Show
End Sub
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim Msg As Long
Msg = X
' The message is passed to the X value
' You must set your form's ScaleMode property to pixels in order to get the correct message
If Msg = WM_LBUTTONDBLCLK Then
' The user has double-clicked your icon
Call mnuShow_Click
' Show the window
ElseIf Msg = WM_RBUTTONDOWN Then
' Right-click
PopupMenu mnuPopup
' Popup the menu
End If
End Sub
Private Sub Form_Unload(Cancel As Integer)
Shell_NotifyIcon NIM_DELETE, IconData
End Sub
Ravindra M.G.