Imports System.Runtime.InteropServices
Imports System.Diagnostics
Imports System.IO
Module winapi
Function GetWindowThreadProcessId(ByVal hwnd As IntPtr, ByRef procId As UInt32) As UInt32
End Function
Function OpenProcess(ByVal access As UInt32, ByVal inherit As Boolean, ByVal procid As UInt32) As IntPtr
End Function
Function CloseHandle(ByVal handle As IntPtr) As Boolean
End Function
Function GetModuleFileNameExW(ByVal hProc As IntPtr, ByVal hMod As IntPtr, ByVal arrName() As Char, ByVal arrSize As UInt32) As UInt32
End Function
End Module
Public Class 任务管理器
'创建一个Process类型的数组
Private 进程数组 As System.Diagnostics.Process()
Private Sub 任务管理器_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ColumnHeader1.Text = "进程名称"
ColumnHeader2.Text = "进程ID"
ColumnHeader3.Text = "窗体名称"
ColumnHeader4.Text = "进程句柄"
ColumnHeader5.Text = "命令行"
ListView1.Items.Clear()
'通过此语句返回的是进程数组,这些进程数组存放的是当前运行存在地进程资源
进程数组 = System.Diagnostics.Process.GetProcesses()
'得到当前运行进程数目
ToolStripStatusLabel1.Text = "进程数:" + 进程数组.Length.ToString
'获取计算机中每一个进程的信息,并显示出来
Dim 进程数组Process As System.Diagnostics.Process
For Each 进程数组Process In 进程数组
'得到进程的名称
Dim sProcessNeme As String = 进程数组Process.ProcessName
'得到进程的ID号
Dim sProcessID As String = 进程数组Process.Id.ToString()
'活动窗口名称
Dim nametitle As String = 进程数组Process.MainWindowTitle
'获得进程句柄
Dim nameinternet As String = 进程数组Process.MainWindowHandle
'获得关联进程的主模块
Dim name(260) As Char
Dim nameSize As UInt32 = GetModuleFileNameExW(进程数组Process.MainWindowHandle, IntPtr.Zero, name, 260)
Dim strName As String = New String(name, 0, nameSize)
Dim lvtTemp As ListViewItem = New ListViewItem(New String() {sProcessNeme, sProcessID, nametitle, nameinternet, strName}, -1)
ListView1.Items.Add(lvtTemp)
Next
End Sub
Private 任务管理器_操作_状态 As Boolean
Private 任务管理器_操作_显示 As Integer = 0
Private 鼠标X坐标 As Integer
Private 鼠标Y坐标 As Integer
Public 进程名称 As String
Private Sub ListView1_MouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListView1.MouseClick
If e.Button = Windows.Forms.MouseButtons.Right Then
任务管理器_操作.Close()
任务管理器_操作.Show()
End If
If e.Button = Windows.Forms.MouseButtons.Left Then
任务管理器_操作.Close()
End If
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
ListView1.Items.Clear()
'通过此语句返回的是进程数组,这些进程数组存放的是当前运行存在地进程资源
进程数组 = System.Diagnostics.Process.GetProcesses()
'得到当前运行进程数目
ToolStripStatusLabel1.Text = "进程数:" + 进程数组.Length.ToString
'获取计算机中每一个进程的信息,并显示出来
Dim 进程数组Process As System.Diagnostics.Process
For Each 进程数组Process In 进程数组
'得到进程的名称
Dim sProcessNeme As String = 进程数组Process.ProcessName
'得到进程的ID号
Dim sProcessID As String = 进程数组Process.Id.ToString()
'活动窗口名称
Dim nametitle As String = 进程数组Process.MainWindowTitle
'获得进程句柄
Dim nameinternet As String = 进程数组Process.MainWindowHandle
'获得关联进程的主模块
Dim name(260) As Char
Dim nameSize As UInt32 = GetModuleFileNameExW(进程数组Process.MainWindowHandle, IntPtr.Zero, name, 260)
Dim strName As String = New String(name, 0, nameSize)
Dim lvtTemp As ListViewItem = New ListViewItem(New String() {sProcessNeme, sProcessID, nametitle, nameinternet, strName}, -1)
ListView1.Items.Add(lvtTemp)
Next
End Sub
End Class
Public Class 任务管理器_操作
Private Sub 任务管理器_操作_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.FormBorderStyle = Windows.Forms.FormBorderStyle.None '无任何窗体边框样式
Me.Top = System.Windows.Forms.Cursor.Position.Y
Me.Left = System.Windows.Forms.Cursor.Position.X
Label1.Text = "进程名称:" & 任务管理器.ListView1.SelectedItems.Item(0).Text
Me.TopMost = True '最顶层的窗体
Me.ShowInTaskbar = False '不在任务栏显示窗体
Me.BackColor = Color.Gray
Label1.ForeColor = Color.Blue
End Sub
Private Sub ListBox1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListBox1.MouseDown
If MouseButtons = Windows.Forms.MouseButtons.Left Then
Dim 选择命令 As String = ListBox1.SelectedItems.Item(0)
If 选择命令 = "打开程序所在文件夹" Then
ElseIf 选择命令 = "结束本进程" Then
Dim 待结束进程() As Process = Process.GetProcessesByName(任务管理器.ListView1.SelectedItems.Item(0).Text) '绑定获得指的进程定名称
If 待结束进程.Length > 0 Then
待结束进程(0).Kill()
End If
ElseIf 选择命令 = "结束所有关联进程" Then
ElseIf 选择命令 = "取消" Then
Me.Close()
End If
End If
End Sub
End Class