怎样清除托盘残留的图标

vbgood2008 2010-07-10 03:40:25
当软件退出时,怎样清除托盘残留的图标。请高手指教。
论坛里找的这个方法是不行的。
Declare Function SetForegroundWindow Lib "user32 " Alias "SetForegroundWindow " (ByVal hwnd As Long) As Long


SetForegroundWindow Me.nwnd
Me.PopupMenu mnuTest
以上方法是不行。有没有其它法子。谢谢。
...全文
187 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
vbgood2008 2010-07-10
  • 打赏
  • 举报
回复
用不起来也。
jackey 2010-07-10
  • 打赏
  • 举报
回复
下面有处理系统托盘图标的函数
Public Declare Function GetComputerName Lib "KERNEL32" Alias "GetComputerNameA" (ByVal lpBuffer As String, nSize As Long) As Long
Public Declare Sub Sleep Lib "KERNEL32" (ByVal dwMilliseconds As Long)

Public Const WM_SYSCOMMAND = &H112

Public Const SC_MOVE = &HF012
Public Const HWND_TOPMOST = 8
Global Const SWP_MOVE = 2
Global Const SWP_NOSIZE = 1
Public Const WM_NCLBUTTONDOWN = &HA1
Public Const HTCAPTION = 2

Global Const Flags = SWP_NOSIZE Or SWP_MOVE
Declare Function SetWindowsPos Lib "user" (ByVal h%, ByVal hb%, ByVal X%, ByVal y%, ByVal cx%, ByVal cy%, ByVal f%) As Integer
Public OldWindowProc As Long
Public TheForm As Form
Public TheMenu As Menu

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


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
Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Declare Function Shell_NotifyIcon Lib "shell32.dll" Alias "Shell_NotifyIconA" (ByVal dwMessage As Long, lpData As NOTIFYICONDATA) As Long
Declare Function sendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long

Public Const WM_USER = &H400
Public Const WM_LBUTTONUP = &H202
Public Const WM_MBUTTONUP = &H208
Public Const WM_RBUTTONUP = &H205
Public Const TRAY_CALLBACK = (WM_USER + 1001&)
Public Const GWL_WNDPROC = (-4)
Public Const GWL_USERDATA = (-21)
Public Const NIF_ICON = &H2
Public Const NIF_TIP = &H4
Public Const NIM_ADD = &H0
Public Const NIF_MESSAGE = &H1
Public Const NIM_MODIFY = &H1
Public Const NIM_DELETE = &H2


Private TheData As NOTIFYICONDATA

Public Function getthisComputerName() As String
Dim computerName As String
Dim strLength As Long
strLength = 255
computerName = String(strLength, Chr(0))
GetComputerName computerName, strLength
getthisComputerName = computerName
End Function


' *********************************************
' The replacement window proc.
' *********************************************
Public Function NewWindowProc(ByVal hwnd As Long, ByVal msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
If msg = TRAY_CALLBACK Then
' The user clicked on the tray icon.
' Look for click events.
If lParam = WM_LBUTTONUP Then
' On left click, show the form.
If TheForm.WindowState = vbMinimized Then _
TheForm.WindowState = TheForm.LastState
TheForm.Visible = True
TheForm.SetFocus
Exit Function
End If
If lParam = WM_RBUTTONUP Then
' On right click, show the menu.
TheForm.PopupMenu TheMenu
Exit Function
End If
End If

NewWindowProc = CallWindowProc(OldWindowProc, hwnd, msg, wParam, lParam)
End Function

' *********************************************
' Add the form's icon to the tray.
' *********************************************
Public Sub AddToTray(Frm As Form, Mnu As Menu)
Set TheForm = Frm
Set TheMenu = Mnu
OldWindowProc = SetWindowLong(TheForm.hwnd, GWL_WNDPROC, AddressOf NewWindowProc)
With TheData
.uID = 0
.hwnd = Frm.hwnd
.cbSize = Len(TheData)
.hIcon = Frm.Icon.Handle
.uFlags = NIF_ICON
.uCallbackMessage = TRAY_CALLBACK
.uFlags = .uFlags Or NIF_MESSAGE
.cbSize = Len(TheData)
End With
Shell_NotifyIcon NIM_ADD, TheData
End Sub

' *********************************************
' Remove the icon from the system tray.
' *********************************************
Public Sub RemoveFromTray()
With TheData
.uFlags = 0
End With
Shell_NotifyIcon NIM_DELETE, TheData

' Restore the original window proc.
SetWindowLong TheForm.hwnd, GWL_WNDPROC, OldWindowProc
End Sub

' *********************************************
' Set a new tray tip.
' *********************************************
Public Sub SetTrayTip(tip As String)
With TheData
.szTip = tip & vbNullChar
.uFlags = NIF_TIP
End With
Shell_NotifyIcon NIM_MODIFY, TheData
End Sub

' *********************************************
' Set a new tray icon.
' *********************************************
Public Sub SetTrayIcon(pic As Picture)
' Do nothing if the picture is not an icon.
If pic.Type <> vbPicTypeIcon Then Exit Sub

' Update the tray icon.
With TheData
.hIcon = pic.Handle
.uFlags = NIF_ICON
End With
Shell_NotifyIcon NIM_MODIFY, TheData
End Sub

7,763

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧