清理任务栏残留图标VB6版 附源程序

时间:2023-01-13 19:58:49


今日的工作中要强制结束另外一个进程,但是那个进程却拥有任务栏图标,在强制结束后它的任务栏图标没有被清除...呵呵,网上找不到这样的的函数,不过有个Delphi版,那只有俺亲自动手改写它了,...

下面是我写的VB6源代码,需要就拿去..不需要的就学习..呵呵


'清理任务栏残留图标 2006-9-20

'我想任何人都遇到过这样的情况:
'任务栏右下角的快捷图标有时并不会随着程序的关闭而消失
'只有当鼠标划过时才消失
'下面的函数可以通过自动划过并清除这些图标

Option Explicit
Private Declare Function RedrawWindow Lib "user32" (ByVal hwnd As Long, lprcUpdate As Long, ByVal hrgnUpdate As Long, ByVal fuRedraw As Long) As Long
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long

Private Declare Function GetSystemMetrics Lib "user32" (ByVal nIndex As Long) As Long
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Declare Function SetCursorPos Lib "user32" (ByVal x As Long, ByVal y As Long) As Long
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Private Type POINTAPI
x As Long
y As Long
End Type
Private Const RDW_INVALIDATE = &H1
Private Const RDW_ERASE = &H4
Private Const RDW_UPDATENOW = &H100
Private Const SM_CXSMICON = 49
Private Const SM_CYSMICON = 50

Public Sub RemoveDeadIconFromSysTray()
Dim TrayWindow As Long
Dim WindowRect As RECT
Dim SmallIconWidth As Long
Dim SmallIconHeight As Long
Dim CursorPos As POINTAPI
Dim Row As Long
Dim Col As Long
'获得任务栏句柄和边框
TrayWindow = FindWindowEx(FindWindow("Shell_TrayWnd", vbNullString), 0, "TrayNotifyWnd", vbNullString)
If GetWindowRect(TrayWindow, WindowRect) = 0 Then Exit Sub
'获得小图标大小
SmallIconWidth = GetSystemMetrics(SM_CXSMICON)
SmallIconHeight = GetSystemMetrics(SM_CYSMICON)
'保存当前鼠标位置
Call GetCursorPos(CursorPos)
'使鼠标快速划过每个图标
For Row = 0 To (WindowRect.Bottom - WindowRect.Top) / SmallIconHeight
For Col = 0 To (WindowRect.Right - WindowRect.Left) / SmallIconWidth
Call SetCursorPos(WindowRect.Left + Col * SmallIconWidth, WindowRect.Top + Row * SmallIconHeight)
Call Sleep(10) '发现这个地方参数为 0 的时候,有时候是不够的
Next
Next
'恢复鼠标位置
Call SetCursorPos(CursorPos.x, CursorPos.y)
'重画任务栏
Call RedrawWindow(TrayWindow, 0, 0, RDW_INVALIDATE Or RDW_ERASE Or RDW_UPDATENOW)
End Sub


(请参考Delphi版:清理任务栏残留图标Delphi版 附源程序)