效果图
窗体里放一个ListView和两个按钮
Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode 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 CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long Private Declare Function CreateToolhelpSnapshot Lib "kernel32" Alias "CreateToolhelp32Snapshot" (ByVal lFlags As Long, ByVal lProcessID As Long) As Long Private Declare Function ProcessFirst Lib "kernel32" Alias "Process32First" (ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As Long Private Declare Function ProcessNext Lib "kernel32" Alias "Process32Next" (ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As Long
Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long Private Const SW_HIDE = 0 Private Const SW_RESTORE = 9 Private Const SW_SHOW = 5
Private Const TH32CS_SNAPPROCESS = &H2 Private Const TH32CS_SNAPheaplist = &H1 Private Const TH32CS_SNAPthread = &H4 Private Const TH32CS_SNAPmodule = &H8 Private Const TH32CS_SNAPall = TH32CS_SNAPPROCESS + TH32CS_SNAPheaplist + TH32CS_SNAPthread + TH32CS_SNAPmodule Private Const MAX_PATH As Integer = 260 Private Const PROCESS_TERMINATE = &H1
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 * MAX_PATH End Type
Private Sub command1_Click() Dim i As Long Dim proc As PROCESSENTRY32 Dim snap As Long Dim exename As String lvw.ListItems.Clear ': 清空所有内容 snap = CreateToolhelpSnapshot(TH32CS_SNAPall, 0) ':获得进程“快照”的句柄 proc.dwSize = Len(proc) theloop = ProcessFirst(snap, proc) ':获取第一个进程,并得到其返回值 i = 0 While theloop <> 0 ':当返回值非零时继续获取下一个进程 exename = proc.szExeFile ret = lvw.ListItems.Add(, "first" & CStr(i), exename) ':将进程名添加到第一项中 lvw.ListItems("first" & CStr(i)).SubItems(1) = proc.th32ProcessID ':将进程ID添加到第二项中 i = i + 1 theloop = ProcessNext(snap, proc) Wend CloseHandle snap ':关闭进程“快照”句柄 End Sub Private Sub Command2_Click() Dim i As Long hand = OpenProcess(PROCESS_TERMINATE, True, CLng(lvw.SelectedItem.SubItems(1))) ':获取进程句柄 TerminateProcess hand, 0 ':关闭进程 Call command1_Click ':调用查看进程来刷新进程列表 End Sub
Private Sub Command3_Click() hand = OpenProcess(PROCESS_TERMINATE, True, CLng(lvw.SelectedItem.SubItems(1))) ':获取进程句柄 ShowWindow hand, SW_SHOW End Sub
Private Sub Command4_Click() hand = OpenProcess(PROCESS_TERMINATE, True, CLng(lvw.SelectedItem.SubItems(1))) ':获取进程句柄 ShowWindow hand, SW_HIDE Text1.Text = hand End Sub
Private Sub Form_Load() Dim header As ColumnHeader lvw.View = lvwReport lvw.ColumnHeaders.Clear Set header = lvw.ColumnHeaders.Add(, "first", "进程", 4000) ':设置ListView中项目的宽度,读者也可自行设置 Set header = lvw.ColumnHeaders.Add(, "second", "ID", 1400) lvw.Refresh End Sub
[此贴子已经被作者于2004-05-20 17:53:56编辑过]