如何获取CPU和内存及C盘的使用率
请问各位朋友:如何获取CPU和内存及C盘的使用率,见下面的代码,请各位朋友指教。#include <ButtonConstants.au3>#include <GUIConstantsEx.au3>
#include <ProgressConstants.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI section ### Form=
$Form1_1 = GUICreate("Form1", 433, 278, -1,-1)
$Group1 = GUICtrlCreateGroup("常用软件", 8, 13, 193, 210)
$Checkbox1 = GUICtrlCreateCheckbox("Checkbox1", 24, 40, 80, 25)
$Checkbox2 = GUICtrlCreateCheckbox("Checkbox2", 24, 96, 80, 25)
$Checkbox2 = GUICtrlCreateCheckbox("Checkbox3", 24, 160, 80, 17)
GUICtrlCreateGroup("", -99, -99, 1, 1)
$Group2 = GUICtrlCreateGroup("系统资源", 216, 13, 209, 210)
$Label1 = GUICtrlCreateLabel("CPU 使用率:", 232, 48, 100, 17)
$Label2 = GUICtrlCreateLabel("内存使用率:", 232, 104, 100, 17)
$Label3 = GUICtrlCreateLabel("C 盘使用率:", 232, 160, 100, 17)
$Progress1 = GUICtrlCreateProgress(232, 72, 185, 10)
$Progress2 = GUICtrlCreateProgress(232, 128, 185, 10)
$Progress3 = GUICtrlCreateProgress(232, 184, 185, 10)
GUICtrlCreateGroup("", -99, -99, 1, 1)
$Button1 = GUICtrlCreateButton("安装", 176, 232, 73, 33)
$Button2 = GUICtrlCreateButton("退出", 264, 232, 73, 33)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
Case $Button2
Exit
EndSwitch
WEnd
随便添加了下 自己去优化
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <ProgressConstants.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI section ### Form=
$var = DriveSpaceFree("C:\")
$hdd=Ceiling($var/1024)
$Form1_1 = GUICreate("Form1", 433, 278, -1,-1)
$Group1 = GUICtrlCreateGroup("常用软件", 8, 13, 193, 210)
$Checkbox1 = GUICtrlCreateCheckbox("Checkbox1", 24, 40, 80, 25)
$Checkbox2 = GUICtrlCreateCheckbox("Checkbox2", 24, 96, 80, 25)
$Checkbox2 = GUICtrlCreateCheckbox("Checkbox3", 24, 160, 80, 17)
GUICtrlCreateGroup("", -99, -99, 1, 1)
$Group2 = GUICtrlCreateGroup("系统资源", 216, 13, 209, 210)
$Label1 = GUICtrlCreateLabel("CPU 使用率:", 232, 48, 100, 17)
$Label2 = GUICtrlCreateLabel("内存使用率:", 232, 104, 100, 17)
$Label3 = GUICtrlCreateLabel("C 盘使用率(GB):", 232, 160, 100, 17)
$Label4 = GUICtrlCreateLabel($hdd, 330, 160, 100, 17)
$Progress1 = GUICtrlCreateProgress(232, 72, 185, 10)
$Progress2 = GUICtrlCreateProgress(232, 128, 185, 10)
$Progress3 = GUICtrlCreateProgress(232, 184, 185, 10)
GUICtrlSetData($Progress3,$hdd)
GUICtrlCreateGroup("", -99, -99, 1, 1)
$Button1 = GUICtrlCreateButton("安装", 176, 232, 73, 33)
$Button2 = GUICtrlCreateButton("退出", 264, 232, 73, 33)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
AdlibRegister("CpuRamRate",500)
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
Case $Button2
Exit
EndSwitch
WEnd
Func CpuRamRate()
Local $rCPU,$rRAM
$rCPU=CurrentCPURate()
$rRAM=CurrentRAMRate()
GUICtrlSetData($Progress1,$rCPU)
GUICtrlSetData($Progress2,$rRAM)
EndFunc
Func CurrentCPURate()
$wbemServices = ObjGet("winmgmts:\\.\root\cimv2")
$wbemObjectSet= $wbemServices.ExecQuery("Select * from Win32_Processor")
For $wbemObject In $wbemObjectSet
$dd=$wbemObject.LoadPercentage
Next
Return $dd
EndFunc
Func CurrentRAMRate()
Local $Array
$Array=MemGetStats()
Return $Array
EndFunc
本帖最后由 dnvplj 于 2015-8-2 07:48 编辑
回复 2# laomeng
非常感,能否帮添加:如,内存使用率:.6/2GB;CPU 使用率:30%;运行程序后退不出来,点“退出”和X都退不出来。 卡的话自己写 事件模式吧
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <ProgressConstants.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI section ### Form=
$var = DriveSpaceFree("C:\")
$hdd=Ceiling($var/1024)
$Form1_1 = GUICreate("Form1", 433, 278, -1,-1)
$Group1 = GUICtrlCreateGroup("常用软件", 8, 13, 193, 210)
$Checkbox1 = GUICtrlCreateCheckbox("Checkbox1", 24, 40, 80, 25)
$Checkbox2 = GUICtrlCreateCheckbox("Checkbox2", 24, 96, 80, 25)
$Checkbox2 = GUICtrlCreateCheckbox("Checkbox3", 24, 160, 80, 17)
GUICtrlCreateGroup("", -99, -99, 1, 1)
$Group2 = GUICtrlCreateGroup("系统资源", 216, 13, 209, 210)
$Label1 = GUICtrlCreateLabel("CPU 使用率:", 232, 48, 100, 17)
$Label2 = GUICtrlCreateLabel("内存使用率:", 232, 104, 100, 17)
$Label3 = GUICtrlCreateLabel("C 盘使用率(GB):", 232, 160, 100, 17)
$Label4 = GUICtrlCreateLabel($hdd, 330, 160, 100, 17)
$Label5 = GUICtrlCreateLabel("", 310, 48, 100, 17)
$Label6 = GUICtrlCreateLabel("", 310, 104, 100, 17)
$Progress1 = GUICtrlCreateProgress(232, 72, 185, 10)
$Progress2 = GUICtrlCreateProgress(232, 128, 185, 10)
$Progress3 = GUICtrlCreateProgress(232, 184, 185, 10)
GUICtrlSetData($Progress3,$hdd)
GUICtrlCreateGroup("", -99, -99, 1, 1)
$Button1 = GUICtrlCreateButton("安装", 176, 232, 73, 33)
$Button2 = GUICtrlCreateButton("退出", 264, 232, 73, 33)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
AdlibRegister("CpuRamRate",500)
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
Case $Button2
Exit
EndSwitch
WEnd
Func CpuRamRate()
Local $rCPU,$rRAM
$rCPU=CurrentCPURate()
$rRAM=CurrentRAMRate()
GUICtrlSetData($Progress1,$rCPU)
GUICtrlSetData($Progress2,$rRAM)
GUICtrlSetData($Label5,$rCPU&"%")
GUICtrlSetData($Label6,$rRAM&"%")
EndFunc
Func CurrentCPURate()
$wbemServices = ObjGet("winmgmts:\\.\root\cimv2")
$wbemObjectSet= $wbemServices.ExecQuery("Select * from Win32_Processor")
For $wbemObject In $wbemObjectSet
$dd=$wbemObject.LoadPercentage
Next
Return $dd
EndFunc
Func CurrentRAMRate()
Local $Array
$Array=MemGetStats()
Return $Array
EndFunc
回复 4# laomeng
朋友,和2楼的效果好像没有什么变化。 不是添加了百分比显示么{:face (382):} 回复 7# paesn
不用谢我,这是我的求助帖,正在朋友的帮助下完善他。 回复 6# laomeng
谢谢分享!学习了
回复 9# chzj589
为何有“AdlibRegister("CpuRamRate",500)”,窗体就退不出来呢?请问,如何解决这一问题。 回复 10# dnvplj
用事件模式就可以退出 回复 11# chzj589
请问,什么是:用事件模式就可以退出,能给个小例子吗?多谢。 这个功能 你还可以使用 kernel32.dll 中的 GetSystemTimes 函数去完成。
楼主 是不是一点也不会这个au3????? 回复 12# dnvplj
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
Opt("GUIOnEventMode", 1)
GUI_D()
While 1
Sleep(1000)
WEnd
Func GUI_D()
$Form1 = GUICreate("CPU内存使用率", 306, 100, -1, -1)
GUISetOnEvent($GUI_EVENT_CLOSE, "Form1Close")
GUISetState(@SW_SHOW)
EndFunc ;==>GUI_D
Func Form1Close()
Exit
EndFunc ;==>Form1Close 回复 14# chzj589
出来个副窗体,什么也没有显示,能关闭。 回复 15# dnvplj
你不会自己添加吗?这就是主窗口
页:
[1]