📄 form1.frm
字号:
VERSION 5.00
Object = "{831FDD16-0C5C-11D2-A9FC-0000F8754DA1}#2.0#0"; "MSCOMCTL.OCX"
Begin VB.Form Form1
Caption = "检测CPU"
ClientHeight = 1065
ClientLeft = 2445
ClientTop = 1515
ClientWidth = 5340
LinkTopic = "Form1"
MaxButton = 0 'False
MinButton = 0 'False
ScaleHeight = 1065
ScaleWidth = 5340
Begin MSComctlLib.ProgressBar pb1
Height = 375
Left = 0
TabIndex = 1
Top = 600
Width = 5295
_ExtentX = 9340
_ExtentY = 661
_Version = 393216
Appearance = 1
Scrolling = 1
End
Begin VB.Timer Timer1
Enabled = 0 'False
Interval = 1000
Left = 2160
Top = 4200
End
Begin VB.Label Label1
Height = 375
Left = 120
TabIndex = 0
Top = 120
Width = 4935
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
Private Declare Function PdhVbGetOneCounterPath Lib "PDH.DLL" ( _
ByVal PathString As String, ByVal PathLength As Long, _
ByVal DetailLevel As Long, ByVal CaptionString As String) As Long
Private Declare Function PdhVbCreateCounterPathList Lib "PDH.DLL" _
(ByVal PERF_DETAIL As Long, ByVal CaptionString As String) As Long
Private Declare Function PdhVbGetCounterPathFromList Lib "PDH.DLL" _
(ByVal Index As Long, ByVal Buffer As String, _
ByVal BufferLength As Long) As Long
Private Declare Function PdhOpenQuery Lib "PDH.DLL" (ByVal Reserved As Long, _
ByVal dwUserData As Long, ByRef hQuery As Long) As PDH_STATUS
Private Declare Function PdhCloseQuery Lib "PDH.DLL" (ByVal hQuery As Long) As PDH_STATUS
Private Declare Function PdhVbAddCounter Lib "PDH.DLL" _
(ByVal QueryHandle As Long, ByVal CounterPath As String, _
ByRef CounterHandle As Long) As PDH_STATUS
Private Declare Function PdhCollectQueryData Lib "PDH.DLL" _
(ByVal QueryHandle As Long) As PDH_STATUS
Private Declare Function PdhVbIsGoodStatus Lib "PDH.DLL" (ByVal StatusValue As Long) As Long
Private Declare Function PdhVbGetDoubleCounterValue Lib "PDH.DLL" _
(ByVal CounterHandle As Long, ByRef CounterStatus As Long) As Double
' 枚举常数
Enum PERF_DETAIL
PERF_DETAIL_NOVICE = 100
PERF_DETAIL_ADVANCED = 200
PERF_DETAIL_EXPERT = 300
PERF_DETAIL_WIZARD = 400
End Enum
Enum PDH_STATUS
PDH_CSTATUS_VALID_DATA = &H0
PDH_CSTATUS_NEW_DATA = &H1
PDH_CSTATUS_NO_MACHINE = &H800007D0
PDH_CSTATUS_NO_INSTANCE = &H800007D1
PDH_MORE_DATA = &H800007D2
PDH_CSTATUS_ITEM_NOT_VALIDATED = &H800007D3
PDH_RETRY = &H800007D4
PDH_NO_DATA = &H800007D5
PDH_CALC_NEGATIVE_DENOMINATOR = &H800007D6
PDH_CALC_NEGATIVE_TIMEBASE = &H800007D7
PDH_CALC_NEGATIVE_VALUE = &H800007D8
PDH_DIALOG_CANCELLED = &H800007D9
PDH_CSTATUS_NO_OBJECT = &HC0000BB8
PDH_CSTATUS_NO_COUNTER = &HC0000BB9
PDH_CSTATUS_INVALID_DATA = &HC0000BBA
PDH_MEMORY_ALLOCATION_FAILURE = &HC0000BBB
PDH_INVALID_HANDLE = &HC0000BBC
PDH_INVALID_ARGUMENT = &HC0000BBD
PDH_FUNCTION_NOT_FOUND = &HC0000BBE
PDH_CSTATUS_NO_COUNTERNAME = &HC0000BBF
PDH_CSTATUS_BAD_COUNTERNAME = &HC0000BC0
PDH_INVALID_BUFFER = &HC0000BC1
PDH_INSUFFICIENT_BUFFER = &HC0000BC2
PDH_CANNOT_CONNECT_MACHINE = &HC0000BC3
PDH_INVALID_PATH = &HC0000BC4
PDH_INVALID_INSTANCE = &HC0000BC5
PDH_INVALID_DATA = &HC0000BC6
PDH_NO_DIALOG_DATA = &HC0000BC7
PDH_CANNOT_READ_NAME_STRINGS = &HC0000BC8
End Enum
Const ERROR_SUCCESS = 0
Private Type CounterInfo
hCounter As Long
strName As String
End Type
Dim hQuery As Long
Dim Counters(0 To 99) As CounterInfo
Dim currentCounterIdx As Long
Dim iPerformanceDetail As PERF_DETAIL
Private Sub AddCounter(strCounterName As String, hQuery As Long)
Dim pdhStatus As PDH_STATUS
Dim hCounter As Long
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 Long
Dim strInfo As String
Dim i As Long
PdhCollectQueryData (hQuery)
i = 0
dblCounterValue = PdhVbGetDoubleCounterValue(Counters(i).hCounter, pdhStatus)
' 将CPU利用率信息显示在标签中
If (pdhStatus = PDH_CSTATUS_VALID_DATA) Or (pdhStatus = PDH_CSTATUS_NEW_DATA) Then
strInfo = "CPU Usage: " & Format$(dblCounterValue, "0.00")
pb1.Value = dblCounterValue
Me.Caption = Format$(dblCounterValue, "0") & "% - CPU Status"
End If
Label1 = strInfo
End Sub
Private Sub Form_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_Timer()
UpdateValues
End Sub
Private Sub Form_Unload(Cancel As Integer)
Timer1.Enabled = False
PdhCloseQuery (hQuery)
End Sub
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -