Declare Function Shell_NotifyIcon Lib "shell32.dll" Alias "Shell_NotifyIconA" (ByVal dwMessage As Long, lpData As NOTIFYICONDATA) As Long |
参数: | 意义 |
dwMessage | 为消息设置值,它可以是以下的几个常数值:0、1、2 |
NIM_ADD = 0 | 加入图标到系统状态栏中 |
NIM_MODIFY = 1 | 修改系统状态栏中的图标 |
NIM_DELETE = 2 | 删除系统状态栏中的图标 |
Type NOTIFYICONDATA cbSize As Long 需填入NOTIFYICONDATA数据结构的长度 HWnd As Long 设置成窗口的句柄 Uid As Long 为图标所设置的ID值 UFlags As Long 设置uCallbackMessage,hIcon,szTip是否有效 UCallbackMessage As Long 消息编号 HIcon As Long 显示在状态栏上的图标 SzTip As String * 64 提示信息 End Type |
Public Type NOTIFYICONDATA cbSize As Long HWnd As Long Uid As Long UFlags As Long UCallbackMessage As Long HIcon As Long SzTip As String * 64 End Type |
Private TheData As NOTIFYICONDATA |
这时我们就可以使用这个函数来设置系统托盘图标了,具体方法如下:
1、添加图标
With TheData .Uid = 0 .HWnd = frm.HWnd ''frm.HWnd是程序主窗体的句柄 .cbSize = Len(TheData) .HIcon = frm.Icon.Handle ''frm.Icon.Handle指向主窗体的图标 .UFlags = NIF_ICON .UCallbackMessage = TRAY_CALLBACK ''作用是允许返回消息,在下一节中会有详细解释。 .UFlags = .UFlags Or NIF_MESSAGE .cbSize = Len(TheData) End With Shell_NotifyIcon NIM_ADD, TheData ''根据前面定义NIM_ADD,设置为“添加模式”,然后添加 |
With TheData .UFlags = 0 End With Shell_NotifyIcon NIM_DELETE, TheData ''根据前面定义NIM_DELETE,设置为“删除模式” |
With TheData .HIcon = pic.Handle ''pic是图片狂PictureBox,存放图标文件 .UFlags = NIF_ICON End With Shell_NotifyIcon NIM_MODIFY, TheData ''根据前面定义NIM_MODIFY,设置为“更改模式” |
With TheData .SzTip = tip & vbNullChar ''tip是字符串string,存储提示信息 .UFlags = NIF_TIP ''指明要对浮动提示进行设置 End With Shell_NotifyIcon NIM_MODIFY, TheData ''根据前面定义NIM_MODIFY,设置为“修改模式” |