'VB 终止指定进程,刷新托盘图标 Private Type PROCESSENTRY32 dwSize As Long cntUsage As Long th32ProcessID As Long th32DefaultHeapID As Long th32ModuleID As Long cntThreads As Long th32ParentProcessID As Long pcPriClassBase As Long dwFlags As Long szExeFile As String * 1024 End Type Private Type MODULEENTRY32 dwSize As Long th32ModuleID As Long th32ProcessID As Long GlblcntUsage As Long ProccntUsage As Long modBaseAddr As Byte modBaseSize As Long hModule As Long szModule As String * 256 szExePath As String * 1024 End Type Private Const WM_MOUSEMOVE = &H200 Private Type RECT Left As Long Top As Long Right As Long Bottom As Long End Type Private Type OSVERSIONINFO dwOSVersionInfoSize As Long dwMajorVersion As Long dwMinorVersion As Long dwBuildNumber As Long dwPlatformId As Long szCSDVersion As String * 128 OsName As String End Type Const TH32CS_SNAPHEAPLIST = &H1 Const TH32CS_SNAPPROCESS = &H2 Const TH32CS_SNAPTHREAD = &H4 Const TH32CS_SNAPMODULE = &H8 Const TH32CS_SNAPALL = (TH32CS_SNAPHEAPLIST Or TH32CS_SNAPPROCESS Or TH32CS_SNAPTHREAD Or TH32CS_SNAPMODULE) Const TH32CS_INHERIT = &H80000000 Private Declare Function CreateToolhelp32Snapshot Lib "kernel32" (ByVal dwFlags As Long,ByVal th32ProcessID As Long) As Long Private Declare Function Process32First Lib "kernel32" (ByVal hSnapshot As Long,lppe As PROCESSENTRY32) As Long Private Declare Function Process32Next Lib "kernel32" (ByVal hSnapshot As Long,lppe As PROCESSENTRY32) As Long Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long,ByVal bInheritHandle As Long,ByVal dwProcessId As Long) As Long Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long,ByVal uExitCode As Long) As Long Private Declare Function UpdateWindow Lib "user32" Alias "UpdateWindow" (ByVal hwnd As Long) As Long Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As Any,ByVal lpWindowName As Any) As Long Private Declare Function GetVersionEx Lib "kernel32" Alias "GetVersionExA" (lpVersionInformation As OSVERSIONINFO) 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 GetClientRect Lib "user32" (ByVal hwnd As Long,lpRect As RECT) As Long Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long,ByVal wMsg As Long,ByVal wParam As Long,ByVal lParam As Long) As Long Sub KillExe(EXEName As String) Dim my As PROCESSENTRY32 Dim hProcessHandle As Long Dim success As Long Dim l As Long l = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0) If l Then my.dwSize = 1060 If (Process32First(l,my)) Then Do If UCase$(Right$(Left$(my.szExeFile,InStr(1,my.szExeFile,Chr$(0)) - 1),Len(EXEName))) = UCase$(EXEName) Then hProcessHandle = OpenProcess(&H1F0FFF,True,my.th32ProcessID) If hProcessHandle <> 0 Then success = TerminateProcess(hProcessHandle,ByVal 0&) If success = 1 Then CloseHandle (hProcessHandle) End If Loop Until (Process32Next(l,my) < 1) End If CloseHandle l End If End Sub Private Function GetSysTrayWnd() As Long Dim Result As Long Dim Ver As OSVERSIONINFO Ver.dwOSVersionInfoSize = 148 GetVersionEx Ver Result = FindWindow("Shell_TrayWnd",vbNullString) Result = FindWindowEx(Result,"TrayNotifyWnd",vbNullString) If Ver.dwMajorVersion = 5 And Ver.dwMinorVersion > 0 Then Result = FindWindowEx(Result,"SysPager",vbNullString) If Ver.dwMajorVersion = 5 Then Result = FindWindowEx(Result,"ToolbarWindow32",vbNullString) GetSysTrayWnd = Result End Function Private Sub RefreshTrayIcon() Dim hwndTrayToolBar As Long Dim X,Y As Long Dim rTrayToolBar As RECT Dim pos As Long hwndTrayToolBar = GetSysTrayWnd GetClientRect hwndTrayToolBar,rTrayToolBar For X = 1 To rTrayToolBar.Right - 1 For Y = 1 To rTrayToolBar.Bottom - 1 pos = (X And &HFFFF) + (Y And &HFFFF) * &H10000 PostMessage hwndTrayToolBar,WM_MOUSEMOVE,pos Next Y Next X End Sub Private Sub Command1_Click() Dim hShellTray As Long Dim a As String a = "c:/windows/notepad.exe"'以记事本为例 KillExe a RefreshTrayIcon'刷新托盘图标 End Sub
http://zhidao.baidu.com/question/92767362.html