1,488
社区成员




'删除程序的托盘图标
Sub EdlDel(ByVal hWnd As Long)
With MyNotify
.uId = 0
.uFlags = NIF_ICON
.cbSize = Len(MyNotify)
.hWnd = hWnd 'frm.HWnd 是程序主窗体的句柄
End With
Shell_NotifyIcon NIM_DELETE, MyNotify
End Sub
'根据句柄获取进程ID
Function ProcIDFromWnd(ByVal hWnd As Long) As Long
Dim idProc As Long
GetWindowThreadProcessId hWnd, idProc
ProcIDFromWnd = idProc
End Function
'根据程序进程ID获取程序的主窗口句柄
Function GetWndHandleFromProcID(ByVal idProc As Long) As Long
Dim hWnd As Long
hWnd = FindWindow(vbNullString, vbNullString)
Do Until hWnd = 0
'检查是否具有父窗体
If GetParent(hWnd) = 0 Then
'匹配进程ID,符合则返回窗体的句柄
If idProc = ProcIDFromWnd(hWnd) Then
GetWndHandleFromProcID = hWnd
Exit Do
End If
End If
hWnd = GetWindow(hWnd, GW_HWNDNEXT)
Loop
End Function
遍历所有当前的窗口句柄,如果句柄无父窗体句柄,则根据其句柄得其进程ID与后台的进程ID比较,一致则认为该句柄为后台进程的主窗体句柄。难道在后台程序中加载托盘图标时用的me.hWnd和该方法得到的句柄会不一样?错在了哪个环节呢?