|
APIs: Sous
classement pour fonctionnalité du SysTray |
Author(s) Dev Ashish |
|
Sous-classement pour fonctionnalité du SysTray
(NDLT: Le SysTray est la portion droite, en bas de l'écran, sur laquelle on retrouve par défaut l'heure et autres icônes
particuliers, tel que celui de votre anti-virus, de MS SQL Server, etc. )
Afficher des icônes de tâches dans le TaskBar est un des traits
caractéristiques de Windows 98, Windows 95, et Windows NT 4.0. Pour manipuler un icône
dans le Taskbar Status Area, on peut utiliser la fonction de l'API de Windows Shell_NotifyIcon
du dll Shell32. Cette fonction nous permet d'ajouter, de modifier, d'effacer,
d'ajouter un ToolTip et d'envoyer un message (callback) pour exécuter un événement pour
la souris.
Cependant, puisque les formulaires d'Access sont déjà fortement
sous classés, il est nécessaire d'écrire du code supplémentaire pour réagir avec
succès aux messages provenant du System Tray. Prendre note que la technique montrée ici
n'est ni documentée, ni supportée, dans l'environnement de Microsoft Office 97.
Note: Pour tester le code de cet article, vous
devez avoir également le code pour AddressOf.
Créer un nouveau formulaire avec deux boutons, cmdStartDemo
et cmdEndDemo, puis ajouter le code suivant dans le module de ce
formulaire..
Private mblnSubclassed As Boolean
Private Sub cmdEndDemo_Click()
Call sUnhookTrayIcon(Me)
mblnSubclassed = False
End Sub
Private Sub cmdStartDemo_Click()
If Not mblnSubclassed Then
Call sHookTrayIcon(Me, "fWndProcTray", "Hello World")
mblnSubclassed = True
Else
Me.Visible = False
End If
End Sub
Private Sub Form_Close()
If mblnSubclassed Then
Call sUnhookTrayIcon(Me)
End If
End Sub
Create a new module and paste this code in it.
Private Const WM_GETICON = &H7F
Private Const WM_SETICON = &H80
Private Const IMAGE_BITMAP = 0
Private Const IMAGE_ICON = 1
Private Const IMAGE_CURSOR = 2
Private Const LR_LOADFROMFILE = &H10
Private Const ICON_SMALL = 0&
Private Const ICON_BIG = 1&
Private Declare Function apiLoadImage Lib "user32" _
Alias "LoadImageA" _
(ByVal hInst As Long, _
ByVal lpszName As String, _
ByVal uType As Long, _
ByVal cxDesired As Long, _
ByVal cyDesired As Long, _
ByVal fuLoad As Long) _
As Long
Private Declare Function apiSendMessageLong Lib "user32" _
Alias "SendMessageA" _
(ByVal hWnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
ByVal lParam As Long) _
As Long
Private Const SHGFI_ICON = &H100
Private Const SHGFI_DISPLAYNAME = &H200
Private Const SHGFI_TYPENAME = &H400
Private Const SHGFI_ATTRIBUTES = &H800
Private Const SHGFI_ICONLOCATION = &H1000
Private Const SHGFI_EXETYPE = &H2000
Private Const SHGFI_SYSICONINDEX = &H4000
Private Const SHGFI_LINKOVERLAY = &H8000 e
Private Const SHGFI_SELECTED = &H10000
Private Const SHGFI_ATTR_SPECIFIED = &H20000
Private Const SHGFI_LARGEICON = &H0
Private Const SHGFI_SMALLICON = &H1
Private Const SHGFI_OPENICON = &H2
Private Const SHGFI_SHELLICONSIZE = &H4
Private Const SHGFI_PIDL = &H8
Private Const SHGFI_USEFILEATTRIBUTES = &H10
Private Const FILE_ATTRIBUTE_NORMAL = &H80
Private Const MAX_PATH = 260
Private Type SHFILEINFO
hIcon As Long
iIcon As Long
dwAttributes As Long
szDisplayName As String * MAX_PATH
szTypeName As String * 80
End Type
Private Declare Function apiSHGetFileInfo Lib "shell32.dll" _
Alias "SHGetFileInfoA" _
(ByVal pszPath As String, _
ByVal dwFileAttributes As Long, _
psfi As SHFILEINFO, _
ByVal cbSizeFileInfo As Long, _
ByVal uFlags As Long) _
As Long
Private Declare Function apiDestroyIcon Lib "user32" _
Alias "DestroyIcon" _
(ByVal hIcon As Long) _
As Long
Private psfi As SHFILEINFO
Private Const SW_HIDE = 0
Private Const SW_SHOWNORMAL = 1
Private Const SW_SHOWMINIMIZED = 2
Private Const SW_SHOWMAXIMIZED = 3
Private Declare Function apiShowWindow Lib "user32" _
Alias "ShowWindow" _
(ByVal hWnd As Long, _
ByVal nCmdShow As Long) _
As Long
Private Const NIM_ADD As Long = &H0
Private Const NIM_MODIFY As Long = &H1
Private Const NIM_DELETE As Long = &H2
Private Const NIF_TIP As Long = &H4
Private Const NIF_MESSAGE As Long = &H1
Private Const NIF_ICON As Long = &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 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
Private Declare Function apiShellNotifyIcon Lib "shell32.dll" _
Alias "Shell_NotifyIconA" _
(ByVal dwMessage As Long, _
lpData As NOTIFYICONDATA) _
As Long
Private Declare Function apiCallWindowProc Lib "user32" _
Alias "CallWindowProcA" _
(ByVal lpPrevWndFunc As Long, _
ByVal hWnd As Long, _
ByVal msg As Long, _
ByVal wParam As Long, _
ByVal lParam As Long) _
As Long
Private Declare Function apiSetWindowLong Lib "user32" _
Alias "SetWindowLongA" _
(ByVal hWnd As Long, _
ByVal nIndex As Long, _
ByVal wNewWord As Long) _
As Long
Private nID As NOTIFYICONDATA
Private lpPrevWndProc As Long
Private mblnCustomIcon As Boolean
Private Const GWL_WNDPROC As Long = (-4)
Function fWndProcTray(ByVal hWnd As Long, _
ByVal uMessage As Long, _
ByVal wParam As Long, _
ByVal lParam As Long) _
As Long
On Error Resume Next
Select Case lParam
Case WM_LBUTTONUP:
Call apiShowWindow(hWnd, SW_SHOWNORMAL)
Case WM_LBUTTONDBLCLK:
Call apiShowWindow(hWnd, SW_SHOWNORMAL)
Case WM_LBUTTONDOWN:
Case WM_RBUTTONDBLCLK:
Case WM_RBUTTONDOWN:
Case WM_RBUTTONUP:
Call apiShowWindow(hWnd, SW_SHOWNORMAL)
End Select
fWndProcTray = apiCallWindowProc( _
ByVal lpPrevWndProc, _
ByVal hWnd, _
ByVal uMessage, _
ByVal wParam, _
ByVal lParam)
End Function
Sub sHookTrayIcon(frm As Form, _
strFunction As String, _
Optional strTipText As String, _
Optional strIconPath As String)
If fInitTrayIcon(frm, strTipText, strIconPath) Then
frm.Visible = False
lpPrevWndProc = apiSetWindowLong(frm.hWnd, _
GWL_WNDPROC, _
AddrOf(strFunction))
End If
End Sub
Sub sUnhookTrayIcon(frm As Form)
Call apiSetWindowLong(frm.hWnd, _
GWL_WNDPROC, _
lpPrevWndProc)
Call apiShellNotifyIcon(NIM_DELETE, nID)
If mblnCustomIcon Then
Call fRestoreIcon(frm.hWnd)
End If
Call apiDestroyIcon(psfi.hIcon)
End Sub
Private Function fExtractIcon() As Long
On Error GoTo ErrHandler
Dim hIcon As Long
hIcon = apiSHGetFileInfo(".MAF", FILE_ATTRIBUTE_NORMAL, _
psfi, LenB(psfi), _
SHGFI_USEFILEATTRIBUTES Or _
SHGFI_SMALLICON Or SHGFI_ICON)
If Not hIcon = 0 Then fExtractIcon = psfi.hIcon
ExitHere:
Exit Function
ErrHandler:
fExtractIcon = False
Resume ExitHere
End Function
Private Function fRestoreIcon(hWnd As Long)
Call apiSendMessageLong(hWnd, WM_SETICON, 0&, fExtractIcon())
End Function
Private Function fSetIcon(frm As Form, strIconPath As String) As Long
Dim hIcon As Long
hIcon = apiLoadImage(0&, strIconPath, IMAGE_ICON, 16&, 16&, LR_LOADFROMFILE)
If hIcon Then
Call apiSendMessageLong(frm.hWnd, WM_SETICON, 0&, hIcon&)
mblnCustomIcon = True
fSetIcon = hIcon
End If
End Function
Private Function fInitTrayIcon(frm As Form, strTipText As String, strIconPath As String) As Boolean
Dim hIcon As Long
If strTipText = vbNullString Then strTipText = "MSAccess Form"
If (strIconPath = vbNullString) Or (Dir(strIconPath) = vbNullString) Then
hIcon = fExtractIcon()
Else
hIcon = fSetIcon(frm, strIconPath)
End If
If hIcon Then
With nID
.cbSize = LenB(nID)
.hWnd = frm.hWnd
.uID = vbNull
.uFlags = NIF_ICON Or NIF_TIP Or NIF_MESSAGE
.uCallbackMessage = WM_MOUSEMOVE
.hIcon = hIcon
.szTip = strTipText & vbNullChar
End With
Call apiShellNotifyIcon(NIM_ADD, nID)
fInitTrayIcon = True
End If
End Function
|