Minimizing An Application To The System Tray
I have this module for creating an icon in the system tray for my application:
Quote:
Option Explicit
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
'The following constants are the messages sent to the
'Shell_NotifyIcon function to add, modify, or delete an icon
'from the taskbar status area.
Const NIM_ADD = &H0
Const NIM_MODIFY = &H1
Const NIM_DELETE = &H2
'The following constant is the message sent when a mouse event
'occurs within the rectangular boundaries of the icon in the
'taskbar status area.
Const WM_MOUSEMOVE = &H200
'The following constants are the flags that indicate the valid
'members of the NOTIFYICONDATA data type.
Const NIF_MESSAGE = &H1
Const NIF_ICON = &H2
Const NIF_TIP = &H4
'The following constants are used to determine the mouse input
'on the the icon in the taskbar status area.
'Left-click constants.
Const WM_LBUTTONDBLCLK = &H203 'Double-click
Const WM_LBUTTONDOWN = &H201 'Button down
Const WM_LBUTTONUP = &H202 'Button up
'Right-click constants.
Const WM_RBUTTONDBLCLK = &H206 'Double-click
Const WM_RBUTTONDOWN = &H204 'Button down
Const WM_RBUTTONUP = &H205 'Button up
'Declare the API function call.
Declare Function Shell_NotifyIcon Lib "shell32" Alias _
"Shell_NotifyIconA" (ByVal dwMessage As Long, _
pnid As NOTIFYICONDATA) As Boolean
'Dimension a variable as the user-defined data type.
Dim nid As NOTIFYICONDATA
Public Sub AddTrayIcon(hWnd As Long, hIcon As Long, sTTText As String)
'As the name indicates, this Sub adds an icon to the System Teay.
'Parameters- hWnd: Handle to the form the icon is attached to,
' normally the main form of the application.
' hIcon: Handle to the icon to display. This can
' be the Icon property of a form, or the Picture
' property of an Image control or PictureBox.
' sTTText: The ToolTop text to display when the
' mouse hovers over the icon in the Tray.
nid.cbSize = Len(nid)
nid.hWnd = hWnd
nid.uId = vbNull
nid.uFlags = NIF_ICON Or NIF_TIP Or NIF_MESSAGE
nid.uCallBackMessage = WM_MOUSEMOVE
nid.hIcon = hIcon
nid.szTip = sTTText & vbNullChar
'Call the Shell_NotifyIcon function to add the icon to the taskbar
'status area.
Shell_NotifyIcon NIM_ADD, nid
End Sub
Public Sub RemoveTrayIcon()
'This Sub assumes the "nid" variable is still loaded with
'values from the AddTrayIcon Sub, which will ordinarily be true.
Shell_NotifyIcon NIM_DELETE, nid
End Sub
Public Sub TrayEvent(frmApp As Form, mnuPop As Menu, fXCoord As Single)
'In this example, we are only resonding to double-clicking
'the left button on the Tray Icon, or right-clicking it. If
'you want to respond to other mouse events (like right button
'double-clicks), put code in the relevant cases.
'Parameters- frmApp: A reference to the form calling this code.
' mnuPop: A reference to the PopUpMenu to display.
' fXCoord: The X parameter from the MouseMove event
' calling this code.
Dim lMsg As Long
lMsg = fXCoord / Screen.TwipsPerPixelX
Select Case lMsg
Case WM_LBUTTONDOWN
Case WM_LBUTTONUP
Case WM_LBUTTONDBLCLK 'Show the main form of the app on
frmApp.Show 'the left double-click event.
Case WM_RBUTTONDOWN 'Show the PopUp menu on the right
frmApp.PopupMenu mnuPop 'click event.
Case WM_RBUTTONUP
Case WM_RBUTTONDBLCLK
End Select
End Sub
Public Sub ChangeTrayIcon(hIcon As Long)
'This Sub changes the icon associated with the Tray Icon. It
'assumes the nid variable is still loaded from the AddTrayIcon Sub.
'Parameter - hIcon: Long handle to the icon to be used.
nid.hIcon = hIcon
Shell_NotifyIcon NIM_MODIFY, nid
End Sub
Adding this to the Form_Load event adds the icon into the tray:
Quote:
AddTrayIcon Me.hWnd, Me.Icon, Me.Caption
BUT, here is my problem. I am nearly sure this is the module I used when I added in code to minimize my application to the system tray icon so it's not on the taskbar, but I have forgotten the code. To clarify, I want to minimize my app to the system tray by clicking the minimize button on the form : _ [ ] X
Thanks all in advance. :)