Add a balloon prompt in the tray icon

zhaozj2021-02-16  65

Many friends have seen software that can appear on the tray icon, do not say software, is that the prompts given by Windows when "the disk space is less than", so how to add such a balloon in their own procedure. Tip?

In fact, it is not difficult. The key is the NOTIFYICONDATA structure used in the addition of the tray icon, and the source code is as follows:

Form module:

Option expedition

Private declare function shell_notifyicon lib "shell32.dll" alias "shell_notifyicona" (Byval Dwmessage As Long, LPDATA AS Notifyicondata) AS Long

Private Type Notifyicondata Cbsize As Long 'Structure Size (Byte) HWND AS LONG' Handle UID AS Long 'Unique Identifier UFLAGS As Long' Flags UcallbackMessage As Long 'Handling Messages HiCON As Long 'Tray icon handle sztip as string * 128' Tooltip prompt text DWSTATE AS long 'tray icon status DWSTATEMASK AS long' status mask SZINFO AS STRING * 256 'balloon prompt text UTIMEOUTORVERSION AS Long' balloon prompt disappearance time or version 'utimeout - balloon Tips Time (Units: MS, 1000 - 30000) 'Uversion - Version (0 for v4, 3 for v5) Szinfotitle As String * 64' Balloon Tip Title DWINFOFLAGS AS Long 'Balloon Tips End Type

'DWState to Notifyicondata StructurePrivate const nis_hidden = & h1' hidden icon private const nis_sharedicon = & h2 'shared icon

'DwInfoFlags to NOTIFIICONDATA structurePrivate Const NIIF_NONE = & H0' no icon Private Const NIIF_INFO = & H1 ' "message" icon Private Const NIIF_WARNING = & H2' "warning" icon Private Const NIIF_ERROR = & H3 ' "Error" icon

'UFlags to NOTIFYICONDATA structurePrivate Const NIF_ICON As Long = & H2Private Const NIF_INFO As Long = & H10Private Const NIF_MESSAGE As Long = & H1Private Const NIF_STATE As Long = & H8Private Const NIF_TIP As Long = & H4' dwMessage to Shell_NotifyIconPrivate Const NIM_ADD As Long = & H0Private Const NIM_DELETE As Long = & H2PRIVATE CONST NIM_MODIFY As Long = & H1PRIVATE CONST NIM_SETFOCUS As Long = & H3Private Const Nim_setVersion As long = & h4

Private Sub Form_Load () 'is added to the tray icon Dim IconData As NOTIFYICONDATA Dim title As String title = "tray program" & vbNullChar With IconData .cbSize = Len (IconData) .hwnd = Me.hwnd .uId = 0 .uFlags = NIF_TIP Or NIF_ICON Or NIF_MESSAGE Or NIF_INFO Or NIF_STATE .uCallBackMessage = WM_NOTIFYICON .szTip = title .hIcon = Me.Icon.Handle .dwState = 0 .dwStateMask = 0 .szInfo = "this is a balloon tip" & vbNullChar .szInfoTitle = title .dwInfoFlags = Niif_info .utimeoutorVersion = 10000 End with shell_notifyicon nim_add, icondata prewndproc = setWindowlong (me.hwnd, gwl_wndproc, addressof windowproc) End Sub

Private Sub Form_Unload (Cancel As Integer) 'Delete tray icon Dim IconData As NOTIFYICONDATA With IconData .cbSize = Len (IconData) .hwnd = Me.hwnd .uId = 0 .uFlags = NIF_TIP Or NIF_ICON Or NIF_MESSAGE .uCallBackMessage = WM_NOTIFYICON .szTip = "Tray Program". Hicon = Me.ICON.Handle End with shell_notifyicon nim_delete, icondata setwindowlong me.hwnd, gwl_wndproc, preWndProc 'Uninstall All Forms DIM FRM AS FORM for Each FRM in Forms Unload FRM NEXTEND SUB

Standard module:

Option expedition

Public Declare Function SetWindowLong Lib "user32.dll" Alias ​​"SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As LongPrivate Declare Function CallWindowProc Lib "user32.dll" Alias ​​"CallWindowProcA" (ByVal lpPrevWndFunc As Long , Byval HWnd As Long, Byval WParam As Long, Byval LParam As Long AS Long

Public const wm_rbuttonup = & h205public const wm_user = & h400public const wm_notifyicon = wm_user 1 'custom message public const wm_lbuttondblclk = & h203public const gwl_wndproc = (-4)

'Custom message on the balloon prompt, do not generate these messages in 2000 PUBLIC Const nin_balloonshow = (WM_USER & H2)' When the Balloon Tips pops up, executing public const nin_balloonhide = (WM_USER & H3) 'When the Balloon TIPS disappears (such as Systrayicon Deleted), 'But the specified Timeout time is or the mouse click the disappearance after the Balloon TIPS does not send this message public const nin_balloontimeout = (WM_USER & H4)' When the balloon tiPs TIMEOUSER CLICK = (WM_USER & H5 ) 'When the mouse click to Balloon TIPS. 'Note: There is a closed button on the Balloon TIPS when executed under XP,' If the mouse point will receive the Nin_ballOontimeout message on the button. Public preWndProc As Long

'Form1 entry window function Function WindowProc (ByVal hwnd As Long, ByVal msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long' message interception WM_NOTIFYICON If msg = WM_NOTIFYICON Then Select Case lParam Case WM_RBUTTONUP 'Right-click the icon is run code here, where you can add the code pop-up context menu Case WM_LBUTTONDBLCLK Unload Form1 Case NIN_BALLOONSHOW Debug.Print "display balloon tips" Case NIN_BALLOONHIDE Debug.Print "delete tray icon" Case NIN_BALLOONTIMEOUT Debug.Print "balloon tips disappear" Case NIN_BALLOONUSERCLICK Debug.print "Click Balloon Tips" End Select End IF WindowProc = CallWindowProc (PrewndProc, HWnd, MSG, WPARAM, LPARAM) End Function

转载请注明原文地址:https://www.9cbs.com/read-17112.html

New Post(0)