home *** CD-ROM | disk | FTP | other *** search
/ Planet Source Code Jumbo …e CD Visual Basic 1 to 7 / 1_2002.ISO / Data / Zips / CODE_UPLOAD93018252000.psc / mSysTrayAPI.bas < prev    next >
Encoding:
BASIC Source File  |  2000-08-25  |  8.0 KB  |  221 lines

  1. Attribute VB_Name = "mSysTrayAPI"
  2. Option Explicit
  3.  
  4. ''-------------------------------------------------------------------------------------
  5. ''Application defined variables
  6. ''-------------------------------------------------------------------------------------
  7. Public frm As Form
  8. Public IconObject As Object
  9. Public lngPrevWndProc As Long 'Original WNDPROC address.
  10. Public lngWndID As Long 'Our unique icon identifier.
  11. Public lngHwnd As Long 'The hwnd of frmTray.
  12. Public Notify As NOTIFYICONDATA
  13. Public BarData As APPBARDATA
  14. ''-------------------------------------------------------------------------------------
  15. ''Application defined enumerations
  16. ''-------------------------------------------------------------------------------------
  17. Public Enum ZoomTypes
  18.     ZOOM_FROM_TRAY
  19.     ZOOM_TO_TRAY
  20. End Enum
  21.  
  22. ''-------------------------------------------------------------------------------------
  23. ''WIN32 structures
  24. ''-------------------------------------------------------------------------------------
  25. Type RECT
  26.         Left As Long
  27.         Top As Long
  28.         Right As Long
  29.         Bottom As Long
  30. End Type
  31.  
  32. Type NOTIFYICONDATA
  33.         cbSize As Long
  34.         hwnd As Long
  35.         uID As Long
  36.         uFlags As Long
  37.         uCallbackMessage As Long
  38.         hIcon As Long
  39.         szTip As String * 64
  40. End Type
  41.  
  42. Type APPBARDATA
  43.         cbSize As Long
  44.         hwnd As Long
  45.         uCallbackMessage As Long
  46.         uEdge As Long
  47.         rc As RECT
  48.         lParam As Long
  49. End Type
  50. ''-------------------------------------------------------------------------------------
  51. ''WIN32 API Constants
  52. ''-------------------------------------------------------------------------------------
  53. Public Const GW_CHILD = 5
  54. Public Const GW_HWNDFIRST = 0
  55. Public Const GW_HWNDLAST = 1
  56. Public Const GW_HWNDNEXT = 2
  57. Public Const GW_HWNDPREV = 3
  58. Public Const GWL_WNDPROC = (-4)
  59. Public Const IDANI_OPEN = &H1
  60. Public Const IDANI_CLOSE = &H2
  61. Public Const IDANI_CAPTION = &H3
  62. Public Const NIF_TIP = &H4
  63. Public Const NIM_ADD = 0&
  64. Public Const NIM_DELETE = 2&
  65. Public Const NIM_MODIFY = 1&
  66. Public Const NIF_ICON = 2&
  67. Public Const NIF_MESSAGE = 1&
  68. Public Const ABM_GETTASKBARPOS = &H5&
  69. Public Const WM_LBUTTONDBLCLK = &H203
  70. Public Const WM_MOUSEMOVE = &H200
  71. Public Const WM_RBUTTONDBLCLK = &H206
  72. Public Const WM_RBUTTONDOWN = &H204
  73. Public Const WM_RBUTTONUP = &H205
  74. Public Const WM_USER = &H400
  75.  
  76. ''-------------------------------------------------------------------------------------
  77. ''WIN32 DLL Declares
  78. ''-------------------------------------------------------------------------------------
  79. Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, _
  80.     ByVal lpWindowName As String) As Long
  81.  
  82. Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, _
  83.     ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
  84.  
  85. Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
  86.  
  87. Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
  88.  
  89. Declare Function DrawAnimatedRects Lib "user32" (ByVal hwnd As Long, ByVal idAni As Long, lprcFrom As RECT, _
  90.     lprcTo As RECT) As Long
  91.  
  92. Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, _
  93.     ByVal hwnd As Long, ByVal Msg As Long, _
  94.     ByVal wParam As Long, _
  95.     ByVal lParam As Long) As Long
  96.  
  97. Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, _
  98.     ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
  99.  
  100. Declare Function Shell_NotifyIcon Lib "shell32.dll" Alias "Shell_NotifyIconA" (ByVal dwMessage As Long, lpData As NOTIFYICONDATA) As Long
  101.  
  102. Declare Function SHAppBarMessage Lib "shell32.dll" (ByVal dwMessage As Long, pData As APPBARDATA) As Long
  103.  
  104. ''-------------------------------------------------------------------------------------
  105. ''Application functions
  106. ''-------------------------------------------------------------------------------------
  107. Public Function ZoomForm(zoomToWhere As ZoomTypes, hwnd As Long) As Boolean
  108. 'This function 'zooms' a window.
  109.     Dim rctFrom As RECT
  110.     Dim rctTo As RECT
  111.     Dim lngTrayHand As Long
  112.     Dim lngStartMenuHand As Long
  113.     Dim lngChildHand As Long
  114.     Dim strClass As String * 255
  115.     Dim lngClassNameLen As Long
  116.     Dim lngRetVal As Long
  117.  
  118.     'Select the type of zoom to do.
  119.     Select Case zoomToWhere
  120.         'Zoom the window into the tray.
  121.         Case ZOOM_FROM_TRAY
  122.             'Get the handle to the start menu.
  123.             lngStartMenuHand = FindWindow("Shell_TrayWnd", vbNullString)
  124.  
  125.             'Get the handle to the first child window of the start menu.
  126.             lngChildHand = GetWindow(lngStartMenuHand, GW_CHILD)
  127.  
  128.             'Loop through all siblings until we find the 'System Tray' (A.K.A. --> TrayNotifyWnd)
  129.             Do
  130.                 lngClassNameLen = GetClassName(lngChildHand, strClass, Len(strClass))
  131.  
  132.                 'If it is the tray then store the handle.
  133.                 If InStr(1, strClass, "TrayNotifyWnd") Then
  134.                     lngTrayHand = lngChildHand
  135.                     Exit Do
  136.                 End If
  137.                 'If we didn't find it, go to the next sibling.
  138.                 lngChildHand = GetWindow(lngChildHand, GW_HWNDNEXT)
  139.             Loop
  140.  
  141.             'Get the RECT of  our form.
  142.             lngRetVal = GetWindowRect(hwnd, rctFrom)
  143.  
  144.             'Get the RECT of the Tray.
  145.             lngRetVal = GetWindowRect(lngTrayHand, rctTo)
  146.  
  147.             'Zoom from the tray to where our form is.
  148.             lngRetVal = DrawAnimatedRects(frm.hwnd, IDANI_CLOSE Or IDANI_CAPTION, rctTo, rctFrom)
  149.  
  150.         Case ZOOM_TO_TRAY
  151.  
  152.             'Get the handle to the start menu.
  153.             lngStartMenuHand = FindWindow("Shell_TrayWnd", vbNullString)
  154.  
  155.             'Get the handle to the first child window of the start menu.
  156.             lngChildHand = GetWindow(lngStartMenuHand, GW_CHILD)
  157.  
  158.             'Loop through all siblings until we find the 'System Tray' (A.K.A. --> TrayNotifyWnd)
  159.             Do
  160.                 lngClassNameLen = GetClassName(lngChildHand, strClass, Len(strClass))
  161.                 'If it is the tray then store the handle.
  162.                 If InStr(1, strClass, "TrayNotifyWnd") Then
  163.                     lngTrayHand = lngChildHand
  164.                     Exit Do
  165.                 End If
  166.                 'If we didn't find it, go to the next sibling.
  167.                 lngChildHand = GetWindow(lngChildHand, GW_HWNDNEXT)
  168.             Loop
  169.             'Get the RECT of  our form.
  170.             lngRetVal = GetWindowRect(hwnd, rctFrom)
  171.  
  172.             'Get the RECT of the Tray.
  173.             lngRetVal = GetWindowRect(lngTrayHand, rctTo)
  174.  
  175.             'Zoom from where our form is to the tray .
  176.             lngRetVal = DrawAnimatedRects(frm.hwnd, IDANI_OPEN Or IDANI_CAPTION, rctFrom, rctTo)
  177.     End Select
  178. End Function
  179.  
  180. Public Sub modIcon(Form1 As Form, IconID As Long, Icon As Object, ToolTip As String)
  181. 'Modify an existing icon on the system tray
  182.     Dim Result As Long
  183.     Notify.cbSize = 88&
  184.     Notify.hwnd = Form1.hwnd
  185.     Notify.uID = IconID
  186.     Notify.uFlags = NIF_ICON Or NIF_MESSAGE Or NIF_TIP
  187.     Notify.uCallbackMessage = WM_MOUSEMOVE
  188.     Notify.hIcon = Icon
  189.     Notify.szTip = ToolTip & Chr$(0)
  190.     Result = Shell_NotifyIcon(NIM_MODIFY, Notify)
  191. End Sub
  192.  
  193. Public Sub AddIcon(Form1 As Form, IconID As Long, Icon As Object, ToolTip As String)
  194. 'Create an icon on the system tray
  195.     Dim Result As Long
  196.     BarData.cbSize = 36&
  197.     Result = SHAppBarMessage(ABM_GETTASKBARPOS, BarData)
  198.     Notify.cbSize = 88&
  199.     Notify.hwnd = Form1.hwnd
  200.     Notify.uID = IconID
  201.     Notify.uFlags = NIF_ICON Or NIF_MESSAGE Or NIF_TIP
  202.     Notify.uCallbackMessage = WM_MOUSEMOVE
  203.     Notify.hIcon = Icon
  204.     Notify.szTip = ToolTip & Chr$(0)
  205.     Result = Shell_NotifyIcon(NIM_ADD, Notify)
  206. End Sub
  207.  
  208. Public Sub delIcon(IconID As Long)
  209. 'Remove an icon from the system tray
  210.     Dim Result As Long
  211.     Notify.uID = IconID
  212.     Result = Shell_NotifyIcon(NIM_DELETE, Notify)
  213. End Sub
  214.  
  215.  
  216.  
  217.  
  218.  
  219.  
  220.  
  221.