⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.vb

📁 进程跟踪器程序代码
💻 VB
字号:

Imports System.DirectoryServices

Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows 窗体设计器生成的代码 "

    Public Sub New()
        MyBase.New()

        '该调用是 Windows 窗体设计器所必需的。
        InitializeComponent()
       
        '在 InitializeComponent() 调用之后添加任何初始化

    End Sub

    '窗体重写处置以清理组件列表。
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Windows 窗体设计器所必需的
    Private components As System.ComponentModel.IContainer

    '注意:以下过程是 Windows 窗体设计器所必需的
    '可以使用 Windows 窗体设计器修改此过程。
    '不要使用代码编辑器修改它。
    Friend WithEvents Panel1 As System.Windows.Forms.Panel
    Friend WithEvents Splitter1 As System.Windows.Forms.Splitter
    Friend WithEvents RefreshButton As System.Windows.Forms.Button
    Friend WithEvents StopButton As System.Windows.Forms.Button
    Friend WithEvents ComboBox1 As System.Windows.Forms.ComboBox
    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents TabControl1 As System.Windows.Forms.TabControl
    Friend WithEvents TabPage1 As System.Windows.Forms.TabPage
    Friend WithEvents TabPage2 As System.Windows.Forms.TabPage
    Friend WithEvents ListView1 As System.Windows.Forms.ListView
    Friend WithEvents ColumnHeader1 As System.Windows.Forms.ColumnHeader
    Friend WithEvents ColumnHeader2 As System.Windows.Forms.ColumnHeader
    Friend WithEvents ColumnHeader3 As System.Windows.Forms.ColumnHeader
    Friend WithEvents ColumnHeader4 As System.Windows.Forms.ColumnHeader
    Friend WithEvents ColumnHeader5 As System.Windows.Forms.ColumnHeader
    Friend WithEvents ColumnHeader6 As System.Windows.Forms.ColumnHeader
    Friend WithEvents ListView2 As System.Windows.Forms.ListView
    Friend WithEvents ColumnHeader7 As System.Windows.Forms.ColumnHeader
    Friend WithEvents ColumnHeader8 As System.Windows.Forms.ColumnHeader
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.Panel1 = New System.Windows.Forms.Panel()
        Me.TabControl1 = New System.Windows.Forms.TabControl()
        Me.TabPage1 = New System.Windows.Forms.TabPage()
        Me.ListView2 = New System.Windows.Forms.ListView()
        Me.ColumnHeader7 = New System.Windows.Forms.ColumnHeader()
        Me.ColumnHeader8 = New System.Windows.Forms.ColumnHeader()
        Me.TabPage2 = New System.Windows.Forms.TabPage()
        Me.ListView1 = New System.Windows.Forms.ListView()
        Me.ColumnHeader1 = New System.Windows.Forms.ColumnHeader()
        Me.ColumnHeader2 = New System.Windows.Forms.ColumnHeader()
        Me.ColumnHeader3 = New System.Windows.Forms.ColumnHeader()
        Me.ColumnHeader4 = New System.Windows.Forms.ColumnHeader()
        Me.ColumnHeader5 = New System.Windows.Forms.ColumnHeader()
        Me.ColumnHeader6 = New System.Windows.Forms.ColumnHeader()
        Me.Splitter1 = New System.Windows.Forms.Splitter()
        Me.RefreshButton = New System.Windows.Forms.Button()
        Me.StopButton = New System.Windows.Forms.Button()
        Me.ComboBox1 = New System.Windows.Forms.ComboBox()
        Me.Label1 = New System.Windows.Forms.Label()
        Me.Panel1.SuspendLayout()
        Me.TabControl1.SuspendLayout()
        Me.TabPage1.SuspendLayout()
        Me.TabPage2.SuspendLayout()
        Me.SuspendLayout()
        '
        'Panel1
        '
        Me.Panel1.Controls.AddRange(New System.Windows.Forms.Control() {Me.TabControl1})
        Me.Panel1.Dock = System.Windows.Forms.DockStyle.Top
        Me.Panel1.Name = "Panel1"
        Me.Panel1.Size = New System.Drawing.Size(760, 256)
        Me.Panel1.TabIndex = 1
        '
        'TabControl1
        '
        Me.TabControl1.Controls.AddRange(New System.Windows.Forms.Control() {Me.TabPage1, Me.TabPage2})
        Me.TabControl1.Dock = System.Windows.Forms.DockStyle.Fill
        Me.TabControl1.Name = "TabControl1"
        Me.TabControl1.SelectedIndex = 0
        Me.TabControl1.Size = New System.Drawing.Size(760, 256)
        Me.TabControl1.TabIndex = 1
        '
        'TabPage1
        '
        Me.TabPage1.Controls.AddRange(New System.Windows.Forms.Control() {Me.ListView2})
        Me.TabPage1.Location = New System.Drawing.Point(4, 21)
        Me.TabPage1.Name = "TabPage1"
        Me.TabPage1.Size = New System.Drawing.Size(752, 231)
        Me.TabPage1.TabIndex = 0
        Me.TabPage1.Text = "应用程序"
        '
        'ListView2
        '
        Me.ListView2.Columns.AddRange(New System.Windows.Forms.ColumnHeader() {Me.ColumnHeader7, Me.ColumnHeader8})
        Me.ListView2.Dock = System.Windows.Forms.DockStyle.Fill
        Me.ListView2.FullRowSelect = True
        Me.ListView2.GridLines = True
        Me.ListView2.Name = "ListView2"
        Me.ListView2.Size = New System.Drawing.Size(752, 231)
        Me.ListView2.TabIndex = 0
        Me.ListView2.View = System.Windows.Forms.View.Details
        '
        'ColumnHeader7
        '
        Me.ColumnHeader7.Text = "任务"
        Me.ColumnHeader7.Width = 310
        '
        'ColumnHeader8
        '
        Me.ColumnHeader8.Text = "状态"
        Me.ColumnHeader8.Width = 350
        '
        'TabPage2
        '
        Me.TabPage2.Controls.AddRange(New System.Windows.Forms.Control() {Me.ListView1})
        Me.TabPage2.Location = New System.Drawing.Point(4, 21)
        Me.TabPage2.Name = "TabPage2"
        Me.TabPage2.Size = New System.Drawing.Size(752, 231)
        Me.TabPage2.TabIndex = 1
        Me.TabPage2.Text = "进程"
        '
        'ListView1
        '
        Me.ListView1.AllowColumnReorder = True
        Me.ListView1.Columns.AddRange(New System.Windows.Forms.ColumnHeader() {Me.ColumnHeader1, Me.ColumnHeader2, Me.ColumnHeader3, Me.ColumnHeader4, Me.ColumnHeader5, Me.ColumnHeader6})
        Me.ListView1.Dock = System.Windows.Forms.DockStyle.Fill
        Me.ListView1.FullRowSelect = True
        Me.ListView1.GridLines = True
        Me.ListView1.Name = "ListView1"
        Me.ListView1.Size = New System.Drawing.Size(752, 231)
        Me.ListView1.TabIndex = 1
        Me.ListView1.View = System.Windows.Forms.View.Details
        '
        'ColumnHeader1
        '
        Me.ColumnHeader1.Text = "映象名称"
        Me.ColumnHeader1.Width = 100
        '
        'ColumnHeader2
        '
        Me.ColumnHeader2.Text = "PID"
        '
        'ColumnHeader3
        '
        Me.ColumnHeader3.Text = "CPU"
        Me.ColumnHeader3.Width = 160
        '
        'ColumnHeader4
        '
        Me.ColumnHeader4.Text = "优先级"
        '
        'ColumnHeader5
        '
        Me.ColumnHeader5.Text = "内存使用"
        '
        'ColumnHeader6
        '
        Me.ColumnHeader6.Text = "虚拟内存"
        '
        'Splitter1
        '
        Me.Splitter1.Cursor = System.Windows.Forms.Cursors.HSplit
        Me.Splitter1.Dock = System.Windows.Forms.DockStyle.Top
        Me.Splitter1.Location = New System.Drawing.Point(0, 256)
        Me.Splitter1.Name = "Splitter1"
        Me.Splitter1.Size = New System.Drawing.Size(760, 3)
        Me.Splitter1.TabIndex = 2
        Me.Splitter1.TabStop = False
        '
        'RefreshButton
        '
        Me.RefreshButton.Anchor = System.Windows.Forms.AnchorStyles.Bottom
        Me.RefreshButton.Location = New System.Drawing.Point(540, 288)
        Me.RefreshButton.Name = "RefreshButton"
        Me.RefreshButton.TabIndex = 3
        Me.RefreshButton.Text = "刷新(&F)"
        '
        'StopButton
        '
        Me.StopButton.Anchor = System.Windows.Forms.AnchorStyles.Bottom
        Me.StopButton.Location = New System.Drawing.Point(420, 288)
        Me.StopButton.Name = "StopButton"
        Me.StopButton.Size = New System.Drawing.Size(96, 23)
        Me.StopButton.TabIndex = 4
        Me.StopButton.Text = "终止进程(&E)"
        '
        'ComboBox1
        '
        Me.ComboBox1.Location = New System.Drawing.Point(112, 272)
        Me.ComboBox1.Name = "ComboBox1"
        Me.ComboBox1.Size = New System.Drawing.Size(121, 20)
        Me.ComboBox1.TabIndex = 1
        '
        'Label1
        '
        Me.Label1.Location = New System.Drawing.Point(8, 272)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(80, 23)
        Me.Label1.TabIndex = 5
        Me.Label1.Text = "请选择机器名"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
        Me.ClientSize = New System.Drawing.Size(760, 317)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Label1, Me.StopButton, Me.RefreshButton, Me.Splitter1, Me.Panel1, Me.ComboBox1})
        Me.Name = "Form1"
        Me.Text = "进程跟踪器"
        Me.Panel1.ResumeLayout(False)
        Me.TabControl1.ResumeLayout(False)
        Me.TabPage1.ResumeLayout(False)
        Me.TabPage2.ResumeLayout(False)
        Me.ResumeLayout(False)

    End Sub

#End Region
    Dim ProcessArray() As Process
    Dim pcName As String = "jan"
    Dim sorter As MySorter


    Private Sub StopButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StopButton.Click
        If ListView1.SelectedItems.Count < 0 Then
            MessageBox.Show("请选择需结束的进程")
            Return
        Else
            Dim i As Integer
            For i = 0 To ListView1.SelectedItems.Count - 1
                Dim lvi As ListViewItem = ListView1.SelectedItems(i)
                Dim j As Integer
                For j = 0 To ProcessArray.Length - 1
                    '含主窗口
                    If ProcessArray(j).ProcessName = lvi.Text And ProcessArray(j).MainWindowHandle.ToString() = 0 Then
                        ProcessArray(j).CloseMainWindow()
                        '不含主窗口
                    ElseIf ProcessArray(j).ProcessName = lvi.Text Then
                        ProcessArray(j).Kill()
                    End If
                Next
            Next

        End If

    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        GetPCName()
        GetProcesses()
        InitialCombox()
        pcName = ComboBox1.SelectedText

    End Sub
    Private Sub InitialCombox()
        ComboBox1.Text = ProcessArray(0).MachineName()

    End Sub

    Private Sub RefreshButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RefreshButton.Click
        ListView1.Items.Clear()
        GetProcesses()
    End Sub


    Private Sub GetProcesses()
        Dim i As Integer
        Dim NewProcess(6) As String
        Dim NewApp(2) As String
        Try
            ProcessArray = Process.GetProcesses(pcName)
            For i = 0 To ProcessArray.Length - 1
                NewProcess(0) = ProcessArray(i).ProcessName
                NewProcess(1) = ProcessArray(i).Id.ToString()
                NewProcess(2) = ProcessArray(i).TotalProcessorTime().ToString()
                NewProcess(3) = ProcessArray(i).BasePriority.ToString()
                NewProcess(4) = ProcessArray(i).WorkingSet / 1024
                NewProcess(5) = ProcessArray(i).VirtualMemorySize / 1024
                Dim lvi As New ListViewItem(NewProcess)
                ListView1.Items.Add(lvi)

                '获取与进程关联的应用程序文件名
                If ProcessArray(i).Id <> 0 Then
                    Dim hwnd As Integer = ProcessArray(i).MainWindowHandle.ToInt32

                    '如果有主窗口
                    If hwnd <> 0 Then
                        Dim pm As ProcessModule = ProcessArray(i).MainModule
                        NewApp(0) = pm.ModuleName.ToString()
                        NewApp(1) = "正在运行"

                        Dim llvi As New ListViewItem(NewApp)
                        ListView2.Items.Add(llvi)
                    End If
                End If
            Next
        Catch e As Exception
            MessageBox.Show(e.Message)

        End Try
    End Sub


    Private Sub ListView1_ColumnClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ColumnClickEventArgs)

        '为ListViewItemSorter指定排序类
        sorter = New MySorter()
        ''sorter.MysortOrder = SortOrder.Ascending
        ListView1.ListViewItemSorter = sorter

        'If e.Column = 0 Then
        '    ListView1.Sorting = SortOrder.Ascending
        'End If

        If e.Column = sorter.sortColumn Then

            If sorter.MysortOrder = SortOrder.Ascending Then

                sorter.MysortOrder = SortOrder.Descending

            ElseIf sorter.MysortOrder = SortOrder.Descending Then
                sorter.MysortOrder = SortOrder.Ascending
            Else
                Return
            End If

        Else
            sorter.sortColumn = e.Column
            End If
        ListView1.Sort()

    End Sub

    Private Sub Process1_Exited(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub ComboBox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.TextChanged
        pcName = ComboBox1.Text

    End Sub

    Private Sub GetPCName()
        'or use "WinNT://your_domain_name"
        Dim root As DirectoryEntry = New DirectoryEntry("WinNT:")
        'root = "WinNT:"

        Dim domains As DirectoryEntries = root.Children
        domains.SchemaFilter.Add("domain")
        Dim domain As DirectoryEntry
        For Each domain In domains

            Dim computers As DirectoryEntries = domain.Children
            computers.SchemaFilter.Add("computer")
            Dim computer As DirectoryEntry
            For Each computer In computers
                ComboBox1.Items.Add(computer.Name)
                Dim users As DirectoryEntries = computer.Children
            Next computer

        Next domain

    End Sub

    Private Sub TabControl1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TabControl1.SelectedIndexChanged

    End Sub
End Class

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -