1. The interface is as follows: 2, the code is as follows Option Explicit
'Reprinted from VBNET
Private const app_systray_id = 999
Private const notifyicon_version = & h3
Private const nif_message = & h1private const nif_icon = & h2private const nif_tip = & h4private const nif_State = & h8private const nif_info = & h10
Private Const NIM_ADD = & H0Private Const NIM_MODIFY = & H1Private Const NIM_DELETE = & H2Private Const NIM_SETFOCUS = & H3Private Const NIM_SETVERSION = & H4Private Const NIM_VERSION = & H5
Private const nis_hidden = & h1private const nis_sharedicon = & h2
'Icon flagsPrivate Const NIIF_NONE = & H0Private Const NIIF_INFO = & H1Private Const NIIF_WARNING = & H2Private Const NIIF_ERROR = & H3Private Const NIIF_GUID = & H5Private Const NIIF_ICON_MASK = & HFPrivate Const NIIF_NOSOUND = & H10 Private Const WM_USER = & H400Private Const NIN_BALLOONSHOW = (WM_USER 2) Private Const NIN_BALLOONHIDE = (WM_USER 3) private const nin_balloontimeout = (WM_USER 4) Private const nin_balloonuserclick = (WM_USER 5)
'Shell version / NOTIFIYICONDATA struct size constantsPrivate Const NOTIFYICONDATA_V1_SIZE As Long = 88' pre-5.0 structure sizePrivate Const NOTIFYICONDATA_V2_SIZE As Long = 488 'pre-6.0 structure sizePrivate Const NOTIFYICONDATA_V3_SIZE As Long = 504' 6.0 structure sizePrivate NOTIFYICONDATA_SIZE As Long
Private Type Guid Data1 As Long Data2 AS Integer Data3 AS Integer Data4 (7) AS Byteend Type
Private Type NOTIFYICONDATA cbSize As Long hWnd As Long uID As Long uFlags As Long uCallbackMessage As Long hIcon As Long szTip As String * 128 dwState As Long dwStateMask As Long szInfo As String * 256 uTimeoutAndVersion As Long szInfoTitle As String * 64 dwInfoFlags As Long guidItem As Guidend TypePrivate Declare Function Shell_notifyicon Lib "shell32.dll" _ alias "shell_notifyicona" _ (Byval dwmessage as long, _ lpdata as notifyicondata) as long
Private Declare Function GetFileVersionInFiSize Lib "Version.dll" _ alias "getfileversioninfosizea" _ (Byval LPTSTRFILENAME AS STRING, _ LPDWHANDLE AS Long) As long
Private Declare Function GetFileVersionInfo Lib "version.dll" _ Alias "GetFileVersionInfoA" _ (ByVal lptstrFilename As String, _ ByVal dwHandle As Long, _ ByVal dwLen As Long, _ lpData As Any) As Long Private Declare Function VerQueryValue Lib "version.dll "_ Alias" verqueryvaluea "_ (Pblock as any, _ byval lpsubblock as string, _ lpbuffer as any, _ nversize as long) As long
Private Declare Sub CopyMemory LIB "Kernel32" _ Alias "RTLmoveMemory" _ (Destination as any, _ Source as Any, _ Byval length As long)
Private sub flow_load ()
TEXT1.TEXT = "Copyright? 2004-2008 Lipper Zhao, All Rights Reserved." & _ "Now Available for Download from" & _ "http://www.sompace.com/myapp/Update/." Command1.caption = "Add systray icon" command2.caption = "show balloon tip" command2.enabled = false option1 (0) .caption = "no icon" option1 (1) .caption = "information icon" option1 (2) .caption = "Warning Icon "option1 (3) .caption =" error icon "option1 (1) .value = true end subprivate subform_unload (Cancel AS Integer)
Shelltrayremove End Sub
Private submmand1_click () call shelltrayadd command2.enabled = true end sub
Private submmand2_click () shelltrayModifytip getselectedOptionINDEX () end sub
Private Sub ShellTrayAdd () Dim nid As NOTIFYICONDATA If NOTIFYICONDATA_SIZE = 0 Then SetShellVersion 'set up the type members With nid .cbSize = NOTIFYICONDATA_SIZE .hWnd = Form1.hWnd .uID = APP_SYSTRAY_ID .uFlags = NIF_MESSAGE Or NIF_ICON Or NIF_TIP .dwState = NIS_SHAREDICON. hIcon = Form1.Icon 'szTip is the tooltip shown when the' mouse hovers over the systray icon. 'Terminate it since the strings are' fixed-length in NOTIFYICONDATA. .szTip = "New Download Watcher" & vbNullChar .uTimeoutAndVersion = NOTIFYICON_VERSION End With 'add the icon ... call shell_notifyicon (NIM_ADD, NID)' ... and inform the system of the 'Notifyicon Version in Use call shell_notifyicon (nim_setversion, nid) End Subprivate Sub shelltrayRemove ()
Dim nid As NOTIFYICONDATA If NOTIFYICONDATA_SIZE = 0 Then SetShellVersion With nid .cbSize = NOTIFYICONDATA_SIZE .hWnd = Form1.hWnd .uID = APP_SYSTRAY_ID End With Call Shell_NotifyIcon (NIM_DELETE, nid)
End Sub
Private sub shelltrayModifytip (NICONINDEX AS Long)
DIM NID AS Notifyicondata
If NOTIFYICONDATA_SIZE = 0 Then SetShellVersion With nid .cbSize = NOTIFYICONDATA_SIZE .hWnd = Form1.hWnd .uID = APP_SYSTRAY_ID .uFlags = NIF_INFO .dwInfoFlags = nIconIndex 'InfoTitle is the balloon tip title,' and szInfo is the message displayed. 'Terminating both with vbNullChar prevents 'the display of the unused padding in the' strings defined as fixed-length in NOTIFYICONDATA. .szInfoTitle = "New Download Available!" & vbNullChar .szInfo = Text1.Text & vbNullChar End WithCall Shell_NotifyIcon (NIM_MODIFY, nid)
End Sub
Private sub setshellversion ()
Select Case True Case IsShellVersion (6) NOTIFYICONDATA_SIZE = NOTIFYICONDATA_V3_SIZE '6.0 structure size Case IsShellVersion (5) NOTIFYICONDATA_SIZE = NOTIFYICONDATA_V2_SIZE' pre-6.0 structure size Case Else NOTIFYICONDATA_SIZE = NOTIFYICONDATA_V1_SIZE 'pre-5.0 structure size End Select
End Sub
Private function isshellversion (byval version as long) as boolean
'Returns True if the Shell version' (shell32.dll) is equal or later than 'the value passed as' version' Dim nBufferSize As Long Dim nUnused As Long Dim lpBuffer As Long Dim nVerMajor As Integer Dim bBuffer () As Byte Const sDLLFile As String = "shell32.dll" nBufferSize = GetFileVersionInfoSize (sDLLFile, nUnused) If nBufferSize> 0 Then ReDim bBuffer (nBufferSize - 1) As Byte Call GetFileVersionInfo (sDLLFile, 0 &, nBufferSize, bBuffer (0)) If VerQueryValue (bBuffer (0 ), "/", lpBuffer, nUnused) = 1 Then CopyMemory nVerMajor, ByVal lpBuffer 10, 2 IsShellVersion = nVerMajor> = version End If 'VerQueryValue End If' nBufferSize End FunctionPrivate Function GetSelectedOptionIndex () As Long
'Returns the selected item index from' an option button array. Use in place 'of multiple If ... Then statements!' If your array contains more elements, 'just append them to the test condition,' setting the multiplier to the button's 'Negative -index. getSelectedOptionINDEX = Option1 (0) .Value * 0 or _ Option1 (1) .value * -1 or _ Option1 (2) .value * -2 or _ Option1 (3) .value * -3nd function