📄 form1.vb
字号:
Friend Class Form1
Inherits System.Windows.Forms.Form
#Region "Windows 窗体设计器生成的代码"
Public Sub New()
MyBase.New()
'此调用是 Windows 窗体设计器所必需的。
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
Public ToolTip1 As System.Windows.Forms.ToolTip
Public WithEvents pb1 As AxMSComctlLib.AxProgressBar
Public WithEvents Timer1 As System.Windows.Forms.Timer
Public WithEvents Label1 As System.Windows.Forms.Label
'注意: 以下过程是 Windows 窗体设计器所必需的
'可以使用 Windows 窗体设计器来修改它。
'不要使用代码编辑器修改它。
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.components = New System.ComponentModel.Container
Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(Form1))
Me.ToolTip1 = New System.Windows.Forms.ToolTip(Me.components)
Me.pb1 = New AxMSComctlLib.AxProgressBar
Me.Timer1 = New System.Windows.Forms.Timer(Me.components)
Me.Label1 = New System.Windows.Forms.Label
CType(Me.pb1, System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
'
'pb1
'
Me.pb1.Location = New System.Drawing.Point(0, 40)
Me.pb1.Name = "pb1"
Me.pb1.OcxState = CType(resources.GetObject("pb1.OcxState"), System.Windows.Forms.AxHost.State)
Me.pb1.Size = New System.Drawing.Size(353, 25)
Me.pb1.TabIndex = 1
'
'Timer1
'
Me.Timer1.Interval = 1000
'
'Label1
'
Me.Label1.BackColor = System.Drawing.SystemColors.Control
Me.Label1.Cursor = System.Windows.Forms.Cursors.Default
Me.Label1.ForeColor = System.Drawing.SystemColors.ControlText
Me.Label1.Location = New System.Drawing.Point(8, 8)
Me.Label1.Name = "Label1"
Me.Label1.RightToLeft = System.Windows.Forms.RightToLeft.No
Me.Label1.Size = New System.Drawing.Size(329, 25)
Me.Label1.TabIndex = 0
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
Me.BackColor = System.Drawing.SystemColors.Control
Me.ClientSize = New System.Drawing.Size(356, 71)
Me.Controls.Add(Me.pb1)
Me.Controls.Add(Me.Label1)
Me.Cursor = System.Windows.Forms.Cursors.Default
Me.Location = New System.Drawing.Point(163, 101)
Me.MaximizeBox = False
Me.MinimizeBox = False
Me.Name = "Form1"
Me.RightToLeft = System.Windows.Forms.RightToLeft.No
Me.StartPosition = System.Windows.Forms.FormStartPosition.Manual
Me.Text = "CPU使用情况"
CType(Me.pb1, System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)
End Sub
#End Region
Dim hQuery As Integer
Dim Counters(99) As CounterInfo
Dim currentCounterIdx As Integer
Dim iPerformanceDetail As PERF_DETAIL
Private Sub AddCounter(ByRef strCounterName As String, ByRef hQuery As Integer)
Dim pdhStatus As PDH_STATUS
Dim hCounter As Integer
pdhStatus = PdhVbAddCounter(hQuery, strCounterName, hCounter)
Counters(currentCounterIdx).hCounter = hCounter
Counters(currentCounterIdx).strName = strCounterName
currentCounterIdx = currentCounterIdx + 1
End Sub
' 显示利用率信息
Private Sub UpdateValues()
Dim dblCounterValue As Double
Dim pdhStatus As Integer
Dim strInfo As String
Dim i As Integer
PdhCollectQueryData(hQuery)
i = 0
dblCounterValue = PdhVbGetDoubleCounterValue(Counters(i).hCounter, pdhStatus)
' 将CPU利用率信息显示在标签中
If (pdhStatus = PDH_STATUS.PDH_CSTATUS_VALID_DATA) Or (pdhStatus = PDH_STATUS.PDH_CSTATUS_NEW_DATA) Then
strInfo = "CPU 使用率: " & VB6.Format(dblCounterValue, "0.00")
pb1.Value = dblCounterValue
Me.Text = VB6.Format(dblCounterValue, "0") & "% - CPU 当前状态"
End If
Label1.Text = strInfo
End Sub
Private Sub Form1_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
Dim pdhStatus As PDH_STATUS
pdhStatus = PdhOpenQuery(0, 1, hQuery)
If pdhStatus <> ERROR_SUCCESS Then
MsgBox("OpenQuery failed")
End
End If
' 添加处理器查询
AddCounter("\Processor(0)\% Processor Time", hQuery)
UpdateValues()
Timer1.Enabled = True
End Sub
' 更新CPU利用率信息
Private Sub Timer1_Tick(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Timer1.Tick
UpdateValues()
End Sub
Private Sub Form1_Closed(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Closed
Timer1.Enabled = False
PdhCloseQuery(hQuery)
End Sub
End Class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -