找回密码
 加入
搜索
查看: 439|回复: 1

获取CPU和内存以及C盘的使用率

[复制链接]
发表于 2025-8-30 09:12:45 | 显示全部楼层 |阅读模式
类似这样的效果AU3如何实现呢?



本帖子中包含更多资源

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

×
发表于 2025-9-2 13:59:48 | 显示全部楼层
AI给出的,需要修改
#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
您需要登录后才可以回帖 登录 | 加入

本版积分规则

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

GMT+8, 2026-1-17 20:32 , Processed in 0.580673 second(s), 21 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2025 Discuz! Team.

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