任务栏图标大家都会使用了吧,但是如果在软件运行过程中,Explorer 出现异常而重新启动(即Explorer.exe被关闭后重新运行)了,任务栏图标会消失而不是跟着重新出现。要解决这个问题,需要使用一个API:RegisterWindowMessage,这个函数需要一个String类型的参数,返回值为long型,用途是注册一个唯一的新的Windows窗口消息,要注册任务栏重启的消息,我们应该这样写:
MsgTaskbarRestart = RegisterWindowMessage("TaskbarCreated")
返回值即为消息的标识符。如果出现错误,返回值为0。
系统要求:安装了Internet Explorer 4.0及以上版本的Windows操作系统。
下面是完整源代码,如有任何问题,或需要完整源工程,请发邮件给我。
---------------------------------------------------------
'Form1 中
'Explorer 崩溃重启后的任务栏图标重建
'作者 可爱猪
'Email:lovelypig43@163.com
Option Explicit
Private Declare Function RegisterWindowMessage Lib "user32" Alias "RegisterWindowMessageA" (ByVal lpString As String) As Long
Private Const NIM_DELETE = &H2
Private Sub Form_Load()
Call AddTrayIcon
MsgTaskbarRestart = RegisterWindowMessage("TaskbarCreated")
OldWndProc = SetWindowLong(hwnd, GWL_WNDPROC, AddressOf WndProc)
End Sub
Private Sub Form_Unload(Cancel As Integer)
Call Shell_NotifyIcon(NIM_DELETE, ano)
Call SetWindowLong(hwnd, GWL_WNDPROC, OldWndProc)
End Sub
---------------------------------------------------------
'Module1 中
'Explorer 崩溃重启后的任务栏图标重建
'作者 可爱猪
'Email:lovelypig43@163.com
Option Explicit
Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Public Declare Function Shell_NotifyIcon Lib "shell32.dll" Alias "Shell_NotifyIconA" (ByVal dwMessage As Long, lpData As NOTIFYICONDATA) As Long
Private Declare Function CallWindowProc Lib "user32" 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 GWL_WNDPROC = (-4)
Private Const WM_USER = &H400
Private Const WM_NOTIFYICON = WM_USER + &H100
Private Const NIF_ICON = &H2
Private Const NIF_TIP = &H4
Private Const NIF_MESSAGE = &H1
Private Const NIM_ADD = &H0
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
Public MsgTaskbarRestart As Long
Public OldWndProc As Long
Public ano As NOTIFYICONDATA
Function WndProc(ByVal hwnd As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
If uMsg <> MsgTaskbarRestart Then
WndProc = CallWindowProc(OldWndProc, hwnd, uMsg, wParam, lParam)
Else
Call AddTrayIcon
WndProc = True
End If
End Function
Public Sub AddTrayIcon()
With ano
.cbSize = Len(ano)
.hIcon = Form1.Icon
.hwnd = Form1.hwnd
.szTip = "Exolorer 重启后任务栏图标自动重建 -by 可爱猪" & Chr(0)
.uCallbackMessage = WM_NOTIFYICON
.uFlags = NIF_ICON Or NIF_TIP Or NIF_MESSAGE
.uID = 0
End With
Call Shell_NotifyIcon(NIM_ADD, ano)
End Sub
MsgTaskbarRestart = RegisterWindowMessage("TaskbarCreated")
返回值即为消息的标识符。如果出现错误,返回值为0。
系统要求:安装了Internet Explorer 4.0及以上版本的Windows操作系统。
下面是完整源代码,如有任何问题,或需要完整源工程,请发邮件给我。
---------------------------------------------------------
'Form1 中
'Explorer 崩溃重启后的任务栏图标重建
'作者 可爱猪
'Email:lovelypig43@163.com
Option Explicit
Private Declare Function RegisterWindowMessage Lib "user32" Alias "RegisterWindowMessageA" (ByVal lpString As String) As Long
Private Const NIM_DELETE = &H2
Private Sub Form_Load()
Call AddTrayIcon
MsgTaskbarRestart = RegisterWindowMessage("TaskbarCreated")
OldWndProc = SetWindowLong(hwnd, GWL_WNDPROC, AddressOf WndProc)
End Sub
Private Sub Form_Unload(Cancel As Integer)
Call Shell_NotifyIcon(NIM_DELETE, ano)
Call SetWindowLong(hwnd, GWL_WNDPROC, OldWndProc)
End Sub
---------------------------------------------------------
'Module1 中
'Explorer 崩溃重启后的任务栏图标重建
'作者 可爱猪
'Email:lovelypig43@163.com
Option Explicit
Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Public Declare Function Shell_NotifyIcon Lib "shell32.dll" Alias "Shell_NotifyIconA" (ByVal dwMessage As Long, lpData As NOTIFYICONDATA) As Long
Private Declare Function CallWindowProc Lib "user32" 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 GWL_WNDPROC = (-4)
Private Const WM_USER = &H400
Private Const WM_NOTIFYICON = WM_USER + &H100
Private Const NIF_ICON = &H2
Private Const NIF_TIP = &H4
Private Const NIF_MESSAGE = &H1
Private Const NIM_ADD = &H0
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
Public MsgTaskbarRestart As Long
Public OldWndProc As Long
Public ano As NOTIFYICONDATA
Function WndProc(ByVal hwnd As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
If uMsg <> MsgTaskbarRestart Then
WndProc = CallWindowProc(OldWndProc, hwnd, uMsg, wParam, lParam)
Else
Call AddTrayIcon
WndProc = True
End If
End Function
Public Sub AddTrayIcon()
With ano
.cbSize = Len(ano)
.hIcon = Form1.Icon
.hwnd = Form1.hwnd
.szTip = "Exolorer 重启后任务栏图标自动重建 -by 可爱猪" & Chr(0)
.uCallbackMessage = WM_NOTIFYICON
.uFlags = NIF_ICON Or NIF_TIP Or NIF_MESSAGE
.uID = 0
End With
Call Shell_NotifyIcon(NIM_ADD, ano)
End Sub