VB 怎么获取 CPU温度和硬盘温度
首先运行VB 6中文版,建立一个标准的exe工程,将窗体命名为frmmain,borderstyle属性设为:3-fixed,caption,属性设置为空;接着添加一个frame控件——“frame1”,caption属性设置为空,再添加picturebox控件pctPrg,前景色为绿色,背景色为浅红色;最后添加一个定时器控件——“timer”,interval设置为100(当然,也可以根据自己的需要设置读取间隔),enabled设置为true,其他的属性采用系统的默认值,调整窗体和控件到适当位置和适当大小即可(参见附图)。
成都创新互联公司是一家专注于成都做网站、网站制作与策划设计,唐山网站建设哪家好?成都创新互联公司做网站,专注于网站建设十余年,网设计领域的专业建站公司;建站业务涵盖:唐山等地区。唐山做网站价格咨询:18980820575
2.编写程序代码,双击窗体frmmain并输入以下代码
Option Explicit'变量检查
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 Const REG_DWORD = 4
Private Const HKEY_DYN_DATA = H80000006
'这两个常数是读写注册表必须的
'以上API函数和常数结合能够实现对注册表任意指定项的访问
Private Avg As Long
'CPU 资源占用参数平均数
Private Sum As Long
'CPU 资源占用参数合计数
Private Index As Long '统计次数
'窗体级函数、常数和变量声明结束
Private Function GetCPUUsage() As Long
On Error Resume Next
'忽略可能产生的错误
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 Form_Load()
'初始化标题和提示信息
Dim hKey As Long, Data As Long
App.Title = "CPU监视器 占有率:"
Me.Caption = App.Title
'初始化应用程序标题栏
pctPrg.ToolTipText = "单击右键退出程序!"
RegOpenKey HKEY_DYN_DATA, "PerfStats\ StartStat", hKey
RegQueryValueEx hKey, "KERNEL\CPUUsage", 0, REG_DWORD, Data, 4
RegCloseKey hKey
'打开注册表的资源监控标识
End Sub
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
'程序结束前关闭定时器
Timer.Enabled = False
End Sub
Private Sub Form_Unload(Cancel As Integer)
End'结束程序
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 If
End Sub
Private Sub Timer_Timer()
'利用定时器周期性地读取资源状况
'本程序每隔0.1秒读取一次数据
Dim tmp As Long ' CPU资源占用状况参数
tmp = GetCPUUsage '获得参数
Me.Caption = App.Title FormatPercent(tmp / 100) '
'在窗体标题栏用百分比数字形式反映占用率
Sum = Sum + tmp '累加统计
Index = Index + 1 '次数统计
Avg = Int(Sum / Index) ' 求平均值
pctPrg.Cls ' 清除图示内容
pctPrg.Line (0, 0)-(tmp, 18), , BF
'以绿色图示资源占用状况
pctPrg.Line (Avg, 0)-(Avg, 18), HFF
'以深红色画出平均线
pctPrg.Line (Avg + 1, 0)-(Avg + 1, 18), HFF
End Sub
'程序结束
通过以上步骤,装备精良的CPU监视器就制作完成了。
注意事项
需要说明的是,在内存配置较低的计算机中(比如内存32MB,Windows 98第二版),CPU的占有率是较高的,一般能够达到50%,这可能是由于系统需要CPU进行许多的运算模拟来弥补资源的不足。然而在88MB内存,同样操作系统的机器上,占有率一般只有10%。另外,本文主窗体中定时器运行过程函数中调用的FormatPercent函数是VB 6.0新添加的一个函数,它的作用是将一个表达式转化成百分数。用VB 5.0开发的读者需要采取其他变通方法,以实现监控CPU功能。本程序在Windows 98第二版、VB 6.0中文企业版下测试通过。
VB 如何得到CPU温度呢?
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 u.CurrentTemperature
Next
Set mCPU = Nothing
GetCPUTemp = (s - 2732) / 10
End Function
Private Sub Form_Load()
Timer1.Interval = 500
End Sub
Private Sub Timer1_Timer()
Print GetCPUTemp()
End Sub
如何用VB.Net进行CPU温度读取
没有命令可以查看CPU温度,可以通过第三方软件鲁大师查看CPU温度:
百度搜索鲁大师,并进行下载安装;
运行已安装的软件的,在界面中就能看到CPU温度:
VB如何获取硬件信息比如CPU温度,显卡,CPUID号之类的、
Dim MemoyCounter As New ComputerInfo()
Dim CPUCounter As New PerformanceCounter("Processor", "% Processor Time", "_Total")
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Dim CPU As Double = CPUCounter.NextValue
Dim neicun As Double = (MemoyCounter.TotalPhysicalMemory - MemoyCounter.AvailablePhysicalMemory) / MemoyCounter.TotalPhysicalMemory * 100%
获取CPU及物理内存使用率
本文题目:vb.netcpu温度 vbnet c
地址分享:http://scgulin.cn/article/dddegpg.html