xiong616 发表于 2010-1-25 19:16:53

谢谢7楼提供代码

鸟人 发表于 2010-1-25 19:31:31

7# 的 不错,

guland 发表于 2010-1-25 21:46:03

:face (33):这个不错哈哈

kemyliu 发表于 2010-1-27 08:38:18

本帖最后由 kemyliu 于 2010-1-27 08:44 编辑

问题得到解决,秀出自己的拙作.
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
Global $IDLETIME, $KERNELTIME, $USERTIME
Global $StartIdle, $StartKernel, $StartUser
Global $EndIdle, $EndKernel, $EndUser
Global $Timer
Dim $HDDDRV,$HDDSP1,$HDDSP2,$HDDSP3,$HDDSP4,$HDDSP5,$HDDSP6
$HDDDRV = DriveGetDrive( "FIXED" )
Dim $HDD
$IDLETIME = DllStructCreate('dword;dword')
$KERNELTIME = DllStructCreate('dword;dword')
$USERTIME = DllStructCreate('dword;dword')
$hGUI = GUICreate('System Info', 200, 100+20*$HDDDRV, -1, -1, -1, $WS_EX_TOPMOST)
GUISetIcon('shell32.dll', 13)
GUICtrlCreateLabel('CPU Usage:', 25, 20, 105, 20)
GUICtrlSetFont(-1, 8.5, 800, Default, 'MS Sans Serif')
$ValueLabel = GUICtrlCreateLabel('', 130, 20, 40, 20)
GUICtrlSetFont(-1, 8.5, 800, Default, 'MS Sans Serif')

GUICtrlCreateLabel('Memory Usage:', 25, 40, 105, 20)
GUICtrlSetFont(-1, 8.5, 800, Default, 'MS Sans Serif')
$Mem = GUICtrlCreateLabel('000', 130, 40, 40, 20)
GUICtrlSetFont(-1, 8.5, 800, Default, 'MS Sans Serif')
If $HDDDRV >= 1 Then
        GUICtrlCreateLabel(StringUpper('Free Space ' & $HDDDRV), 25, 40+20*1, 105, 20)
        GUICtrlSetFont(-1, 8.5, 800, Default, 'MS Sans Serif')
        $HDDSP1= GUICtrlCreateLabel('000', 130, 40+20*1, 40, 20)
        GUICtrlSetFont(-1, 8.5, 800, Default, 'MS Sans Serif')
EndIf
If $HDDDRV >= 2 Then
        GUICtrlCreateLabel(StringUpper( 'Free Space ' & $HDDDRV), 25, 40+20*2, 105, 20)
        GUICtrlSetFont(-1, 8.5, 800, Default, 'MS Sans Serif')
        $HDDSP2= GUICtrlCreateLabel('000', 130, 40+20*2, 40, 20)
        GUICtrlSetFont(-1, 8.5, 800, Default, 'MS Sans Serif')
EndIf
If $HDDDRV >= 3 Then
        GUICtrlCreateLabel(StringUpper('Free Space ' & $HDDDRV), 25, 40+20*3, 105, 20)
        GUICtrlSetFont(-1, 8.5, 800, Default, 'MS Sans Serif')
        $HDDSP3= GUICtrlCreateLabel('000', 130, 40+20*3, 40, 20)
        GUICtrlSetFont(-1, 8.5, 800, Default, 'MS Sans Serif')
EndIf
If $HDDDRV >= 4 Then
        GUICtrlCreateLabel(StringUpper('Free Space ' & $HDDDRV), 25, 40+20*4, 105, 20)
        GUICtrlSetFont(-1, 8.5, 800, Default, 'MS Sans Serif')
        $HDDSP4= GUICtrlCreateLabel('000', 130, 40+20*4, 40, 20)
        GUICtrlSetFont(-1, 8.5, 800, Default, 'MS Sans Serif')
EndIf
If $HDDDRV >= 5 Then
        GUICtrlCreateLabel(StringUpper('Free Space ' & $HDDDRV), 25, 40+20*5, 105, 20)
        GUICtrlSetFont(-1, 8.5, 800, Default, 'MS Sans Serif')
        $HDDSP5= GUICtrlCreateLabel('000', 130, 40+20*5, 40, 20)
        GUICtrlSetFont(-1, 8.5, 800, Default, 'MS Sans Serif')
EndIf
If $HDDDRV >= 6 Then
        GUICtrlCreateLabel(StringUpper('Free Space ' & $HDDDRV), 25, 40+20*6, 105, 20)
        GUICtrlSetFont(-1, 8.5, 800, Default, 'MS Sans Serif')
        $HDDSP6= GUICtrlCreateLabel('000', 130, 40+20*6,40, 20)
        GUICtrlSetFont(-1, 8.5, 800, Default, 'MS Sans Serif')
EndIf
$StartButton = GUICtrlCreateButton('Start', 10, 60+20*$HDDDRV, 75, 23)
$StopButton = GUICtrlCreateButton('Stop', 115, 60+20*$HDDDRV, 75, 23)
GUICtrlSetState(-1, $GUI_DISABLE)
GUISetState()

_GetSysTime($StartIdle, $StartKernel, $StartUser)
$Timer = TimerInit()

GUICtrlSetState($StartButton, $GUI_DISABLE)
GUICtrlSetState($StopButton, $GUI_ENABLE)

While 1
    $msg = GUIGetMsg()
    Switch $msg
      Case $GUI_EVENT_CLOSE
            ExitLoop
      Case $StartButton
            _GetSysTime($StartIdle, $StartKernel, $StartUser)
            $Timer = TimerInit()

            GUICtrlSetState($StartButton, $GUI_DISABLE)
            GUICtrlSetState($StopButton, $GUI_ENABLE)
      Case $StopButton
            GUICtrlSetState($StopButton, $GUI_DISABLE)
            GUICtrlSetState($StartButton, $GUI_ENABLE)

            GUICtrlSetData($ValueLabel, '')
    EndSwitch

    If BitAND(GUICtrlGetState($StopButton), $GUI_ENABLE) And TimerDiff($Timer) >= 1000 Then
      $Mems = MemGetStats ( )      
      ControlSetText($hGUI, "",$Mem, $Mems & "%")
      _GetSysTime($EndIdle, $EndKernel, $EndUser)
      _CPUCalc()
      _GetSysTime($StartIdle, $StartKernel, $StartUser)
               $Timer = TimerInit()
                For $i=1 To $HDDDRV
                        $HDD[$i]=Round(DriveSpaceFree($HDDDRV[$i])/1024,1) & "G"
                Next
               
                If $HDDDRV >= 1 Then
                        GUICtrlSetData($HDDSP1,$HDD)
                EndIf
                If $HDDDRV >= 2 Then
                        GUICtrlSetData($HDDSP2,$HDD)
                EndIf
                If $HDDDRV >= 3 Then
                        GUICtrlSetData($HDDSP3,$HDD)
                EndIf
                If $HDDDRV >= 4 Then
                        GUICtrlSetData($HDDSP4,$HDD)
                EndIf
                If $HDDDRV >= 5 Then
                        GUICtrlSetData($HDDSP5,$HDD)
                EndIf
                If $HDDDRV >= 6 Then
                        GUICtrlSetData($HDDSP6,$HDD)
                EndIf
    EndIf
        Sleep(100)
WEnd
Func _GetSysTime(ByRef $sIdle, ByRef $sKernel, ByRef $sUser)
    DllCall('kernel32.dll', 'int', 'GetSystemTimes', 'ptr', DllStructGetPtr($IDLETIME), _
            'ptr', DllStructGetPtr($KERNELTIME), _
            'ptr', DllStructGetPtr($USERTIME))
    $sIdle = DllStructGetData($IDLETIME, 1)
    $sKernel = DllStructGetData($KERNELTIME, 1)
    $sUser = DllStructGetData($USERTIME, 1)
EndFunc   ;==>_GetSysTime
Func _CPUCalc()
    Local $iSystemTime, $iTotal, $iCalcIdle, $iCalcKernel, $iCalcUser

    $iCalcIdle = ($EndIdle - $StartIdle)
    $iCalcKernel = ($EndKernel - $StartKernel)
    $iCalcUser = ($EndUser - $StartUser)

    $iSystemTime = ($iCalcKernel + $iCalcUser)
    $iTotal = Int(($iSystemTime - $iCalcIdle) * (100 / $iSystemTime)) & '%'

    If GUICtrlRead($ValueLabel) <> $iTotal Then ControlSetText($hGUI, '', $ValueLabel, $iTotal)
EndFunc   ;==>_CPUCalc

思夜月心 发表于 2010-1-27 10:37:21

回复 7# gapkiller
超赞的!不过好像没有内核的显示!

思夜月心 发表于 2010-1-27 10:39:39

回复 19# kemyliu


好想97行有点错误啊!我这边运行不了!

koalacoca 发表于 2010-1-31 18:56:23

我也是这个问题谢谢分享

lxz 发表于 2010-2-1 18:25:48

回复kemyliu


好想97行有点错误啊!我这边运行不了!
思夜月心 发表于 2010-1-27 10:39 http://www.autoitx.com/images/common/back.gif

重新定义数组就行。

wuweixian 发表于 2010-3-15 19:38:39

回复 7# gapkiller


    感谢楼上

曼菲士 发表于 2010-7-24 20:08:11

谢谢分享,不依赖WMI的真少,值得学习。

风行月影 发表于 2011-1-5 20:11:55

学习一下,最近正在研究这个

harrye9016 发表于 2011-2-24 00:49:40

我也想知道

ayumi502 发表于 2011-3-7 13:44:23

学习了~~谢谢分享~~

davoer 发表于 2011-4-26 15:21:08

回复 7# gapkiller


    正好用的到,谢谢~~

qddoit 发表于 2011-10-21 15:59:02

{:face (88):}```````
页: 1 [2] 3
查看完整版本: 如何获得CPU使用率和内存占用率?