MS Access in Icon Tray?

M

Mauricio Silva

I have an application in MS Access that should be opened during all times for
the users be aware of new items that shows up. Is it possible to minimize it
to the Icon Tray? It just saves a lot of space in the users desktop.

Thanks
Mauricio Silva
 
P

Pieter Wijnen

I haven't tried it with MSAccess but it works for VB
Might lack a few WinAPI Calls


'--------------------- modSysTray ---------------------------
Option Explicit



'Required Public constants, types & declares
'for the Shell_Notify API method
Public Const NIM_ADD As Long = &H0
Public Const NIM_MODIFY As Long = &H1
Public Const NIM_DELETE As Long = &H2

Public Const NIF_ICON As Long = &H2 'adding an ICON
Public Const NIF_TIP As Long = &H4 'adding a TIP
Public Const NIF_MESSAGE As Long = &H1 'want return messages

'rodent constant we'll need for the callback
Public Const WM_LBUTTONDOWN As Long = &H201
Public Const WM_LBUTTONUP As Long = &H202
Public Const WM_LBUTTONDBLCLK As Long = &H203

Public Const WM_MBUTTONDOWN As Long = &H207
Public Const WM_MBUTTONUP As Long = &H208
Public Const WM_MBUTTONDBLCLK As Long = &H209

Public Const WM_RBUTTONDOWN As Long = &H204
Public Const WM_RBUTTONUP As Long = &H205
Public Const WM_RBUTTONDBLCLK As Long = &H206

'the actual workhorse
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 NID As NOTIFYICONDATA

Public Declare Function Shell_NotifyIcon Lib "shell32.dll" _
Alias "Shell_NotifyIconA" _
(ByVal dwMessage As Long, _
lpData As NOTIFYICONDATA) As Long

Public Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA"
_
(ByVal lpPrevWndFunc As Long, _
ByVal hwnd As Long, _
ByVal uMsg As Long, _
ByVal wParam As Long, _
ByVal lParam As Long) As Long

Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" _
(ByVal hwnd As Long, _
ByVal nIndex As Long, _
ByVal dwNewLong As Any) As Long

Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As
Long

'defWindowProc: Variable to hold the ID of the
' default window message processing
' procedure. Returned by SetWindowLong.
Public defWindowProc As Long

'isSubclassed: flag indicating that subclassing
' has been done. Provides the means
' to call the correct message-handler.
Public isSubclassed As Boolean


Public Function ShellTrayAdd() As Long

'prepare the NOTIFYICONDATA type with the
'required parameters:

'.cbSize: Size of this structure, in bytes.
'
'.hwnd: Handle of the window that will receive
' notification messages associated with
' an icon in the taskbar status area.
'
'uID: Application-defined identifier of
' the taskbar icon. In an application
' with a single tray icon, this can be
' an arbitrary number. For apps with
' multiple icons, each icon ID must be
' different as this member identifies
' which of the icons was selected.
'
'.uFlags: flags that indicate which of the other
' members contain valid data. This member
' can be a combination of the following:
' NIF_ICON hIcon member is valid.
' NIF_MESSAGE uCallbackMessage member is valid.
' NIF_TIP szTip member is valid.
'
'uCallbackMessage: Application-defined message identifier.
' The system uses this identifier for
' notification messages that it sends
' to the window identified in hWnd.
' These notifications are sent when a
' mouse event occurs in the bounding
' rectangle of the icon. (Note: 'callback'
' is a bit misused here (in the context of
' other callback demonstrations); there is
' no systray-specific callback defined -
' instead the form itself must be subclassed
' to respond to this message.
'
'hIcon: Handle to the icon to add, modify, or delete.
'
'szTip: Tooltip text to display for the icon. Must
' be terminated with a Chr$(0).

'Shell_NotifyIcon messages:
'dwMessage: Message value to send. This parameter
' can be one of these values:
' NIM_ADD Adds icon to status area
' NIM_DELETE Deletes icon from status area
' NIM_MODIFY Modifies icon in status area
'
'pnid: Address of the prepared NOTIFYICONDATA.
' The content of the structure depends
' on the value of dwMessage.

With NID
.cbSize = LenB(NID)
.hwnd = frmMain.hwnd 'must be changed for Access
.uID = 125& 'icon
.uFlags = NIF_ICON Or NIF_TIP Or NIF_MESSAGE
.uCallbackMessage = WM_MYHOOK
.hIcon = Me.Icon 'must be changed for Access
.szTip = AppTitle & Chr$(0)
End With

ShellTrayAdd = Shell_NotifyIcon(NIM_ADD, NID)

End Function

Public Sub ShellTrayRemove()

'Remove the icon from the taskbar
Call Shell_NotifyIcon(NIM_DELETE, NID)

End Sub


Private Sub UnSubClass()

'restore the default message handling
'before exiting
If defWindowProc Then
SetWindowLong frmMain.hwnd, GWL_WNDPROC, defWindowProc
defWindowProc = 0
End If

End Sub


Public Sub SubClass(hwnd As Long)

'assign our own window message
'procedure (WindowProc)

On Error Resume Next
defWindowProc = SetWindowLong(hwnd, GWL_WNDPROC, AddressOf WindowProc)

End Sub

Option Explicit


'our own window message procedure
Public Function WindowProc(ByVal hwnd As Long, _
ByVal uMsg As Long, _
ByVal wParam As Long, _
ByVal lParam As Long) As Long

'window message procedure
'
'If the handle returned is to our form,
'call a form-specific message handler to
'deal with the tray notifications. If it
'is a general system message, pass it on to
'the default window procedure.
'
'If its ours, we look at lParam for the
'message generated, and react appropriately.
On Error Resume Next

Select Case hwnd

'form-specific handler
Case frmMain.hwnd

Select Case uMsg
'check uMsg for the application-defined
'identifier (NID.uID) assigned to the
'systray icon in NOTIFYICONDATA (NID).

'WM_MYHOOK was defined as the message sent
'as the .uCallbackMessage member of
'NOTIFYICONDATA the systray icon
Case WM_MYHOOK

'lParam is the value of the message
'that generated the tray notification.
Select Case lParam
Case WM_RBUTTONUP:

'This assures that focus is restored to
'the form when the menu is closed. If the
'form is hidden, it (correctly) has no effect.
'frmMain.Visible = True
'frmMain.PopupMenu frmMain.zmnuSysTrayDemo
Call SetForegroundWindow(frmMain.hwnd)

'show the menu
frmMain.PopupMenu frmMain.zmnuSysTrayDemo
Case WM_LBUTTONUP:
Call SetForegroundWindow(frmMain.hwnd)
frmMain.RestoreIt
End Select

'handle any other form messages by
'passing to the default message proc
Case Else

WindowProc = CallWindowProc(defWindowProc, _
hwnd, _
uMsg, _
wParam, _
lParam)
Exit Function

End Select


'this takes care of messages when the
'handle specified is not that of the form
Case Else

WindowProc = CallWindowProc(defWindowProc, _
hwnd, _
uMsg, _
wParam, _
lParam)
End Select

End Function





'--------------------- modSysTray ---------------------------
'--end block--'
 
M

Mauricio Silva

Thank you. I'll give t a try.

Pieter Wijnen said:
I haven't tried it with MSAccess but it works for VB
Might lack a few WinAPI Calls


'--------------------- modSysTray ---------------------------
Option Explicit



'Required Public constants, types & declares
'for the Shell_Notify API method
Public Const NIM_ADD As Long = &H0
Public Const NIM_MODIFY As Long = &H1
Public Const NIM_DELETE As Long = &H2

Public Const NIF_ICON As Long = &H2 'adding an ICON
Public Const NIF_TIP As Long = &H4 'adding a TIP
Public Const NIF_MESSAGE As Long = &H1 'want return messages

'rodent constant we'll need for the callback
Public Const WM_LBUTTONDOWN As Long = &H201
Public Const WM_LBUTTONUP As Long = &H202
Public Const WM_LBUTTONDBLCLK As Long = &H203

Public Const WM_MBUTTONDOWN As Long = &H207
Public Const WM_MBUTTONUP As Long = &H208
Public Const WM_MBUTTONDBLCLK As Long = &H209

Public Const WM_RBUTTONDOWN As Long = &H204
Public Const WM_RBUTTONUP As Long = &H205
Public Const WM_RBUTTONDBLCLK As Long = &H206

'the actual workhorse
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 NID As NOTIFYICONDATA

Public Declare Function Shell_NotifyIcon Lib "shell32.dll" _
Alias "Shell_NotifyIconA" _
(ByVal dwMessage As Long, _
lpData As NOTIFYICONDATA) As Long

Public Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA"
_
(ByVal lpPrevWndFunc As Long, _
ByVal hwnd As Long, _
ByVal uMsg As Long, _
ByVal wParam As Long, _
ByVal lParam As Long) As Long

Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" _
(ByVal hwnd As Long, _
ByVal nIndex As Long, _
ByVal dwNewLong As Any) As Long

Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As
Long

'defWindowProc: Variable to hold the ID of the
' default window message processing
' procedure. Returned by SetWindowLong.
Public defWindowProc As Long

'isSubclassed: flag indicating that subclassing
' has been done. Provides the means
' to call the correct message-handler.
Public isSubclassed As Boolean


Public Function ShellTrayAdd() As Long

'prepare the NOTIFYICONDATA type with the
'required parameters:

'.cbSize: Size of this structure, in bytes.
'
'.hwnd: Handle of the window that will receive
' notification messages associated with
' an icon in the taskbar status area.
'
'uID: Application-defined identifier of
' the taskbar icon. In an application
' with a single tray icon, this can be
' an arbitrary number. For apps with
' multiple icons, each icon ID must be
' different as this member identifies
' which of the icons was selected.
'
'.uFlags: flags that indicate which of the other
' members contain valid data. This member
' can be a combination of the following:
' NIF_ICON hIcon member is valid.
' NIF_MESSAGE uCallbackMessage member is valid.
' NIF_TIP szTip member is valid.
'
'uCallbackMessage: Application-defined message identifier.
' The system uses this identifier for
' notification messages that it sends
' to the window identified in hWnd.
' These notifications are sent when a
' mouse event occurs in the bounding
' rectangle of the icon. (Note: 'callback'
' is a bit misused here (in the context of
' other callback demonstrations); there is
' no systray-specific callback defined -
' instead the form itself must be subclassed
' to respond to this message.
'
'hIcon: Handle to the icon to add, modify, or delete.
'
'szTip: Tooltip text to display for the icon. Must
' be terminated with a Chr$(0).

'Shell_NotifyIcon messages:
'dwMessage: Message value to send. This parameter
' can be one of these values:
' NIM_ADD Adds icon to status area
' NIM_DELETE Deletes icon from status area
' NIM_MODIFY Modifies icon in status area
'
'pnid: Address of the prepared NOTIFYICONDATA.
' The content of the structure depends
' on the value of dwMessage.

With NID
.cbSize = LenB(NID)
.hwnd = frmMain.hwnd 'must be changed for Access
.uID = 125& 'icon
.uFlags = NIF_ICON Or NIF_TIP Or NIF_MESSAGE
.uCallbackMessage = WM_MYHOOK
.hIcon = Me.Icon 'must be changed for Access
.szTip = AppTitle & Chr$(0)
End With

ShellTrayAdd = Shell_NotifyIcon(NIM_ADD, NID)

End Function

Public Sub ShellTrayRemove()

'Remove the icon from the taskbar
Call Shell_NotifyIcon(NIM_DELETE, NID)

End Sub


Private Sub UnSubClass()

'restore the default message handling
'before exiting
If defWindowProc Then
SetWindowLong frmMain.hwnd, GWL_WNDPROC, defWindowProc
defWindowProc = 0
End If

End Sub


Public Sub SubClass(hwnd As Long)

'assign our own window message
'procedure (WindowProc)

On Error Resume Next
defWindowProc = SetWindowLong(hwnd, GWL_WNDPROC, AddressOf WindowProc)

End Sub

Option Explicit


'our own window message procedure
Public Function WindowProc(ByVal hwnd As Long, _
ByVal uMsg As Long, _
ByVal wParam As Long, _
ByVal lParam As Long) As Long

'window message procedure
'
'If the handle returned is to our form,
'call a form-specific message handler to
'deal with the tray notifications. If it
'is a general system message, pass it on to
'the default window procedure.
'
'If its ours, we look at lParam for the
'message generated, and react appropriately.
On Error Resume Next

Select Case hwnd

'form-specific handler
Case frmMain.hwnd

Select Case uMsg
'check uMsg for the application-defined
'identifier (NID.uID) assigned to the
'systray icon in NOTIFYICONDATA (NID).

'WM_MYHOOK was defined as the message sent
'as the .uCallbackMessage member of
'NOTIFYICONDATA the systray icon
Case WM_MYHOOK

'lParam is the value of the message
'that generated the tray notification.
Select Case lParam
Case WM_RBUTTONUP:

'This assures that focus is restored to
'the form when the menu is closed. If the
'form is hidden, it (correctly) has no effect.
'frmMain.Visible = True
'frmMain.PopupMenu frmMain.zmnuSysTrayDemo
Call SetForegroundWindow(frmMain.hwnd)

'show the menu
frmMain.PopupMenu frmMain.zmnuSysTrayDemo
Case WM_LBUTTONUP:
Call SetForegroundWindow(frmMain.hwnd)
frmMain.RestoreIt
End Select

'handle any other form messages by
'passing to the default message proc
Case Else

WindowProc = CallWindowProc(defWindowProc, _
hwnd, _
uMsg, _
wParam, _
lParam)
Exit Function

End Select


'this takes care of messages when the
'handle specified is not that of the form
Case Else

WindowProc = CallWindowProc(defWindowProc, _
hwnd, _
uMsg, _
wParam, _
lParam)
End Select

End Function





'--------------------- modSysTray ---------------------------
'--end block--'
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top