问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501

VB 查看进程CPU使用率

发布网友 发布时间:2022-04-25 11:10

我来回答

1个回答

热心网友 时间:2022-04-24 06:13

Windows 9x下是通过读取注册表获取CPU占用,但是只能是整体,不可能细分到每个进程

Private Type LARGE_INTEGER
lowpart As Long
highpart As Long
End Type

Private Declare Function QueryPerformanceCounter Lib _
"kernel32" (lpPerformanceCount As LARGE_INTEGER) _
As Long
Private Declare Function QueryPerformanceFrequency _
Lib "kernel32" (lpFrequency As LARGE_INTEGER) As Long

Private Const REG_DWORD = 4 ' 32-bit number
Private Const HKEY_DYN_DATA = &H80000006

Private Declare Function RegQueryValueEx Lib _
"advapi32.dll" Alias "RegQueryValueExA" (ByVal hKey _
As Long, ByVal lpValueName As String, ByVal _
lpReserved As Long, lpType As Long, lpData _
As Any, lpcbData As Long) As Long

Private Declare Function RegOpenKey Lib "advapi32.dll" _
Alias "RegOpenKeyA" (ByVal hKey As Long, ByVal _
lpSubKey As String, phkResult As Long) As Long

Private Declare Function RegCloseKey Lib "advapi32.dll" _
(ByVal hKey As Long) As Long

Private Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal x As Long, ByVal y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long

Private Sub Form_DblClick()
'If the mouse is double clicked then end the
'program.
End

End Sub

Private Sub Form_Load()
'Positions the form at the top right corner of
'the screen regardless of monitor size.
Form1.Top = 1
Form1.Left = Screen.Width - Form1.Width

Call InitCPU
Call OnTop
End Sub

Private Sub SSPanel1_DblClick(Index As Integer)
Call Form_DblClick
End Sub

Private Sub Timer1_Timer()
Dim lData As Long, lType As Long, lSize As Long
Dim hKey As Long

Qry = RegOpenKey(HKEY_DYN_DATA, "PerfStats\StatData", hKey)

'If there's a problem accessing the registry
If Qry <> 0 Then
MsgBox "Can't Open Statistics Key"
End
End If

lType = REG_DWORD
lSize = 4

'Querying the registry for CPUUsage
Qry = RegQueryValueEx(hKey, "KERNEL\CPUUsage", _
0, lType, lData, lSize)

'clears the SSPanel boxes
Do Until SSPanel1(x).BackColor = &HC0C0C0
SSPanel1(x).BackColor = &HC0C0C0
x = x + 1
If x >= 10 Then Exit Do
Loop

'statbar is the variable that holds the CPU
'usage divided by 10.
'(ex. if 79% of the CPU is being used then
' statbar will hold the int(7.9) = 8)
statbar = Int(lData / 10)
If statbar >= 1 Then statbar = statbar - 1

'used to fill the SSPanel with the color green
'beginning with 0 and ending with the value of
'statbar.
For fillall = 0 To statbar
SSPanel1(fillall).BackColor = &HFF&
Next fillall

If Int(lData / 10) = 0 Then SSPanel1(0).BackColor = &HC0C0C0
'Print lData
'Label2.Caption = lData & "%"
Qry = RegCloseKey(hKey)

End Sub

Private Sub InitCPU()
Dim lData As Long, lType As Long, lSize As Long
Dim hKey As Long
Qry = RegOpenKey(HKEY_DYN_DATA, "PerfStats\StartStat", hKey)
If Qry <> 0 Then
MsgBox "Can't Open Statistics Key"
End
End If

lType = REG_DWORD
lSize = 4
Qry = RegQueryValueEx(hKey, "KERNEL\CPUUsage", 0, lType, lData, lSize)
Qry = RegCloseKey(hKey)

End Sub

Private Sub OnTop()
Const SWP_NOMOVE = &H2
Const SWP_NOSIZE = &H1
Const FLAGS = SWP_NOMOVE Or SWP_NOSIZE
Const HWND_TOPMOST = -1
Const HWND_NOTOPMOST = -2

If SetWindowPos(Form1.hwnd, HWND_TOPMOST, 0, 0, 0, 0, FLAGS) = True Then
success% = SetWindowPos(Form1.hwnd, HWND_TOPMOST, 0, 0, 0, 0, FLAGS)
End If
End Sub

Windows NT/2000 下
参考一下c的吧,没时间写成VB
主要是使用了Microsoft未公开的 NtQuerySystemInformation
需要ntdll.dll

一个VB获取CPU占用率的代码:
http://www.freevbcode.com/source/0806/processcpuusage.zip
声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com
橙子冷藏能保存多久 橙子会过期吗 新鲜橙子如何保存更久 橙子保鲜剂对人体有害吗 粒上皇开口熟栗120g*5袋(共600g)-详细介绍 三皇王板栗介绍 品牌榜:2024年板栗十大品牌排行榜 投票结果公布【新】 为什么来大姨妈胸会胀 少儿学什么舞蹈 青年学什么舞蹈好 cfgmgr是一条什么命令 安装oracle statspack工具时一直出现sp2-0640:未连接 错误信息 aix软如何安装? oracle perfstat用户可以删么 pfsvperfstats是什么文件 oracle9i 表空间中PERFSTAT是什么意思 oracle的perfstat用户可能出了什么问题,建立statsback的快照也显示下... 微信部分功能被限制,解封后,多久可以正常使用 山西省运城市中等职业学校有哪些? 运城恒泽大酒店招聘信息是真的吗谁知道 急求毕业生户口档案转回原籍山西运城,怎么才能将档案转到太原人才中心... 山西省运城市最近是否招聘教师? 运城工作? 运城有没有招聘淘宝模特的呢? 山西运城空港新区入驻公司有哪些 运城人才市场在哪 运城市人才市场档案转到太原市省人才市场后,运城市人才市场拒给报到证... 运城人才招聘会在南风广场哪里???就是8月29号的那场? 运城有什么公司啊,我查了好多都没有找到运城招聘人员的 运城有人才市场吗? sqlplus perfstat/perfstat 请问如何安装安装Statspack? oracle运行execute statspack.snap出现ORA 人力资源工资一般多少? 考证后人力资源管理师就业薪资是多少? 人力资源管理工资一般是多少 人力资源招聘工资待遇怎么样 人力资源管理师的薪资待遇 人力资源管理毕业后就业和工资怎么样 人力资源行业从业者的薪资待遇和职业前景怎样? 一级人力资源管理师薪资水平和前景怎么样? 人力资源管理这个专业就业怎么样?工资怎么样? 人力资源工资待遇怎么样 人力资源公司招聘专员,薪资待遇怎么给。 请问:考人力资源管理专业的研究生,毕业后工作怎么样??? 唱吧怎么三个人唱? 唱吧如何多人合唱?一人几句的那种 唱吧怎么才可以多人合唱啊 唱吧怎么可以合唱? 唱吧怎样合唱