• 全部
  • VBS
  • .NET技术
  • VBA
  • VB网络编程
  • VB多媒体
  • VB数据库
  • VB控件
  • VB COM/DCOM
  • VB基础类
  • VB API
  • 问答

[请教]如何让NotifyIcon显示消息

niciyou 2005-03-02 11:30:24
经常可以看到Windows任务栏右侧的Notification Area的小图标弹出一些消息。这个如何实现?
我知道可以为程序创建一个NotifyIcon就可以在Notification Area显示一个图标。但如何用这个小图标显示消息呢?
多谢了!
...全文
178 点赞 收藏 6
写回复
6 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
niciyou 2005-03-03
多谢两位的帮助:)
回复
amendajing 2005-03-02
mark
回复
nga96 2005-03-02
楼上的,顶
回复
hamadou 2005-03-02
转贴!


在托盘图标中添加气球提示
很多朋友都见到过能在托盘图标上出现气球提示的软件,不说软件,就是在“磁盘空间不足”时Windows给出的提示就属于气球提示,那么怎样在自己的程序中添加这样的气球提示呢?
其实并不难,关键就在添加托盘图标时所使用的NOTIFYICONDATA结构,源代码如下:
窗体模块:
Option Explicit
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 ' 结构大小(字节)
hwnd As Long ' 处理消息的窗口的句柄
uId As Long ' 唯一的标识符
uFlags As Long ' Flags
uCallBackMessage As Long ' 处理消息的窗口接收的消息
hIcon As Long ' 托盘图标句柄
szTip As String * 128 ' Tooltip 提示文本
dwState As Long ' 托盘图标状态
dwStateMask As Long ' 状态掩码
szInfo As String * 256 ' 气球提示文本
uTimeoutOrVersion As Long ' 气球提示消失时间或版本
' uTimeout - 气球提示消失时间(单位:ms, 10000 -- 30000)
' uVersion - 版本(0 for V4, 3 for V5)
szInfoTitle As String * 64 ' 气球提示标题
dwInfoFlags As Long ' 气球提示图标
End Type
' dwState to NOTIFYICONDATA structure
Private Const NIS_HIDDEN = &H1 ' 隐藏图标
Private Const NIS_SHAREDICON = &H2 ' 共享图标
' dwInfoFlags to NOTIFIICONDATA structure
Private Const NIIF_NONE = &H0 ' 无图标
Private Const NIIF_INFO = &H1 ' "消息"图标
Private Const NIIF_WARNING = &H2 ' "警告"图标
Private Const NIIF_ERROR = &H3 ' "错误"图标
' uFlags to NOTIFYICONDATA structure
Private Const NIF_ICON As Long = &H2
Private Const NIF_INFO As Long = &H10
Private Const NIF_MESSAGE As Long = &H1
Private Const NIF_STATE As Long = &H8
Private Const NIF_TIP As Long = &H4
' dwMessage to Shell_NotifyIcon
Private Const NIM_ADD As Long = &H0
Private Const NIM_DELETE As Long = &H2
Private Const NIM_MODIFY As Long = &H1
Private Const NIM_SETFOCUS As Long = &H3
Private Const NIM_SETVERSION As Long = &H4
Private Sub Form_Load()
' 向托盘区添加图标
Dim IconData As NOTIFYICONDATA
Dim title As String
title = "托盘程序" & 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 = "这是气球提示" & 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)
' 删除托盘区图标
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 = "托盘程序"
.hIcon = Me.Icon.Handle
End With
Shell_NotifyIcon NIM_DELETE, IconData
SetWindowLong Me.hwnd, GWL_WNDPROC, preWndProc
' 卸载所有窗体
Dim frm As Form
For Each frm In Forms
Unload frm
Next
End Sub

标准模块:
Option Explicit
Public Declare Function SetWindowLong Lib "user32.dll" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Declare Function CallWindowProc Lib "user32.dll" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, ByVal msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Public Const WM_RBUTTONUP = &H205
Public Const WM_USER = &H400
Public Const WM_NOTIFYICON = WM_USER + 1 ' 自定义消息
Public Const WM_LBUTTONDBLCLK = &H203
Public Const GWL_WNDPROC = (-4)
' 关于气球提示的自定义消息, 2000下不产生这些消息
Public Const NIN_BALLOONSHOW = (WM_USER + &H2) ' 当 Balloon Tips 弹出时执行
Public Const NIN_BALLOONHIDE = (WM_USER + &H3) ' 当 Balloon Tips 消失时执行(如 SysTrayIcon 被删除),
' 但指定的 TimeOut 时间到或鼠标点击 Balloon Tips 后的消失不发送此消息
Public Const NIN_BALLOONTIMEOUT = (WM_USER + &H4) ' 当 Balloon Tips 的 TimeOut 时间到时执行
Public Const NIN_BALLOONUSERCLICK = (WM_USER + &H5) ' 当鼠标点击 Balloon Tips 时执行。
' 注意:在XP下执行时 Balloon Tips 上有个关闭按钮,
' 如果鼠标点在按钮上将接收到 NIN_BALLOONTIMEOUT 消息。
Public preWndProc As Long
' Form1 窗口入口函数
Function WindowProc(ByVal hwnd As Long, ByVal msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
' 拦截 WM_NOTIFYICON 消息
If msg = WM_NOTIFYICON Then
Select Case lParam
Case WM_RBUTTONUP
' 右键单击图标是运行这里的代码, 可以在这里添加弹出右键菜单的代码
Case WM_LBUTTONDBLCLK
Unload Form1
Case NIN_BALLOONSHOW
Debug.Print "显示气球提示"
Case NIN_BALLOONHIDE
Debug.Print "删除托盘图标"
Case NIN_BALLOONTIMEOUT
Debug.Print "气球提示消失"
Case NIN_BALLOONUSERCLICK
Debug.Print "单击气球提示"
End Select
End If
WindowProc = CallWindowProc(preWndProc, hwnd, msg, wParam, lParam)
End Function


回复
T2 2005-03-02
Imports System.Runtime.InteropServices

#Region "Win32Api_NotifyIcon"
Private Declare Function Shell_NotifyIcon Lib "shell32.dll" Alias _
"Shell_NotifyIconA" (ByVal dwMessage As Int32, _
ByRef lpData As NOTIFYICONDATA) As Int32

<System.Runtime.InteropServices.StructLayout(Runtime.InteropServices.LayoutKind.Sequential)> Private Structure NOTIFYICONDATA
Public cbSize As Int32
Public hwnd As IntPtr
Public uId As Int32
Public uFlags As Int32
Public uCallBackMessage As Int32
Public hIcon As IntPtr
<MarshalAs(UnmanagedType.ByValTStr, sizeconst:=128)> _
Public szTip As String
Public dwState As Int32
Public dwStateMask As Int32
<MarshalAs(UnmanagedType.ByValTStr, sizeconst:=256)> _
Public szInfo As String
Public uTimeoutOrVersion As Int32
<MarshalAs(UnmanagedType.ByValTStr, sizeconst:=64)> _
Public szInfoTitle As String
Public dwInfoFlags As Int32
End Structure

Private Const NOTIFYICON_VERSION = 3
Private Const NOTIFYICON_OLDVERSION = 0

Private Const NIM_ADD = &H0
Private Const NIM_MODIFY = &H1
Private Const NIM_DELETE = &H2

Private Const NIM_SETFOCUS = &H3
Private Const NIM_SETVERSION = &H4

Private Const NIF_MESSAGE = &H1
Private Const NIF_ICON = &H2
Private Const NIF_TIP = &H4

Private Const NIF_STATE = &H8
Private Const NIF_INFO = &H10

Private Const NIS_HIDDEN = &H1
Private Const NIS_SHAREDICON = &H2


Private Const NIIF_NONE = &H0
Private Const NIIF_WARNING = &H2
Private Const NIIF_ERROR = &H3
Private Const NIIF_INFO = &H1

Private nfIconData As NOTIFYICONDATA

#End Region


Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
nfIconData = New NOTIFYICONDATA
With nfIconData
.cbSize = System.Runtime.InteropServices.Marshal.SizeOf(nfIconData)
.hwnd = Me.Handle
.uId = vbNull
.uFlags = NIF_INFO Or NIF_ICON Or NIF_TIP Or NIF_MESSAGE
.hIcon = Me.Icon.Handle
.szTip = "123" ''& Microsoft.VisualBasic.vbNullChar
.dwState = 0
.dwStateMask = 0
.szInfo = "Balloon Style Tool-tip" & Chr(13) & "哈哈" ''& vbNullChar
.uTimeoutOrVersion = 15000
.szInfoTitle = "看到了么?" ''& Microsoft.VisualBasic.vbNullChar
.dwInfoFlags = NIIF_INFO
End With


Call Shell_NotifyIcon(NIM_ADD, nfIconData)

End Sub

Private Sub Form1_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Closed
Call Shell_NotifyIcon(NIM_DELETE, nfIconData)
End Sub
回复
redeemleep 2005-03-02
一个讨论软件工程的群 9949057
回复
相关推荐
发帖
VB
创建于2007-09-28

1.6w+

社区成员

VB技术相关讨论,主要为经典vb,即VB6.0
申请成为版主
帖子事件
创建了帖子
2005-03-02 11:30
社区公告
暂无公告