回复 8# lusheng0028
#include <array.au3>
#include <file.au3>
#include <GDIPlus.au3>
Opt('MustDeclareVars', 1)
Opt("TrayIconHide", 1)
HotKeySet("{ESC}", "_Exit");ESC退出
Dim $log = "cpu.log"
Global Const $Process_All_Access = 0x1F0FFF
Global $CreateTime = DllStructCreate("dword;dword")
Global $ExitTime = DllStructCreate("dword;dword")
Global $KernelTime = DllStructCreate("dword;dword")
Global $UserTime = DllStructCreate("dword;dword")
Global $sUserTime, $sKernelTime, $eUserTime, $eKernelTime
Global $ret
Global $ProcHandle, $Process_CPU_Usage
Global $PID, $CPUTime
Global $logical_cpus = CPU()
Global $info
Global $hwnd, $pic, $graphics, $bitmap, $backbuffer, $ffamily, $arial, $sformat, $blackbrush, $rectf, $arr,$time
_FileWriteLog(@ScriptDir & $log, "$logical_cpus" & $logical_cpus)
Global $list = ProcessList("QQ.exe")
If $list[0][0] = 1 Then $PID = $list[1][1]
$ProcHandle = DllCall("kernel32.dll", "hwnd", "OpenProcess", "dword", $Process_All_Access, "int", False, "dword", $PID)
$ProcHandle = $ProcHandle[0]
$hwnd = GUICreate("GDI+ Example", 200, 40, @DesktopWidth - 200 - 10, @DesktopHeight - 40 - 45, 0x80000000,-1,guicreate(""))
$pic = GUICtrlCreatePic("", 0, 0, 200, 40)
GUISetState()
_GDIPlus_Startup()
$graphics = _GDIPlus_GraphicsCreateFromHWND(GUICtrlGetHandle($pic))
$bitmap = _GDIPlus_BitmapCreateFromGraphics(200, 40, $graphics)
$backbuffer = _GDIPlus_ImageGetGraphicsContext($bitmap)
$ffamily = _GDIPlus_FontFamilyCreate("微软雅黑")
$arial = _GDIPlus_FontCreate($ffamily, 15)
$sformat = _GDIPlus_StringFormatCreate()
$blackbrush = _GDIPlus_BrushCreateSolid(0xff0000FF)
$rectf = _GDIPlus_RectFCreate(0)
$arr = _GDIPlus_GraphicsMeasureString($graphics, "CPU usage: 100 % ", $arial, $rectf, $sformat)
$time=TimerInit()
AdlibRegister("_Test", 250);这里设置回显时间
While True
If Not ProcessExists($PID) Then _Exit()
Sleep(100)
WEnd
Func _Test()
$CPUTime = _GetProcTime($ProcHandle)
$info = " CPU usage: " & $Process_CPU_Usage & " % "
_GDIPlus_GraphicsClear($backbuffer, 0xFFabcdef)
_GDIPlus_GraphicsDrawStringEx($backbuffer, $info, $arial, $arr[0], $sformat, $blackbrush)
_GDIPlus_GraphicsDrawImageRect($graphics, $bitmap, 0, 0, 200, 40)
if TimerDiff($time)>6*1000 Then
MsgBox(0,0,$info)
$time=TimerInit()
EndIf
EndFunc ;==>_Test
Func _GetProcTime($ProcessHandle)
$ret = DllCall("kernel32.dll", "int", "GetProcessTimes", "int", $ProcessHandle, "ptr", DllStructGetPtr($CreateTime), "ptr", DllStructGetPtr($ExitTime), "ptr", DllStructGetPtr($KernelTime), "ptr", DllStructGetPtr($UserTime))
If $ret[0] = 0 Then
ConsoleWrite("(" & @ScriptLineNumber & ") : = Error in GetProcessTimes call" & @CRLF)
SetError(1, 0, $ret[0])
EndIf
$sKernelTime = DllStructGetData($KernelTime, 1)
$sUserTime = DllStructGetData($UserTime, 1)
$Process_CPU_Usage = Floor(($sKernelTime - $eKernelTime + $sUserTime - $eUserTime) / 100000 / $logical_cpus)
If $Process_CPU_Usage > 100 Then $Process_CPU_Usage = "100"
$eKernelTime = $sKernelTime
$eUserTime = $sUserTime
Return $sUserTime + $sKernelTime
EndFunc ;==>_GetProcTime
Func CPU()
Local $sLPSystemInfo = DllStructCreate("ushort dwOemId;" & "short wProcessorArchitecture;" & "dword dwPageSize;" & _
"ptr lpMinimumApplicationAddress;" & _
"ptr lpMaximumApplicationAddress;" & _
"long_ptr dwActiveProcessorMask;" & _
"dword dwNumberOfProcessors;" & _
"dword dwProcessorType;" & _
"dword dwAllocationGranularity;" & _
"short wProcessorLevel;" & _
"short wProcessorRevision")
Local $aResult = DllCall("Kernel32.dll", "none", "GetSystemInfo", "ptr", DllStructGetPtr($sLPSystemInfo))
If @error Or Not IsArray($aResult) Then Return SetError(1, 0, 0)
Return SetError(0, 0, DllStructGetData($sLPSystemInfo, "dwNumberOfProcessors"))
EndFunc ;==>CPU
Func _Exit()
_GDIPlus_BrushDispose($blackbrush)
_GDIPlus_StringFormatDispose($sformat)
_GDIPlus_FontDispose($arial)
_GDIPlus_FontFamilyDispose($ffamily)
_GDIPlus_GraphicsDispose($backbuffer)
_GDIPlus_BitmapDispose($bitmap)
_GDIPlus_GraphicsDispose($graphics)
_GDIPlus_Shutdown()
Exit
EndFunc ;==>_Exit
|