#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <StaticConstants.au3>
; 创建GUI窗口
GUICreate("系统资源监控", 400, 250)
GUISetFont(9, 400)
; CPU使用率
GUICtrlCreateLabel("CPU使用率:", 20, 20, 80, 20)
$cpuProgress = GUICtrlCreateProgress(100, 20, 200, 20)
$cpuLabel = GUICtrlCreateLabel("0%", 310, 20, 40, 20)
; 内存使用率
GUICtrlCreateLabel("内存使用率:", 20, 60, 80, 20)
$memProgress = GUICtrlCreateProgress(100, 60, 200, 20)
$memLabel = GUICtrlCreateLabel("0%", 310, 60, 40, 20)
; C盘使用率
GUICtrlCreateLabel("C盘使用率:", 20, 100, 80, 20)
$diskProgress = GUICtrlCreateProgress(100, 100, 200, 20)
$diskLabel = GUICtrlCreateLabel("0%", 310, 100, 40, 20)
; 状态更新标签
$statusLabel = GUICtrlCreateLabel("最后更新: " & @HOUR & ":" & @MIN & ":" & @SEC, 20, 140, 200, 20)
; 退出按钮
$exitButton = GUICtrlCreateButton("退出", 150, 180, 100, 30)
GUISetState()
; 获取内存使用率的函数
Func GetMemoryUsage()
Local $memStats = MemGetStats()
If @error Then Return 0
; $memStats[0] 是内存负载百分比
Return $memStats[0]
EndFunc
; 获取CPU使用率的函数
Func GetCPUUsage()
; 第一次测量
Local $aIdle1 = DllCall("kernel32.dll", "int", "GetTickCount")
If @error Then Return 0
Local $aTimes1 = _GetSystemTimes()
If @error Then Return 0
; 等待一秒
Sleep(1000)
; 第二次测量
Local $aIdle2 = DllCall("kernel32.dll", "int", "GetTickCount")
If @error Then Return 0
Local $aTimes2 = _GetSystemTimes()
If @error Then Return 0
; 计算CPU使用率
Local $iIdleTime = $aTimes2[0] - $aTimes1[0] ; 空闲时间差异
Local $iTotalTime = $aTimes2[1] - $aTimes1[1] ; 总时间差异
If $iTotalTime = 0 Then Return 0
Local $iCpuUsage = 100 * (1 - $iIdleTime / $iTotalTime)
Return Round($iCpuUsage)
EndFunc
; 辅助函数:获取系统时间信息
Func _GetSystemTimes()
Local $aResult = DllCall("kernel32.dll", "int", "GetSystemTimes", "int*", 0, "int*", 0, "int*", 0)
If @error Then Return SetError(1, 0, 0)
Return $aResult
EndFunc
; 获取C盘使用率的函数
Func GetDiskUsage()
Local $iTotal = DriveSpaceTotal("C:")
Local $iFree = DriveSpaceFree("C:")
If $iTotal = 0 Then Return 0
Local $iUsage = 100 * ($iTotal - $iFree) / $iTotal
Return Round($iUsage)
EndFunc
; 主循环
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE, $exitButton
ExitLoop
EndSwitch
; 更新资源使用率(每2秒更新一次)
If Mod(@SEC, 2) = 0 Then
; 获取CPU使用率
$cpuUsage = GetCPUUsage()
GUICtrlSetData($cpuProgress, $cpuUsage)
GUICtrlSetData($cpuLabel, $cpuUsage & "%")
; 获取内存使用率
$memUsage = GetMemoryUsage()
GUICtrlSetData($memProgress, $memUsage)
GUICtrlSetData($memLabel, $memUsage & "%")
; 获取C盘使用率
$diskUsage = GetDiskUsage()
GUICtrlSetData($diskProgress, $diskUsage)
GUICtrlSetData($diskLabel, $diskUsage & "%")
; 更新状态时间
GUICtrlSetData($statusLabel, "最后更新: " & @HOUR & ":" & @MIN & ":" & @SEC)
; 短暂延迟防止过于频繁更新
Sleep(100)
EndIf
Sleep(100)
WEnd