Public Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Public Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
Public Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long
Public 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_CLOSE = &H10
Public Const GW_HWNDNEXT = 2
Public Const GW_HWNDFIRST = 0
Public WinText As String * 256
Public Handler(200) As Long
Public Sub findrunningprogram()
Dim RenHWND As Long
Dim Count As Integer
Dim RetValue As Long
Dim WinTextLength As Long
Count = 0
Form1.List1.Clear
RenHWND = GetWindow(Form1.hwnd, GW_HWNDFIRST)
Do
RetValue = GetWindowText(RenHWND, WinText, 256)
If RetValue <> 0 Then
WinTextLength = GetWindowTextLength(RenHWND)
If Left$(WinText, WinTextLength) <> Form1.Caption And Left$(WinText, WinTextLength) <> App.Title Then
Form1.List1.AddItem "[" & RenHWND & "] " & WinText
Handler(Count) = RenHWND
Count = Count + 1
End If
End If
RenHWND = GetWindow(RenHWND, GW_HWNDNEXT)
Loop Until RenHWND = 0
Form1.List1.ListIndex = 0
Form1.Label2.Caption = "共(" & Count & "项)"
End Sub
Form1中的代码:
Private Sub Command1_Click()
Dim RenValue As Long
RetValue = SendMessage(Handler(Form1.List1.ListIndex), WM_CLOSE, 0, 0)
If RetValue <> 0 Then
MsgBox "清除不成功!"
Call findrunningprogram
Else
MsgBox "清除成功!"
End If
End Sub
Private Sub Command2_Click()
Call findrunningprogram
End Sub
Private Sub Command3_Click()
Unload Form1
End
End Sub
Private Sub Form_Load()
Call findrunningprogram
End Sub
Private Sub Command1_Click()
Dim Process As PROCESSENTRY32
Dim ProcSnap As Long
Dim cntProcess As Long
cntProcess = 0
List1.Clear
ProcSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
If ProcSnap Then
Process.dwsize = 1060 ' 通常用法
Process32First ProcSnap, Process
Do Until Process32Next(ProcSnap, Process) < 1 ' 遍历所有进程直到返回值为False
List1.AddItem Trim(Process.szExeFile)
cntProcess = cntProcess + 1
Loop
End If
ReDim ProcessID(cntProcess) As Long
Dim i As Long
i = 0
Process32First ProcSnap, Process
Do Until Process32Next(ProcSnap, Process) < 1 ' 遍历所有进程直到返回值为False
ProcessID(i) = Process.th32ProcessID
i = i + 1
Loop
CloseHandle (ProcSnap)
End Sub
Private Sub Command2_Click()
Dim c As Integer
If List1.ListIndex < 0 Then
MsgBox "请选择进程!", vbOKOnly + vbInformation, "提示"
Else
Dim hProcess As Long
hProcess = OpenProcess(PROCESS_TERMINATE, False, ProcessID(List1.ListIndex))
If hProcess Then TerminateProcess hProcess, 0
c = List1.ListCount
While List1.ListCount = c
Command1_Click
Wend
End If
End Sub
Private Declare Function CreateToolhelp32Snapshot Lib "KERNEL32.DLL" (ByVal dwFlags As Long, ByVal th32ProcessID As Long) As Long
Private Declare Function EnumProcesses Lib "psapi.dll" (ByRef lpidProcess As Long, ByVal cb As Long, ByRef cbNeeded As Long) As Long