关于操作系统托盘图标

为方便操作系统托盘图标,写了一个模块:

'--------------------------------------------------------------------------------------- ' Module : basNotifyIcon ' Author : chenbo ' Date : 2010-8-10 ' Purpose : 操作系统托盘图标模块 '--------------------------------------------------------------------------------------- Option Explicit Private Declare Function Shell_NotifyIcon Lib "shell32.dll" Alias "Shell_NotifyIconA" (ByVal dwMessage As Long, lpData As NOTIFYICONDATA) As Long '定义结构类型NOTIFYICONDATA Public 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 ' 共享图标 ' 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 ' dwInfoFlags to NOTIFIICONDATA structure Private Const NIIF_NONE = &H0 ' 无图标 Private Const NIIF_INFO = &H1 ' "消息"图标 Private Const NIIF_WARNING = &H2 ' "警告"图标 Private Const NIIF_ERROR = &H3 ' "错误"图标 Private Const NIIF_ICON_MASK = &HF '保留值 Private Const NIIF_NOSOUND = &H10 '限6.0版本,不播放对应的声音 ' 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 '--------------------------------------------------------------------------------------- ' Procedure : SetNotifyIcon ' Author : chenbo ' Date : 2010-8-10 ' Purpose : 初始化并添加图标到系统托盘 '--------------------------------------------------------------------------------------- ' Public Sub SetNotifyIcon(IconData As NOTIFYICONDATA, frm As Form) With IconData .cbSize = Len(IconData) .hwnd = frm.hwnd .uId = 1 .uFlags = NIF_ICON Or NIF_INFO Or NIF_MESSAGE Or NIF_TIP .uCallBackMessage = WM_MOUSEMOVE .hIcon = frm.Icon.Handle '默认为窗口图标 .uTimeoutOrVersion = 15000 End With Shell_NotifyIcon NIM_ADD, IconData End Sub '--------------------------------------------------------------------------------------- ' Procedure : DeleteNotifyIcon ' Author : chenbo ' Date : 2010-8-10 ' Purpose : 删除托盘图标 '--------------------------------------------------------------------------------------- ' Public Sub DeleteNotifyIcon(IconData As NOTIFYICONDATA) Shell_NotifyIcon NIM_DELETE, IconData End Sub '--------------------------------------------------------------------------------------- ' Procedure : ShowNotifyIconBalloonInfo ' Author : chenbo ' Date : 2010-8-10 ' Purpose : 显示信息提示气泡 '--------------------------------------------------------------------------------------- ' Public Sub ShowNotifyIconBalloonInfo(IconData As NOTIFYICONDATA, strInfo As String, strInfoTitle As String) With IconData .szInfo = strInfo & vbNullChar .szInfoTitle = strInfoTitle & vbNullChar .dwInfoFlags = NIIF_INFO End With Shell_NotifyIcon NIM_MODIFY, IconData End Sub '--------------------------------------------------------------------------------------- ' Procedure : ShowNotifyIconBalloonWarning ' Author : chenbo ' Date : 2010-8-10 ' Purpose : 显示警告提示气泡 '--------------------------------------------------------------------------------------- ' Public Sub ShowNotifyIconBalloonWarning(IconData As NOTIFYICONDATA, strInfo As String, strInfoTitle As String) With IconData .szInfo = strInfo & vbNullChar .szInfoTitle = strInfoTitle & vbNullChar .dwInfoFlags = NIIF_WARNING End With Shell_NotifyIcon NIM_MODIFY, IconData End Sub '--------------------------------------------------------------------------------------- ' Procedure : ShowNotifyIconBalloonError ' Author : chenbo ' Date : 2010-8-10 ' Purpose : 显示错误提示气泡 '--------------------------------------------------------------------------------------- ' Public Sub ShowNotifyIconBalloonError(IconData As NOTIFYICONDATA, strInfo As String, strInfoTitle As String) With IconData .szInfo = strInfo & vbNullChar .szInfoTitle = strInfoTitle & vbNullChar .dwInfoFlags = NIIF_ERROR End With Shell_NotifyIcon NIM_MODIFY, IconData End Sub '--------------------------------------------------------------------------------------- ' Procedure : SetNotifyIconTip ' Author : chenbo ' Date : 2010-8-10 ' Purpose : 设置提示信息,当鼠标移到托盘图标上时显示 '--------------------------------------------------------------------------------------- ' Public Sub SetNotifyIconTip(IconData As NOTIFYICONDATA, strTip As String) With IconData .szTip = strTip & vbNullChar End With Shell_NotifyIcon NIM_MODIFY, IconData End Sub  

 

关于气泡提示的超时时间,曾迷惑了一段时间,经常发现气泡提示不会在设置的超时时间满足后自动消失。而且一个程序显示的气泡未消失前,其他程序的气泡弹不出来。这些问题都在MSDN中找到了答案,以下是MSDN中关于NOTIFYICONDATA的uTimeout的描述,摘录如下。

uTimeout
Union with uVersion. The timeout value, in milliseconds, for a balloon ToolTip. The system enforces minimum and maximum timeout values. uTimeout values that are too large are set to the maximum value and values that are too small default to the minimum value. The system minimum and maximum timeout values are currently set at 10 seconds and 30 seconds, respectively. See the remarks for further discussion of uTimeout.

 

Remarks

If you set the NIF_INFO flag in the uFlags member, the standard ToolTip is replaced by a balloon ToolTip. For more discussion of balloon ToolTips, see the Using ToolTip Controls chapter.

No more than one balloon ToolTip at a time is displayed for the taskbar. If an application attempts to display a ToolTip when one is already being displayed, the ToolTip will not appear until the existing balloon ToolTip has been visible for at least the system minimum timeout value. For example, a balloon ToolTip with uTimeout set to 30 seconds has been visible for seven seconds when another application attempts to display a balloon ToolTip. If the system minimum timeout is ten seconds, the first ToolTip displays for an additional three seconds before being replaced by the second ToolTip. If the user does not appear to be using the computer, the system does not count this time towards the timeout.

MSDN写的很明白,如果用户没有操作电脑——没有动鼠标或键盘,系统是不会为系统托盘气泡记超时的。这是在XP下的情况,据说在win2000下没有这个问题,没有测试,不置可否。
另外,系统托盘一次只能显示一个气泡,后面的气泡至少要等到前面显示的气泡达到最小超时时间后才能显示。

你可能感兴趣的:(Date,String,shell,application,System,2010)