找回密码
 加入
搜索
查看: 6250|回复: 14

[系统综合] 如何获取CPU和内存及C盘的使用率

 火.. [复制链接]
发表于 2015-8-1 11:16:22 | 显示全部楼层 |阅读模式
请问各位朋友:如何获取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
发表于 2015-8-1 17:49:25 | 显示全部楼层
随便添加了下 自己去优化
#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[7]
        $Array=MemGetStats()
        Return $Array[0]
        EndFunc
 楼主| 发表于 2015-8-1 18:45:30 | 显示全部楼层
本帖最后由 dnvplj 于 2015-8-2 07:48 编辑

回复 2# laomeng
非常感,能否帮添加:如,内存使用率:.6/2GB;CPU 使用率:30%;运行程序后退不出来,点“退出”和X都退不出来。
发表于 2015-8-3 18:23:09 | 显示全部楼层
卡的话自己写 事件模式吧
#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[7]
        $Array=MemGetStats()
        Return $Array[0]
        EndFunc
 楼主| 发表于 2015-8-3 19:32:20 | 显示全部楼层
回复 4# laomeng
朋友,和2楼的效果好像没有什么变化。
发表于 2015-8-3 19:49:59 | 显示全部楼层
不是添加了百分比显示么
 楼主| 发表于 2015-8-7 11:23:17 | 显示全部楼层
回复 7# paesn
不用谢我,这是我的求助帖,正在朋友的帮助下完善他。
发表于 2015-8-8 15:08:48 | 显示全部楼层
回复 6# laomeng
谢谢分享!学习了

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?加入

×
 楼主| 发表于 2015-8-9 00:08:34 | 显示全部楼层
回复 9# chzj589
为何有“AdlibRegister("CpuRamRate",500)”,窗体就退不出来呢?请问,如何解决这一问题。
发表于 2015-8-9 08:56:42 | 显示全部楼层
回复 10# dnvplj

用事件模式就可以退出
 楼主| 发表于 2015-8-9 12:11:00 | 显示全部楼层
回复 11# chzj589
请问,什么是:用事件模式就可以退出,能给个小例子吗?多谢。
发表于 2015-8-9 13:25:43 | 显示全部楼层
这个功能 你还可以使用 kernel32.dll 中的 GetSystemTimes 函数去完成。

楼主 是不是一点也不会这个au3?????
发表于 2015-8-9 14:04:26 | 显示全部楼层
回复 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
 楼主| 发表于 2015-8-9 18:13:54 | 显示全部楼层
回复 14# chzj589
出来个副窗体,什么也没有显示,能关闭。
发表于 2015-8-9 19:14:25 | 显示全部楼层
回复 15# dnvplj
你不会自己添加吗?这就是主窗口
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-5-3 05:35 , Processed in 0.091334 second(s), 24 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表