VB 托盘图标如何闪动的问题
愿听真正的VB高手解答,并恳请注释一下程序,谢谢。(网上复制来的都不行,我找了几天了,那些就省着吧。)问题如下:用VB测试一下托盘图标,API函数也有了的,固定图标的时候...
愿听真正的VB高手解答,并恳请注释一下程序,谢谢。
(网上复制来的都不行,我找了几天了,那些就省着吧。)
问题如下:
用VB测试一下托盘图标,API函数也有了的,固定图标的时候一切正常,可以有托盘图标,但就是无法实现闪动托盘图标和更换托盘图标。有文章说用TIMER实现,但我却怎么也实现不了。
在窗体里已有一个Picture1(0)和一个Picture1(1),有一个timer1,我应该如何让图标闪起来,和如何更换手动更换新的图标呢?
求教VB高手。
窗体代码如下,这段运行正常有效果:
Private Sub Form_Load()
TrayAddIcon Form1, "系统" '新建一个图标
End Sub
下面程序不能正常运行,也不知对不对:
Private Sub Timer1_Timer()
a = Not a
If a = True Then
TrayAddIcon Picture1, "演示"
Else
TrayAddIcon Picture2, "演示"
End If
End Sub
目前未有正确答案,几个答案都不行。 展开
(网上复制来的都不行,我找了几天了,那些就省着吧。)
问题如下:
用VB测试一下托盘图标,API函数也有了的,固定图标的时候一切正常,可以有托盘图标,但就是无法实现闪动托盘图标和更换托盘图标。有文章说用TIMER实现,但我却怎么也实现不了。
在窗体里已有一个Picture1(0)和一个Picture1(1),有一个timer1,我应该如何让图标闪起来,和如何更换手动更换新的图标呢?
求教VB高手。
窗体代码如下,这段运行正常有效果:
Private Sub Form_Load()
TrayAddIcon Form1, "系统" '新建一个图标
End Sub
下面程序不能正常运行,也不知对不对:
Private Sub Timer1_Timer()
a = Not a
If a = True Then
TrayAddIcon Picture1, "演示"
Else
TrayAddIcon Picture2, "演示"
End If
End Sub
目前未有正确答案,几个答案都不行。 展开
展开全部
'我已经试过,没问题的。
'你说的都不行,估计是托盘图标模块与我的不同。
‘下面我把完整的模块贴出来,请复制下面代码,替换原来代码就可以。
'===窗体代码====
Private Sub Form_Load()
TrayAddIcon Form1, picture1(0).picture, "系统" '新建一个图标
End Sub
Private Sub Timer1_Timer()
a = Not a
If a = True Then
TrayIcon Picture1(0).Picture
Else
TrayIcon Picture1(1).Picture
End If
End Sub
'====模块代码modTray.bas====
Option Explicit
'使用高分辨率图标所用的API
Private Declare Function LoadImage Lib "user32" Alias "LoadImageA" (ByVal hInst As Long, ByVal lpsz As String, ByVal dwImageType As Long, ByVal dwDesiredWidth As Long, ByVal dwDesiredHeight As Long, ByVal dwFlags As Long) As Long
Private Const LR_LOADFROMFILE = &H10
Private Const LR_LOADMAP3DCOLORS = &H1000
Private Const IMAGE_ICON = 1
'系统托盘
Private Declare Function Shell_NotifyIcon Lib "shell32.dll" Alias "Shell_NotifyIconA" (ByVal dwMessage As Long, lpData As NOTIFYICONDATA) As Long
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 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 NIM_VERSION = &H5
Private Const WM_USER As Long = &H400
Private 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)
Private Const NOTIFYICON_VERSION = 3
Private Const NIS_HIDDEN = &H1
Private Const NIS_SHAREDICON = &H2
Private Const WM_NOTIFY As Long = &H4E
Private Const WM_COMMAND As Long = &H111
Private Const WM_CLOSE As Long = &H10
Private Const WM_MOUSEMOVE As Long = &H200
Private Const WM_LBUTTONDOWN As Long = &H201
Private Const WM_LBUTTONUP As Long = &H202
Private Const WM_LBUTTONDBLCLK As Long = &H203
Private Const WM_MBUTTONDOWN As Long = &H207
Private Const WM_MBUTTONUP As Long = &H208
Private Const WM_MBUTTONDBLCLK As Long = &H209
Private Const WM_RBUTTONDOWN As Long = &H204
Private Const WM_RBUTTONUP As Long = &H205
Private Const WM_RBUTTONDBLCLK As Long = &H206
Public Enum bFlag
NIIF_NONE = &H0
NIIF_INFO = &H1
NIIF_WARNING = &H2
NIIF_ERROR = &H3
NIIF_GUID = &H5
NIIF_ICON_MASK = &HF
NIIF_NOSOUND = &H10 '关闭提示音标志
End Enum
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
End Type
'鼠标事件,在调用窗口中的move()过程中返回以下按键值,可用来进行相关操作
Public Enum TrayClick
MouseMove = &H200
LeftUp = &H202
LeftDown = &H201
LeftDbClick = &H203
RightUp = &H205
RightDown = &H204
RightDbClick = &H206
MiddleUp = &H208
MiddleDown = &H207
MiddleDbClick = &H209
BalloonClick = (WM_USER + 5)
End Enum
Public ni As NOTIFYICONDATA
' **************************************************************
'1 设置托盘一般提示
'注:MyIcon 图标既可以来源于文件,又可能来源于窗体内的某个图标
' **************************************************************
Public Sub TrayAddIcon(ByVal MyForm As Form, ByVal MyIcon, ByVal ToolTip As String, Optional ByVal bFlag As bFlag)
With ni
.cbSize = Len(ni)
.Hwnd = MyForm.Hwnd
.uID = vbNull
.uFlags = NIF_ICON Or NIF_TIP Or NIF_MESSAGE
.uCallbackMessage = WM_MOUSEMOVE
If TypeName(MyIcon) = "String" Then
.hIcon = LoadImage(App.hInstance, MyIcon, IMAGE_ICON, 16, 16, LR_LOADFROMFILE Or LR_LOADMAP3DCOLORS)
Else
If InStr(1, "Picture,Image", TypeName(MyIcon), vbTextCompare) > 0 Then
.hIcon = MyIcon
End If
End If
.szTip = ToolTip & vbNullChar
End With
Call Shell_NotifyIcon(NIM_ADD, ni)
End Sub
' *********************************************
'2 托盘托盘气泡提示
' *********************************************
'一、有声气泡提示
'TrayBalloon Form1, Command1.Caption & vbNewLine & Format(Date, "日期:yyyy年mm月dd日"), "系统托盘", NIIF_INFO
'二、无声气泡提示
'TrayBalloon Form1, Command2.Caption, "系统托盘", NIIF_INFO Or NIIF_NOSOUND
Public Sub TrayBalloon(ByVal MyForm As Form, ByVal sBaloonText As String, sBallonTitle As String, Optional ByVal bFlag As bFlag)
With ni
.cbSize = Len(ni)
.Hwnd = MyForm.Hwnd
.uID = vbNull
.uFlags = NIF_INFO
.dwInfoFlags = bFlag
.szInfoTitle = sBallonTitle & vbNullChar
.szInfo = sBaloonText & vbNullChar
End With
Shell_NotifyIcon NIM_MODIFY, ni
End Sub
' *********************************************
'3 移除托盘图标
' *********************************************
Public Sub TrayRemoveIcon()
Shell_NotifyIcon NIM_DELETE, ni
End Sub
'3.1删除其他程序的托盘图标
Sub TrayRemoveIcon_Ex(Hwnd As Long)
Dim TrayIcon As NOTIFYICONDATA
TrayIcon.cbSize = Len(TrayIcon)
TrayIcon.Hwnd = Hwnd
TrayIcon.uID = vbNull
Call Shell_NotifyIcon(NIM_DELETE, TrayIcon)
End Sub
' *********************************************
'4 更改托盘的提示文本
' *********************************************
Public Sub TrayTip(ByVal MyForm As Form, ByVal sTipText As String)
With ni
.cbSize = Len(ni)
.Hwnd = MyForm.Hwnd
.uID = vbNull
.uFlags = NIF_ICON Or NIF_TIP Or NIF_MESSAGE
.szTip = sTipText & vbNullChar
End With
Shell_NotifyIcon NIM_MODIFY, ni
End Sub
' *********************************************
'5 更改托盘的图标
' *********************************************
Sub TrayIcon(MyIcon)
' Update the tray icon.
With ni
If TypeName(MyIcon) = "String" Then
.hIcon = LoadImage(App.hInstance, MyIcon, IMAGE_ICON, 16, 16, LR_LOADFROMFILE Or LR_LOADMAP3DCOLORS)
Else
If TypeName(MyIcon) = "Picture" Then
.hIcon = MyIcon
End If
End If
.uFlags = NIF_ICON
End With
Shell_NotifyIcon NIM_MODIFY, ni
End Sub
'5 获取托盘图标的提示文本
Function TrayInfo(Wnd As Long) As String
' Update the tray icon.
With ni
.Hwnd = Wnd
.uFlags = NIF_INFO
End With
Shell_NotifyIcon NIM_MODIFY, ni
TrayInfo = ni.szTip
End Function
'你说的都不行,估计是托盘图标模块与我的不同。
‘下面我把完整的模块贴出来,请复制下面代码,替换原来代码就可以。
'===窗体代码====
Private Sub Form_Load()
TrayAddIcon Form1, picture1(0).picture, "系统" '新建一个图标
End Sub
Private Sub Timer1_Timer()
a = Not a
If a = True Then
TrayIcon Picture1(0).Picture
Else
TrayIcon Picture1(1).Picture
End If
End Sub
'====模块代码modTray.bas====
Option Explicit
'使用高分辨率图标所用的API
Private Declare Function LoadImage Lib "user32" Alias "LoadImageA" (ByVal hInst As Long, ByVal lpsz As String, ByVal dwImageType As Long, ByVal dwDesiredWidth As Long, ByVal dwDesiredHeight As Long, ByVal dwFlags As Long) As Long
Private Const LR_LOADFROMFILE = &H10
Private Const LR_LOADMAP3DCOLORS = &H1000
Private Const IMAGE_ICON = 1
'系统托盘
Private Declare Function Shell_NotifyIcon Lib "shell32.dll" Alias "Shell_NotifyIconA" (ByVal dwMessage As Long, lpData As NOTIFYICONDATA) As Long
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 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 NIM_VERSION = &H5
Private Const WM_USER As Long = &H400
Private 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)
Private Const NOTIFYICON_VERSION = 3
Private Const NIS_HIDDEN = &H1
Private Const NIS_SHAREDICON = &H2
Private Const WM_NOTIFY As Long = &H4E
Private Const WM_COMMAND As Long = &H111
Private Const WM_CLOSE As Long = &H10
Private Const WM_MOUSEMOVE As Long = &H200
Private Const WM_LBUTTONDOWN As Long = &H201
Private Const WM_LBUTTONUP As Long = &H202
Private Const WM_LBUTTONDBLCLK As Long = &H203
Private Const WM_MBUTTONDOWN As Long = &H207
Private Const WM_MBUTTONUP As Long = &H208
Private Const WM_MBUTTONDBLCLK As Long = &H209
Private Const WM_RBUTTONDOWN As Long = &H204
Private Const WM_RBUTTONUP As Long = &H205
Private Const WM_RBUTTONDBLCLK As Long = &H206
Public Enum bFlag
NIIF_NONE = &H0
NIIF_INFO = &H1
NIIF_WARNING = &H2
NIIF_ERROR = &H3
NIIF_GUID = &H5
NIIF_ICON_MASK = &HF
NIIF_NOSOUND = &H10 '关闭提示音标志
End Enum
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
End Type
'鼠标事件,在调用窗口中的move()过程中返回以下按键值,可用来进行相关操作
Public Enum TrayClick
MouseMove = &H200
LeftUp = &H202
LeftDown = &H201
LeftDbClick = &H203
RightUp = &H205
RightDown = &H204
RightDbClick = &H206
MiddleUp = &H208
MiddleDown = &H207
MiddleDbClick = &H209
BalloonClick = (WM_USER + 5)
End Enum
Public ni As NOTIFYICONDATA
' **************************************************************
'1 设置托盘一般提示
'注:MyIcon 图标既可以来源于文件,又可能来源于窗体内的某个图标
' **************************************************************
Public Sub TrayAddIcon(ByVal MyForm As Form, ByVal MyIcon, ByVal ToolTip As String, Optional ByVal bFlag As bFlag)
With ni
.cbSize = Len(ni)
.Hwnd = MyForm.Hwnd
.uID = vbNull
.uFlags = NIF_ICON Or NIF_TIP Or NIF_MESSAGE
.uCallbackMessage = WM_MOUSEMOVE
If TypeName(MyIcon) = "String" Then
.hIcon = LoadImage(App.hInstance, MyIcon, IMAGE_ICON, 16, 16, LR_LOADFROMFILE Or LR_LOADMAP3DCOLORS)
Else
If InStr(1, "Picture,Image", TypeName(MyIcon), vbTextCompare) > 0 Then
.hIcon = MyIcon
End If
End If
.szTip = ToolTip & vbNullChar
End With
Call Shell_NotifyIcon(NIM_ADD, ni)
End Sub
' *********************************************
'2 托盘托盘气泡提示
' *********************************************
'一、有声气泡提示
'TrayBalloon Form1, Command1.Caption & vbNewLine & Format(Date, "日期:yyyy年mm月dd日"), "系统托盘", NIIF_INFO
'二、无声气泡提示
'TrayBalloon Form1, Command2.Caption, "系统托盘", NIIF_INFO Or NIIF_NOSOUND
Public Sub TrayBalloon(ByVal MyForm As Form, ByVal sBaloonText As String, sBallonTitle As String, Optional ByVal bFlag As bFlag)
With ni
.cbSize = Len(ni)
.Hwnd = MyForm.Hwnd
.uID = vbNull
.uFlags = NIF_INFO
.dwInfoFlags = bFlag
.szInfoTitle = sBallonTitle & vbNullChar
.szInfo = sBaloonText & vbNullChar
End With
Shell_NotifyIcon NIM_MODIFY, ni
End Sub
' *********************************************
'3 移除托盘图标
' *********************************************
Public Sub TrayRemoveIcon()
Shell_NotifyIcon NIM_DELETE, ni
End Sub
'3.1删除其他程序的托盘图标
Sub TrayRemoveIcon_Ex(Hwnd As Long)
Dim TrayIcon As NOTIFYICONDATA
TrayIcon.cbSize = Len(TrayIcon)
TrayIcon.Hwnd = Hwnd
TrayIcon.uID = vbNull
Call Shell_NotifyIcon(NIM_DELETE, TrayIcon)
End Sub
' *********************************************
'4 更改托盘的提示文本
' *********************************************
Public Sub TrayTip(ByVal MyForm As Form, ByVal sTipText As String)
With ni
.cbSize = Len(ni)
.Hwnd = MyForm.Hwnd
.uID = vbNull
.uFlags = NIF_ICON Or NIF_TIP Or NIF_MESSAGE
.szTip = sTipText & vbNullChar
End With
Shell_NotifyIcon NIM_MODIFY, ni
End Sub
' *********************************************
'5 更改托盘的图标
' *********************************************
Sub TrayIcon(MyIcon)
' Update the tray icon.
With ni
If TypeName(MyIcon) = "String" Then
.hIcon = LoadImage(App.hInstance, MyIcon, IMAGE_ICON, 16, 16, LR_LOADFROMFILE Or LR_LOADMAP3DCOLORS)
Else
If TypeName(MyIcon) = "Picture" Then
.hIcon = MyIcon
End If
End If
.uFlags = NIF_ICON
End With
Shell_NotifyIcon NIM_MODIFY, ni
End Sub
'5 获取托盘图标的提示文本
Function TrayInfo(Wnd As Long) As String
' Update the tray icon.
With ni
.Hwnd = Wnd
.uFlags = NIF_INFO
End With
Shell_NotifyIcon NIM_MODIFY, ni
TrayInfo = ni.szTip
End Function
展开全部
代码如下;
Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
If iconFlag = "0" Then
NotifyIcon1.Icon = New Icon("info232.ico")
iconFlag = "1"
Else
NotifyIcon1.Icon = New Icon("Icon1.ico")
iconFlag = "0"
End If
End Sub
要+++++分哦!!
Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
If iconFlag = "0" Then
NotifyIcon1.Icon = New Icon("info232.ico")
iconFlag = "1"
Else
NotifyIcon1.Icon = New Icon("Icon1.ico")
iconFlag = "0"
End If
End Sub
要+++++分哦!!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你的代码永远是Picture1,因为你没定义a,默认是模块级别的,即每次访问TIMER1,都会重新赋值A=0
Private Sub Timer1_Timer()
Static a As Boolean
a = Not a
If a = True Then
TrayAddIcon Picture1(0), "演示"
Else
TrayAddIcon Picture1(1), "演示"
End If
End Sub
晕,刚才没注意,你用的PICTURE控件数组,代码中确是Picture1\Picture2......
如果我的演示再不正确,那么就是你的TrayAddIcon函数有问题了,你应该把TrayAddIcon函数代码公开。
Private Sub Timer1_Timer()
Static a As Boolean
a = Not a
If a = True Then
TrayAddIcon Picture1(0), "演示"
Else
TrayAddIcon Picture1(1), "演示"
End If
End Sub
晕,刚才没注意,你用的PICTURE控件数组,代码中确是Picture1\Picture2......
如果我的演示再不正确,那么就是你的TrayAddIcon函数有问题了,你应该把TrayAddIcon函数代码公开。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
期待中!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询