VB 如何得到CPU温度呢

VB 如何得到CPU温度呢,第1张

Public Function GetCPUTemp() As Double

Dim i As Integer

Dim mCPU As Variant

Dim u As Variant

Dim s As String

Set mCPU = GetObject("WINMGMTS:{impersonationLevel=impersonate}!root\wmi")ExecQuery("SELECT CurrentTemperature From MSAcpi_ThermalZoneTemperature")

For Each u In mCPU

s = s & uCurrentTemperature

Next

Set mCPU = Nothing

GetCPUTemp = (s - 2732) / 10

End Function

Private Sub Form_Load()

Timer1Interval = 500

End Sub

Private Sub Timer1_Timer()

Print GetCPUTemp()

End Sub

抄的代码,如果能用,采纳时选能解决。FrmMain

Option Explicit

'

' CPUUsage 10

' ----------------

'

' This program is total FREEWARE

'

' It use the registry to get the CPU usage in percent

'

' You can also move the form without title bar and close it with the right button

'

' Happy programming !

'

'

'

' The CPUUsage object

Private CPU As New CPUUsage

Private Avg As Long ' Average of CPU Usage

Private Sum As Long

Private Index As Long

Private Sub Form_Load()

' First, open the "StartStat " key

CPUInitCPUUsage

End Sub

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)

TimerEnabled = False

Set CPU = Nothing

Cancel = 0

End Sub

Private Sub Frame1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)

Call Form_MouseDown(Button, Shift, X, Y)

End Sub

Private Sub pctPrg_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)

If Button = 2 Then

Unload Me

End

End If

Call Form_MouseDown(Button, Shift, X, Y)

End Sub

Private Sub Timer_Timer()

'Get the CPU Usage every second

Dim tmp As Long

tmp = CPUGetCPUUsage

Sum = Sum + tmp

Index = Index + 1

Avg = Int(Sum / Index)

'Draw the bar

pctPrgCls

pctPrgLine (0, 0)-(tmp, 18), , BF

pctPrgLine (Avg, 0)-(Avg, 18), &HFF

pctPrgLine (Avg + 1, 0)-(Avg + 1, 18), &HFF

End Sub

Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)

Dim rc As Long

rc = ReleaseCapture

rc = SendMessage(hwnd, WM_NCLBUTTONDOWN, LP_HT_CAPTION, ByVal 0&)

End Sub

Mudula Api

Option Explicit

'Const&Functions used for the FormMove methods

Public Const LP_HT_CAPTION = 2

Public Const WM_NCLBUTTONDOWN = &HA1

Declare Function ReleaseCapture Lib "user32 " () As Long

Declare Function SendMessage Lib "user32 " Alias "SendMessageA " (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long

cls:CPUUsage

Option Explicit

Private Declare Function RegQueryValueEx Lib "advapi32dll " 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 "advapi32dll " Alias "RegOpenKeyA " (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long

Private Declare Function RegCloseKey Lib "advapi32dll " (ByVal hKey As Long) As Long

Private Const REG_DWORD = 4

Private Const HKEY_DYN_DATA = &H80000006

'Initiate the key

Public Sub InitCPUUsage()

Dim Data As Long, Typ As Long, Size As Long

Dim hKey As Long, hRet As Long

hRet = RegOpenKey(HKEY_DYN_DATA, "PerfStats\StartStat ", hKey)

hRet = RegQueryValueEx(hKey, "KERNEL\CPUUsage ", 0, REG_DWORD, Data, 4)

hRet = RegCloseKey(hKey)

End Sub

'Get the cpu info via gfx meter

Public Function GetCPUUsage() As Long

Dim Data As Long, Typ As Long, Size As Long

Dim hKey As Long

Dim hRet As Long

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

hRet = RegQueryValueEx(hKey, "KERNEL\CPUUsage ", 0&, REG_DWORD, Data, 4)

GetCPUUsage = Data

hRet = RegCloseKey(hKey)

End Function

来个简单点的:

Private Sub Command1_Click()

Dim strComputer As String, objWMIService, colComputers, objComputer, objProcess

strComputer = ""

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set colComputers = objWMIServiceInstancesOf("Win32_Processor ")

For Each objComputer In colComputers

If objComputerLoadPercentage >= 70 Then '如果CPU 超过70%

Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\\root\cimv2")

Set colComputers = objWMIServiceExecQuery _

("Select from Win32_Process Where Name='iexploreexe'")

For Each objProcess In colComputers

objComputerTerminate '就结束该进程iexploreexe

Next

Exit For

End If

Next

Set objComputer = Nothing

Set colComputers = Nothing

Set objWMIService = Nothing

End Sub

说明:如果你循环使用上述功能,则应加入控件timer,然后复制代码到Private Sub Timer1_Timer()内即可。

以上就是关于VB 如何得到CPU温度呢全部的内容,包括:VB 如何得到CPU温度呢、vb程序CPU占用率、VB 判断进程 CPU 超过70%就结束等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zz/10118666.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-05
下一篇 2023-05-05

发表评论

登录后才能评论

评论列表(0条)

保存