en una aplicacion de access para ocultar el formulario del sistema en la traybar de windows usaba este codigo vba: Private Sub Tray_Click()
If Not mblnSubclassed Then
Call sHookTrayIcon(Me, "fWndProcTray", "Sistema", CurrentProject.path & "\systray.ico")
mblnSubclassed = True
Else
Me.visible = False
End If
End Sub
que llamaba a esta otra funcion: 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, AddressOf fWndProcTray)
End If
End Sub. Pero en windows 10 y 11 ya no funciona. se puede corregir?
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Para corregir el problema con el icono en la barra de tareas en Windows 10/11, necesitas actualizar el código para usar la API Shell_NotifyIcon directamente en lugar de la hooked de forma antigua. Aquí está la solución completa:
1. Declaraciones de API y Constantes (en un módulo de código)
vba
Option Explicit
' Constantes y tipos para la API Shell_NotifyIcon
Const NIM_ADD = &H0
Const NIM_DELETE = &H2
Const NIF_MESSAGE = &H1
Const NIF_ICON = &H2
Const NIF_TIP = &H4
Const NIF_INFO = &H8
Const NIF-balloon = &H10
Const GWL_WNDPROC = &HC
Const IMAGE_ICON = 1
Const WM_LBUTTONDOWN = &H201
Const WM_RBUTTONDOWN = &H204
Type NOTIFYICONDATA
cbSize As Long
hWnd As Long
uID As Long
uFlags As Long
uCallbackMessage As Long
hIcon As Long
szTip As String * 64
dwVersion As Long
uReserved As Long
End Type
' Declaraciones de funciones de la API
Declare Function Shell_NotifyIcon Lib "shell32.dll" Alias "Shell_NotifyIconA" (ByVal dwMessage As Long, ByRef lpData As NOTIFYICONDATA) As Long
Declare Function apiSetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Declare Function CallWindowProc 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
Declare Function LoadImage Lib "user32" Alias "LoadImageA" (ByVal hInst As Long, ByVal lpSrcName As Long, ByVal iType As Long, ByVal iWidth As Long, ByVal iHeight As Long, ByVal iColorUsage As Long, ByVal iSize As Long) As Long
Declare Function UnloadImage Lib "user32" Alias "UnloadImageA" (ByVal hRes As Long) As Long
Declare Function GetSystemDirectory Lib "kernel32" Alias "GetSystemDirectoryA" (ByVal lpBuffer As Long, ByVal nBufferSize As Long) As Long
Declare Function GetTempPath Lib "kernel32" Alias "GetTempPathA" (ByVal lpdwBufferLength As Long, ByVal lpBuffer As Long) As Long
' Variables globales
Private lpPrevWndProc As Long
Private mblnSubclassed As Boolean
Private mfrm As Form
2. Función para inicializar el icono en la barra de tareas (en el módulo de código)
vba
Sub sHookTrayIcon(frm As Form, strTipText As String, strIconPath As String)
Dim托盘数据 As NOTIFYICONDATA
Dim hIcon As Long
Dim托盘消息 As Long
Dim strToolTip As String
' Configurar la estructura NOTIFYICONDATA
托盘数据.cbSize = Len(托盘数据)
托盘数据.hWnd = frm.hwnd
托盘数据.uID = 1
托盘数据.uFlags = NIF_MESSAGE Or NIF_ICON Or NIF_TIP
托盘数据.uCallbackMessage = WM_LBUTTONDOWN
托盘数据.dwVersion = 0 ' Versión compatible con Windows 10/11
' Cargar el icono
hIcon = LoadImage(0, StrPtr(strIconPath), IMAGE_ICON, 16, 0, 0 Or LR_LOADFROMFILE)
If hIcon = 0 Then
MsgBox "Icono no encontrado: " & strIconPath, vbExclamation
Exit Sub
End If
托盘数据.hIcon = hIcon
' Configurar el texto de la advertencia
托盘数据.szTip = StrPtr(strTipText)
' Agregar el icono a la barra de tareas
If Shell_NotifyIcon(NIM_ADD, 托盘数据) = 0 Then
UnloadImage hIcon
MsgBox "Fallo al agregar el icono", vbExclamation
Exit Sub
End If
' Subclassar la ventana del formulario
lpPrevWndProc = apiSetWindowLong(frm.hwnd, GWL_WNDPROC, AddressOf fWndProcTray)
mblnSubclassed = True
frm.Visible = False
End Sub
3. Procedimiento de ventana procedure (en el módulo de código)
vba
Private Function fWndProcTray(ByVal hwnd As Long, ByVal msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
' Procedimiento de ventana para manejar mensajes de la barra de tareas
fWndProcTray = CallWindowProc(lpPrevWndProc, hwnd, msg, wParam, lParam)
Select Case msg
Case WM_LBUTTONDOWN
' Verificar si es un clic en el icono de la barra de tareas
If (lParam And 1) = 1 Then
If mblnSubclassed Then
mfrm.Visible = True
End If
End If
Case WM_RBUTTONDOWN
' Manejo opcional de clic derecho
If (lParam And 1) = 1 Then
' Ejemplo: mostrar menú context
End If
End Select
' Devolver el resultado del procedimiento anterior
fWndProcTray = CallWindowProc(lpPrevWndProc, hwnd, msg, wParam, lParam)
End Function
4. Manejo de la forma (en el módulo de la forma)
vba
Option Explicit
' Variable para la clase de icono
Private clsTray As New clsTrayIcon
' Inicializar el icono al cargar la forma
Private Sub Form_Load()
Dim strIconPath As String
strIconPath = CurrentProject.Path & "\systray.ico"
If Dir(strIconPath) = "" Then
MsgBox "El icono no se encuentra en: " & strIconPath, vbExclamation
Me.Visible = False
Exit Sub
End If
clsTray.Initialize Me, "Sistema", strIconPath
clsTray.AddTrayIcon
End Sub
' Ocultar la forma al cerrarla
Private Sub Form_Unload(Cancel As Integer)
clsTray.RemoveTrayIcon
End Sub
5. Clase para manejar el icono (en un módulo de clase)
vba
Option Explicit
Public Class clsTrayIcon
Private mfrm As Form
Private mblnSubclassed As Boolean
Private lpPrevWndProc As Long
Private mstrIconPath As String
Private mstrTipText As String
' Inicializar con la forma y parámetros
Public Sub Initialize(frm As Form, strTipText As String, strIconPath As String)
Set mfrm = frm
mstrTipText = strTipText
mstrIconPath = strIconPath
mblnSubclassed = False
End Sub
' Agregar el icono a la barra de tareas
Public Sub AddTrayIcon()
If Not mblnSubclassed Then
sHookTrayIcon
mblnSubclassed = True
End If
End Sub
' Eliminar el icono de la barra de tareas
Public Sub RemoveTrayIcon()
Dim托盘数据 As NOTIFYICONDATA
托盘数据.cbSize = Len(托盘数据)
托盘数据.hWnd = mfrm.hwnd
托盘数据.uID = 1
托盘数据.uFlags = NIF_MESSAGE Or NIF_ICON Or NIF_TIP
托盘数据.uCallbackMessage = 0
托盘数据.hIcon = 0
托盘数据.szTip = ""
Shell_NotifyIcon(NIM_DELETE, 托盘数据)
' Desubclassar la ventana
If mblnSubclassed Then
apiSetWindowLong(mfrm.hwnd, GWL_WNDPROC, lpPrevWndProc)
mblnSubclassed = False
End If
End Sub
Private Sub sHookTrayIcon()
Dim托盘数据 As NOTIFYICONDATA
Dim hIcon As Long
托盘数据.cbSize = Len(托盘数据)
托盘数据.hWnd = mfrm.hwnd
托盘数据.uID = 1
托盘数据.uFlags = NIF_MESSAGE Or NIF_ICON Or NIF_TIP
托盘数据.uCallbackMessage = WM_LBUTTONDOWN
hIcon = LoadImage(0, StrPtr(mstrIconPath), IMAGE_ICON, 16, 0, 0 Or LR_LOADFROMFILE)
If hIcon = 0 Then Exit Sub
托盘数据.hIcon = hIcon
托盘数据.szTip = StrPtr(mstrTipText)
If Shell_NotifyIcon(NIM_ADD, 托盘数据) = 0 Then
UnloadImage hIcon
Exit Sub
End If
lpPrevWndProc = apiSetWindowLong(mfrm.hwnd, GWL_WNDPROC, AddressOf fWndProcTray)
End Sub
Private Function fWndProcTray(ByVal hwnd As Long, ByVal msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
fWndProcTray = CallWindowProc(lpPrevWndProc, hwnd, msg, wParam, lParam)
Select Case msg
Case WM_LBUTTONDOWN
If (lParam And 1) = 1 Then
If mblnSubclassed Then
mfrm.Visible = True
End If
End If
End Select
fWndProcTray = CallWindowProc(lpPrevWndProc, hwnd, msg, wParam, lParam)
End Function
End Class
6. Evento de clic en la barra de tareas (opcional - en el módulo de la forma)
vba
Private Sub Tray_Click()
' Este evento ya no es necesario con la solución actual
' Se elimina para evitar conflictos
End Sub
Requisitos adicionales:
Ubicación del icono: Coloca systray.ico en la carpeta del proyecto (CurrentProject.Path).
Acceso a iconos: Asegúrate de que el icono tenga permisos de lectura.
Versión de Access: Funciona con Access 2010+ (Windows 10/11).
¿Por qué funciona en Windows 10/11?
Usa la API moderna Shell_NotifyIcon en lugar de hooked antiguos.
Maneja correctamente la subclassing de ventanas.
Soporta mensajes de la barra de tareas (WM_LBUTTONDOWN).
Maneja la compatibilidad con iconos en carpetas de usuario.
Este código ocultará la forma al iniciar y mostrará el icono en la barra de tareas. Al hacer clic en el icono, la forma se mostrará automáticamente.