Cómo usar la bandeja de sistema directamente desde Visual Basic
Resumen
En este artículo muestra cómo aprovechar completamente la bandeja de sistema de Windows o área de notificación de Barra de tareas que utiliza Visual Basic. Coloca un icono de su elección en el área de notificación de Barra de tareas que mostrará una información sobre herramientas de su elección cuando el mouse se sitúa por encima de ello, restaurará la aplicación cuando se hace clic en la aplicación y mostrará un menú emergente cuando hecho clic con el botón secundario. Esto es todo posible a causa de la capacidad de Visual Basic de controlar directamente devoluciones de llamada, se exporta por tanto, tomar aquél a todo el partido de la función Shell_NotifyIcon mediante Shell32.dll.
1. |
Escriba el código siguiente en la sección de declaraciones de un módulo estándar de su proyecto: |
'user defined type required by Shell_NotifyIcon API call Public 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 'constants required by Shell_NotifyIcon API call: Public Const NIM_ADD = &H0 Public Const NIM_MODIFY = &H1 Public Const NIM_DELETE = &H2 Public Const NIF_MESSAGE = &H1 Public Const NIF_ICON = &H2 Public Const NIF_TIP = &H4 Public Const WM_MOUSEMOVE = &H200 Public Const WM_LBUTTONDOWN = &H201 'Button down Public Const WM_LBUTTONUP = &H202 'Button up Public Const WM_LBUTTONDBLCLK = &H203 'Double-click Public Const WM_RBUTTONDOWN = &H204 'Button down Public Const WM_RBUTTONUP = &H205 'Button up Public Const WM_RBUTTONDBLCLK = &H206 'Double-click Public Declare Function SetForegroundWindow Lib "user32" _ (ByVal hwnd As Long) As Long Public Declare Function Shell_NotifyIcon Lib "shell32" _ Alias "Shell_NotifyIconA" _ (ByVal dwMessage As Long, pnid As NOTIFYICONDATA) As Boolean Public nid As NOTIFYICONDATA |
2. |
Agregue el código siguiente a cualquier formulario de su proyecto que desea responder al Icono de Bandeja de sistema o Icono de Notificación para su aplicación: |
Private Sub Form_Load() 'the form must be fully visible before calling Shell_NotifyIcon Me.Show Me.Refresh With nid .cbSize = Len(nid) .hwnd = Me.hwnd .uId = vbNull .uFlags = NIF_ICON Or NIF_TIP Or NIF_MESSAGE .uCallBackMessage = WM_MOUSEMOVE .hIcon = Me.Icon .szTip = "Your ToolTip" & vbNullChar End With Shell_NotifyIcon NIM_ADD, nid End Sub Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single) 'this procedure receives the callbacks from the System Tray icon. Dim Result As Long Dim msg As Long 'the value of X will vary depending upon the scalemode setting If Me.ScaleMode = vbPixels Then msg = X Else msg = X / Screen.TwipsPerPixelX End If Select Case msg Case WM_LBUTTONUP '514 restore form window Me.WindowState = vbNormal Result = SetForegroundWindow(Me.hwnd) Me.Show Case WM_LBUTTONDBLCLK '515 restore form window Me.WindowState = vbNormal Result = SetForegroundWindow(Me.hwnd) Me.Show Case WM_RBUTTONUP '517 display popup menu Result = SetForegroundWindow(Me.hwnd) Me.PopupMenu Me.mPopupSys End Select End Sub Private Sub Form_Resize() 'this is necessary to assure that the minimized window is hidden If Me.WindowState = vbMinimized Then Me.Hide End Sub Private Sub Form_Unload(Cancel As Integer) 'this removes the icon from the system tray Shell_NotifyIcon NIM_DELETE, nid End Sub Private Sub mPopExit_Click() 'called when user clicks the popup menu Exit command Unload Me End Sub Private Sub mPopRestore_Click() 'called when the user clicks the popup menu Restore command Dim Result As Long Me.WindowState = vbNormal Result = SetForegroundWindow(Me.hwnd) Me.Show End Sub |
3. |
Realice las Configuraciones de Propiedad siguientes en el mismo formulario de agregar el código anterior al formulario: |
Caption |
Nombre |
Enabled |
Visible |
Position |
&SysTray |
mPopupSys |
True |
False |
Main |
&Restaurar |
mPopRestore |
True |
True |
Sub |
&Salir |
mPopExit |
True |
True |
Sub |
Propiedades del formulario:
ShownInTaskbar = False
Minbutton = True
Puede agregar elemento de menú adicional según es necesario.
Flexibilidad de área de notificación de barra de tareas
Puede modificar la información sobre herramientas que aparece sobre el icono de Notificación cambiando el ToolTip la línea siguiente del procedimiento Form_Load:
.szTip = "Your ToolTip" & vbNullChar |
Reemplace "ToolTip Su" por el texto que desea mostrar.
Puede modificar el Icono que aparece en el área de notificación
de Barra de tareas cambiando la línea siguiente del procedimiento Form_Load:
.hIcon = Me.Icon |
Reemplace Me.Icon con cualquier Icono en su proyecto.
Puede cambiar cualquiera de las configuraciones área de notificación
de Barra de tareas en cualquier momento después del uso de la constante
NIM_ADD volver a asignar los valores de la variable nid y utilizando después
la variación siguiente de llamar a API de Shell_NotifyIcon:
Shell_NotifyIcon NIM_MODIFY, nid. |
Si desea que un formulario diferente lo reciba a usted, necesitará que usar "NIM_Delete nid Shell_NotifyIcon" después de refill el nid como la función NIM_Modify no aceptará un Hwnd nuevo o necesitará que el Callback entonces usted agreguen otro Icono al systray para el formulario nuevo que utiliza "Shell_NotifyIcon NIM_ADD, nid" sin embargo escriba con los formularios nuevos Hwnd para primero eliminar el icono actual. También puede declarar copias independientes del tipo nid como ha activado cada formulario que desea mostrar un icono en la bandeja de sistema de Windows y cambiarlos en cada formulario evento que utiliza la secuencia NIM_DELETE y NIM_ADD.
top.updatecursor(); function ToggleBar() { if(parent.MenuBar.document.readyState == "complete") { if(top.FramePage.MenuBar.ButtonStatus["opendraft"] != "Off") { top.FramePage.MenuBar.ButtonToggle("opendraft", "Off"); top.FramePage.MenuBar.ButtonToggle("reply", "On"); top.FramePage.MenuBar.ButtonToggle("replyall", "On"); top.FramePage.MenuBar.ButtonToggle("forward", "On"); } } else { setTimeout("ToggleBar()",400) } }