Jump to content

Collaboration request: GDI+ - CPU Usage tool


Recommended Posts

I'm building a tool to remotely monitor CPU usage on my server.

I have a working tool but I have a few issues I'd like some help with.

1. The app uses more system memory on a continual growth rate...  It eventually starts displaying strange artifacts and the background flashes between black and and white behind the GDI+ elements.  I determined this couldn't be left to run for any amount of time (greater than 15-20 minutes) - Very frustrating.

I suspect it's because it keeps drawing new GDI+ elements to replace the previous cycle ... the $bar1 = "" is enough to remove the image and allow the new image to be drawn there, but I don't have a handle for the original GDI+ element to throw it away... Not sure where they go...

I also suspect there could be a better way to do what I'm doing here, but from a problem solving perspective - this is what I came up with - I will accept suggestions for how better to accomplish the same / better or acceptable results :) 

I will not however accept corrections on grammar, punctuation or commenting - I didn't comment this as I was going, I use the variables that make sense to me (or as they were when I lifted them from the scraps I found on the internet) and you can never be too careful with punctuation.

2. I have a WMI query that is used to remotely pull the CPU data from the server - if I supply the wrong credentials the app crashes... I tried to make it show an error and go back to allow me to try again ... doesn't work. Any help with catching that error and preventing the crash would be super helpful. (works great if the credentials are correct; domain or local)

 

The whole thing below... 

#include <Date.au3>
#include <WindowsConstants.au3>
#include <GuiConstantsEX.au3>
#include <EditConstants.au3>
#include <ButtonConstants.au3>
#include <GDIPlus.au3>

Global $__g_hGDIPDll
Global $graph[21]
Global $timer, $timeout = 500

Global $hFlag = 0
$timer = TimerInit()


$main = GUICreate("CPU Graph", 125, 220, Default, Default, Default, BitOR($WS_EX_TOOLWINDOW, $WS_EX_TOPMOST))
    $bar1 = GUICtrlCreatePic("", 10, 10, 5, 100)
    $bar2 = GUICtrlCreatePic("", 15, 10, 5, 100)
    $bar3 = GUICtrlCreatePic("", 20, 10, 5, 100)
    $bar4 = GUICtrlCreatePic("", 25, 10, 5, 100)
    $bar5 = GUICtrlCreatePic("", 30, 10, 5, 100)
    $bar6 = GUICtrlCreatePic("", 35, 10, 5, 100)
    $bar7 = GUICtrlCreatePic("", 40, 10, 5, 100)
    $bar8 = GUICtrlCreatePic("", 45, 10, 5, 100)
    $bar9 = GUICtrlCreatePic("", 50, 10, 5, 100)
    $bar10 = GUICtrlCreatePic("", 55, 10, 5, 100)
    $bar11 = GUICtrlCreatePic("", 60, 10, 5, 100)
    $bar12 = GUICtrlCreatePic("", 65, 10, 5, 100)
    $bar13 = GUICtrlCreatePic("", 70, 10, 5, 100)
    $bar14 = GUICtrlCreatePic("", 75, 10, 5, 100)
    $bar15 = GUICtrlCreatePic("", 80, 10, 5, 100)
    $bar16 = GUICtrlCreatePic("", 85, 10, 5, 100)
    $bar17 = GUICtrlCreatePic("", 90, 10, 5, 100)
    $bar18 = GUICtrlCreatePic("", 95, 10, 5, 100)
    $bar19 = GUICtrlCreatePic("", 100, 10, 5, 100)
    $bar20 = GUICtrlCreatePic("", 105, 10, 5, 100)
    $host = GUICtrlCreateInput("Host", 10, 115, 100, 20, $ES_AUTOHSCROLL)
    $user = GUICtrlCreateInput("User", 10, 140, 100, 20, $ES_AUTOHSCROLL)
        GUICtrlSetFont(-1, 8, 400)
    $pass = GUICtrlCreateInput("Pass", 10, 165, 100, 20, BitOR($ES_PASSWORD,$ES_AUTOHSCROLL))
        GUICtrlSetFont(-1, 8, 400)
    $start = GUICtrlCreateButton("Start", 10, 190, 100, 20, $BS_DEFPUSHBUTTON)
GUISetState()

While 1
    $msg = GUIGetMsg()
    If $msg = $GUI_EVENT_CLOSE Then ExitLoop
    If $msg = $start Then ConnectWMI()
    If $hFlag = 1 Then
        If TimerDiff($timer) > $timeout Then UpdateGraph()
    EndIf
WEnd


Func ConnectWMI()
    Global $hostname = GUICtrlRead($host)
        If Ping($hostname, 2000) = 0 Then
            Msgbox(0, "Error", "Unable to reach specified host")
            Return 0
        EndIf
    Local $usr = GUICtrlRead($user)
    Local $pwd = GUICtrlRead($pass)

    Global $objSWbemLocator = ObjCreate("WbemScripting.SWbemLocator")
    Global $objWMIService = $objSWbemLocator.ConnectServer($hostname, "root\cimv2", $usr, $pwd)
    If @error  Then
            Msgbox(0, "Error", "Unable to connect to the Host with the supplied credentials")
            Return 0
    EndIf
    $hFlag = 1
    UpdateGraph()
EndFunc


Func UpdateGraph()
    $usage = _Processor_Usage()
    For $i = 1 to 19
        $graph[$i] = $graph[$i+1]
    Next
    $graph[20] = $usage

    GUICtrlSetImage($bar1, "")
    CreateBar($bar1, $graph[1])
    GUICtrlSetImage($bar2, "")
    CreateBar($bar2, $graph[2])
    GUICtrlSetImage($bar3, "")
    CreateBar($bar3, $graph[3])
    GUICtrlSetImage($bar4, "")
    CreateBar($bar4, $graph[4])
    GUICtrlSetImage($bar5, "")
    CreateBar($bar5, $graph[5])
    GUICtrlSetImage($bar6, "")
    CreateBar($bar6, $graph[6])
    GUICtrlSetImage($bar7, "")
    CreateBar($bar7, $graph[7])
    GUICtrlSetImage($bar8, "")
    CreateBar($bar8, $graph[8])
    GUICtrlSetImage($bar9, "")
    CreateBar($bar9, $graph[9])
    GUICtrlSetImage($bar10, "")
    CreateBar($bar10, $graph[10])
    GUICtrlSetImage($bar11, "")
    CreateBar($bar11, $graph[11])
    GUICtrlSetImage($bar12, "")
    CreateBar($bar12, $graph[12])
    GUICtrlSetImage($bar13, "")
    CreateBar($bar13, $graph[13])
    GUICtrlSetImage($bar14, "")
    CreateBar($bar14, $graph[14])
    GUICtrlSetImage($bar15, "")
    CreateBar($bar15, $graph[15])
    GUICtrlSetImage($bar16, "")
    CreateBar($bar16, $graph[16])
    GUICtrlSetImage($bar17, "")
    CreateBar($bar17, $graph[17])
    GUICtrlSetImage($bar18, "")
    CreateBar($bar18, $graph[18])
    GUICtrlSetImage($bar19, "")
    CreateBar($bar19, $graph[19])
    GUICtrlSetImage($bar20, "")
    CreateBar($bar20, $graph[20])
    $timer = TimerInit()

EndFunc

Func _Processor_Usage()
    Dim $Col_Items = $objWMIService.ExecQuery('SELECT * FROM Win32_PerfFormattedData_PerfOS_Processor', 'WQL')
    Local $Obj_Item
    For $Obj_Item In $Col_Items
        Return $Obj_Item.PercentProcessorTime
    Next
EndFunc

Func CreateBar($target, $value)
    Local $width=5, $height=2
    _GDIPlus_Startup()
    $hImage = DLL_BitmapCreate($width, $height*50)
    $hGraphic = _GDIPlus_ImageGetGraphicsContext($hImage)
    _GDIPlus_GraphicsSetSmoothingMode($hGraphic, 2)

    ;100
    If $value > 98 Then
        $hBrush = _GDIPlus_BrushCreateSolid("0xFFF00F2F")
        _GDIPlus_GraphicsFillRect($hGraphic, 0, 0, $width, $height, $hBrush)
    EndIf
    ;98
    If $value > 96 Then
    $hBrush = _GDIPlus_BrushCreateSolid("0xFFF0182C")
    _GDIPlus_GraphicsFillRect($hGraphic, 0, 2, $width, $height, $hBrush)
    EndIf
    ;96
    If $value > 94 Then
    $hBrush = _GDIPlus_BrushCreateSolid("0xFFF0212A")
    _GDIPlus_GraphicsFillRect($hGraphic, 0, 4, $width, $height, $hBrush)
    EndIf
    ;94
    If $value > 92 Then
    $hBrush = _GDIPlus_BrushCreateSolid("0xFFF02B28")
    _GDIPlus_GraphicsFillRect($hGraphic, 0, 6, $width, $height, $hBrush)
    EndIf
    ;92
    If $value > 90 Then
    $hBrush = _GDIPlus_BrushCreateSolid("0xFFF03426")
    _GDIPlus_GraphicsFillRect($hGraphic, 0, 8, $width, $height, $hBrush)
    EndIf
    ;90
    If $value > 88 Then
    $hBrush = _GDIPlus_BrushCreateSolid("0xFFF03E24")
    _GDIPlus_GraphicsFillRect($hGraphic, 0, 10, $width, $height, $hBrush)
    EndIf
    ;88
    If $value > 86 Then
    $hBrush = _GDIPlus_BrushCreateSolid("0xFFF04722")
    _GDIPlus_GraphicsFillRect($hGraphic, 0, 12, $width, $height, $hBrush)
    EndIf
    ;86
    If $value > 84 Then
    $hBrush = _GDIPlus_BrushCreateSolid("0xFFF05120")
    _GDIPlus_GraphicsFillRect($hGraphic, 0, 14, $width, $height, $hBrush)
    EndIf
    ;84
    If $value > 82 Then
    $hBrush = _GDIPlus_BrushCreateSolid("0xFFF05A1D")
    _GDIPlus_GraphicsFillRect($hGraphic, 0, 16, $width, $height, $hBrush)
    EndIf
    ;82
    If $value > 80 Then
    $hBrush = _GDIPlus_BrushCreateSolid("0xFFF0641B")
    _GDIPlus_GraphicsFillRect($hGraphic, 0, 18, $width, $height, $hBrush)
    EndIf
    ;80
    If $value > 78 Then
    $hBrush = _GDIPlus_BrushCreateSolid("0xFFF06D19")
    _GDIPlus_GraphicsFillRect($hGraphic, 0, 20, $width, $height, $hBrush)
    EndIf
    ;78
    If $value > 76 Then
    $hBrush = _GDIPlus_BrushCreateSolid("0xFFF07717")
    _GDIPlus_GraphicsFillRect($hGraphic, 0, 22, $width, $height, $hBrush)
    EndIf
    ;76
    If $value > 74 Then
    $hBrush = _GDIPlus_BrushCreateSolid("0xFFF08015")
    _GDIPlus_GraphicsFillRect($hGraphic, 0, 24, $width, $height, $hBrush)
    EndIf
    ;74
    If $value > 72 Then
    $hBrush = _GDIPlus_BrushCreateSolid("0xFFF08A13")
    _GDIPlus_GraphicsFillRect($hGraphic, 0, 26, $width, $height, $hBrush)
    EndIf
    ;72
    If $value > 70 Then
    $hBrush = _GDIPlus_BrushCreateSolid("0xFFF09311")
    _GDIPlus_GraphicsFillRect($hGraphic, 0, 28, $width, $height, $hBrush)
    EndIf
    ;70
    If $value > 68 Then
    $hBrush = _GDIPlus_BrushCreateSolid("0xFFF09311")
    _GDIPlus_GraphicsFillRect($hGraphic, 0, 30, $width, $height, $hBrush)
    EndIf
    ;68
    If $value > 66 Then
    $hBrush = _GDIPlus_BrushCreateSolid("0xFFF09D0F")
    _GDIPlus_GraphicsFillRect($hGraphic, 0, 32, $width, $height, $hBrush)
    EndIf
    ;66
    If $value > 64 Then
    $hBrush = _GDIPlus_BrushCreateSolid("0xFFECA20F")
    _GDIPlus_GraphicsFillRect($hGraphic, 0, 34, $width, $height, $hBrush)
    EndIf
    ;64
    If $value > 62 Then
    $hBrush = _GDIPlus_BrushCreateSolid("0xFFE8A80F")
    _GDIPlus_GraphicsFillRect($hGraphic, 0, 36, $width, $height, $hBrush)
    EndIf
    ;62
    If $value > 60 Then
    $hBrush = _GDIPlus_BrushCreateSolid("0xFFE5AD0F")
    _GDIPlus_GraphicsFillRect($hGraphic, 0, 38, $width, $height, $hBrush)
    EndIf
    ;60
    If $value > 58 Then
    $hBrush = _GDIPlus_BrushCreateSolid("0xFFE1B30F")
    _GDIPlus_GraphicsFillRect($hGraphic, 0, 40, $width, $height, $hBrush)
    EndIf
    ;58
    If $value > 56 Then
    $hBrush = _GDIPlus_BrushCreateSolid("0xFFDEB80F")
    _GDIPlus_GraphicsFillRect($hGraphic, 0, 42, $width, $height, $hBrush)
    EndIf
    ;56
    If $value > 54 Then
    $hBrush = _GDIPlus_BrushCreateSolid("0xFFDABE0F")
    _GDIPlus_GraphicsFillRect($hGraphic, 0, 44, $width, $height, $hBrush)
    EndIf
    ;54
    If $value > 52 Then
    $hBrush = _GDIPlus_BrushCreateSolid("0xFFD6C30F")
    _GDIPlus_GraphicsFillRect($hGraphic, 0, 46, $width, $height, $hBrush)
    EndIf
    ;52
    If $value > 50 Then
    $hBrush = _GDIPlus_BrushCreateSolid("0xFFCFCE0F")
    _GDIPlus_GraphicsFillRect($hGraphic, 0, 48, $width, $height, $hBrush)
    EndIf
    ;50
    If $value > 48 Then
    $hBrush = _GDIPlus_BrushCreateSolid("0xFFCCD40F")
    _GDIPlus_GraphicsFillRect($hGraphic, 0, 50, $width, $height, $hBrush)
    EndIf
    ;48
    If $value > 46 Then
    $hBrush = _GDIPlus_BrushCreateSolid("0xFFC8D90F")
    _GDIPlus_GraphicsFillRect($hGraphic, 0, 52, $width, $height, $hBrush)
    EndIf
    ;46
    If $value > 44 Then
    $hBrush = _GDIPlus_BrushCreateSolid("0xFFC4DF0F")
    _GDIPlus_GraphicsFillRect($hGraphic, 0, 54, $width, $height, $hBrush)
    EndIf
    ;44
    If $value > 42 Then
    $hBrush = _GDIPlus_BrushCreateSolid("0xFFC1E40F")
    _GDIPlus_GraphicsFillRect($hGraphic, 0, 56, $width, $height, $hBrush)
    EndIf
    ;42
    If $value > 40 Then
    $hBrush = _GDIPlus_BrushCreateSolid("0xFFBDEA0F")
    _GDIPlus_GraphicsFillRect($hGraphic, 0, 58, $width, $height, $hBrush)
    EndIf
    ;40
    If $value > 38 Then
    $hBrush = _GDIPlus_BrushCreateSolid("0xFFBAF00F")
    _GDIPlus_GraphicsFillRect($hGraphic, 0, 60, $width, $height, $hBrush)
    EndIf
    ;38
    If $value > 36 Then
    $hBrush = _GDIPlus_BrushCreateSolid("0xFFBAF00F")
    _GDIPlus_GraphicsFillRect($hGraphic, 0, 62, $width, $height, $hBrush)
    EndIf
    ;36
    If $value > 34 Then
    $hBrush = _GDIPlus_BrushCreateSolid("0xFFADEF0F")
    _GDIPlus_GraphicsFillRect($hGraphic, 0, 64, $width, $height, $hBrush)
    EndIf
    ;34
    If $value > 32 Then
    $hBrush = _GDIPlus_BrushCreateSolid("0xFFA1EE0F")
    _GDIPlus_GraphicsFillRect($hGraphic, 0, 66, $width, $height, $hBrush)
    EndIf
    ;32
    If $value > 30 Then
    $hBrush = _GDIPlus_BrushCreateSolid("0xFF94ED0F")
    _GDIPlus_GraphicsFillRect($hGraphic, 0, 68, $width, $height, $hBrush)
    EndIf
    ;30
    If $value > 28 Then
    $hBrush = _GDIPlus_BrushCreateSolid("0xFF88ED0F")
    _GDIPlus_GraphicsFillRect($hGraphic, 0, 70, $width, $height, $hBrush)
    EndIf
    ;28
    If $value > 26 Then
    $hBrush = _GDIPlus_BrushCreateSolid("0xFF7CEC0F")
    _GDIPlus_GraphicsFillRect($hGraphic, 0, 72, $width, $height, $hBrush)
    EndIf
    ;26
    If $value > 24 Then
    $hBrush = _GDIPlus_BrushCreateSolid("0xFF6FEB0F")
    _GDIPlus_GraphicsFillRect($hGraphic, 0, 74, $width, $height, $hBrush)
    EndIf
    ;24
    If $value > 22 Then
    $hBrush = _GDIPlus_BrushCreateSolid("0xFF63EA0F")
    _GDIPlus_GraphicsFillRect($hGraphic, 0, 76, $width, $height, $hBrush)
    EndIf
    ;22
    If $value > 20 Then
    $hBrush = _GDIPlus_BrushCreateSolid("0xFF56EA0F")
    _GDIPlus_GraphicsFillRect($hGraphic, 0, 78, $width, $height, $hBrush)
    EndIf
    ;20
    If $value > 18 Then
    $hBrush = _GDIPlus_BrushCreateSolid("0xFF56EA0F")
    _GDIPlus_GraphicsFillRect($hGraphic, 0, 80, $width, $height, $hBrush)
    EndIf
    ;18
    If $value > 16 Then
    $hBrush = _GDIPlus_BrushCreateSolid("0xFF4AE90F")
    _GDIPlus_GraphicsFillRect($hGraphic, 0, 82, $width, $height, $hBrush)
    EndIf
    ;16
    If $value > 14 Then
    $hBrush = _GDIPlus_BrushCreateSolid("0xFF3EE80F")
    _GDIPlus_GraphicsFillRect($hGraphic, 0, 84, $width, $height, $hBrush)
    EndIf
    ;14
    If $value > 12 Then
    $hBrush = _GDIPlus_BrushCreateSolid("0xFF31E70F")
    _GDIPlus_GraphicsFillRect($hGraphic, 0, 86, $width, $height, $hBrush)
    EndIf
    ;12
    If $value > 10 Then
    $hBrush = _GDIPlus_BrushCreateSolid("0xFF25E70F")
    _GDIPlus_GraphicsFillRect($hGraphic, 0, 88, $width, $height, $hBrush)
    EndIf
    ;10
    If $value > 8 Then
    $hBrush = _GDIPlus_BrushCreateSolid("0xFF18E60F")
    _GDIPlus_GraphicsFillRect($hGraphic, 0, 90, $width, $height, $hBrush)
    EndIf
    ;8
    If $value > 6 Then
    $hBrush = _GDIPlus_BrushCreateSolid("0xFF0CE50F")
    _GDIPlus_GraphicsFillRect($hGraphic, 0, 92, $width, $height, $hBrush)
    EndIf
    ;6
    If $value > 4 Then
    $hBrush = _GDIPlus_BrushCreateSolid("0xFF00E510")
    _GDIPlus_GraphicsFillRect($hGraphic, 0, 94, $width, $height, $hBrush)
    EndIf
    ;4
    If $value > 2 Then
    $hBrush = _GDIPlus_BrushCreateSolid("0xFF00E509")
    _GDIPlus_GraphicsFillRect($hGraphic, 0, 96, $width, $height, $hBrush)
    EndIf
    ;2
    $hBrush = _GDIPlus_BrushCreateSolid("0xFF00E509")
    _GDIPlus_GraphicsFillRect($hGraphic, 0, 98, $width, $height, $hBrush)

    $hBitmap = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hImage)
    GUICtrlSendMsg($target, 0x172, 0, $hBitmap)
    _WinAPI_DeleteObject($hBitmap)
    _GDIPlus_BrushDispose($hBrush)
    _GDIPlus_GraphicsDispose($hGraphic)
    _GDIPlus_ImageDispose($hImage)
    _GDIPlus_Shutdown()
EndFunc   ;==>_CreateBar

Func DLL_BitmapCreate($width, $height)
    Local $aResult = DllCall($__g_hGDIPDll, "uint", "GdipCreateBitmapFromScan0", "int", $width, "int", $height, "int", 0, "int", 0x0026200A, "ptr", 0, "int*", 0)
    Return $aResult[6]
EndFunc   ;==>DLL_BitmapCreate

 

 

Link to comment
Share on other sites

Made some tweaks and added some comments - changes made cleaner code but didn't resolve either problem    :mad:

 

#include <Date.au3>
#include <WindowsConstants.au3>
#include <GuiConstantsEX.au3>
#include <EditConstants.au3>
#include <ButtonConstants.au3>
#include <GDIPlus.au3>

Global $__g_hGDIPDll
Global $graph[21], $bars[21]
Global $timer, $timeout = 500

Global $hFlag = 0
_GDIPlus_Startup()
Global $hBrush, $hGraphic, $hImage

;==================================================
$main = GUICreate("CPU Graph", 125, 220, Default, Default, Default, BitOR($WS_EX_TOOLWINDOW, $WS_EX_TOPMOST))
    $bars[1] = GUICtrlCreatePic("", 10, 10, 5, 100)
    $bars[2] = GUICtrlCreatePic("", 15, 10, 5, 100)
    $bars[3] = GUICtrlCreatePic("", 20, 10, 5, 100)
    $bars[4] = GUICtrlCreatePic("", 25, 10, 5, 100)
    $bars[5] = GUICtrlCreatePic("", 30, 10, 5, 100)
    $bars[6] = GUICtrlCreatePic("", 35, 10, 5, 100)
    $bars[7] = GUICtrlCreatePic("", 40, 10, 5, 100)
    $bars[8] = GUICtrlCreatePic("", 45, 10, 5, 100)
    $bars[9] = GUICtrlCreatePic("", 50, 10, 5, 100)
    $bars[10] = GUICtrlCreatePic("", 55, 10, 5, 100)
    $bars[11] = GUICtrlCreatePic("", 60, 10, 5, 100)
    $bars[12] = GUICtrlCreatePic("", 65, 10, 5, 100)
    $bars[13] = GUICtrlCreatePic("", 70, 10, 5, 100)
    $bars[14] = GUICtrlCreatePic("", 75, 10, 5, 100)
    $bars[15] = GUICtrlCreatePic("", 80, 10, 5, 100)
    $bars[16] = GUICtrlCreatePic("", 85, 10, 5, 100)
    $bars[17] = GUICtrlCreatePic("", 90, 10, 5, 100)
    $bars[18] = GUICtrlCreatePic("", 95, 10, 5, 100)
    $bars[19] = GUICtrlCreatePic("", 100, 10, 5, 100)
    $bars[20] = GUICtrlCreatePic("", 105, 10, 5, 100)
    $host = GUICtrlCreateInput("Host", 10, 115, 100, 20, $ES_AUTOHSCROLL)
    $user = GUICtrlCreateInput("User", 10, 140, 100, 20, $ES_AUTOHSCROLL)
        GUICtrlSetFont(-1, 8, 400)
    $pass = GUICtrlCreateInput("Pass", 10, 165, 100, 20, BitOR($ES_PASSWORD,$ES_AUTOHSCROLL))
        GUICtrlSetFont(-1, 8, 400)
    $start = GUICtrlCreateButton("Start", 10, 190, 100, 20, $BS_DEFPUSHBUTTON)
GUISetState() ;<== Draw Main GUI

;== Main Processing Loop===================================
While 1
    $msg = GUIGetMsg()
    If $msg = $GUI_EVENT_CLOSE Then ExitLoop
    If $msg = $start Then ConnectWMI() ;<== connect host
    If $hFlag = 1 Then ;<== If host is connected
        If TimerDiff($timer) > $timeout Then UpdateGraph()
    EndIf
WEnd

;== Pre Exit Cleanup ======================================
    _GDIPlus_BrushDispose($hBrush)
    _GDIPlus_GraphicsDispose($hGraphic)
    _GDIPlus_ImageDispose($hImage)
    _GDIPlus_Shutdown()
;==========================================================

Func ConnectWMI() ; Connect WMI ==> (Establishes a connection to the WMI data on the remote system)
    Global $hostname = GUICtrlRead($host)
        If Ping($hostname, 2000) = 0 Then
            Msgbox(0, "Error", "Unable to reach specified host")
            Return 0
        EndIf
    Local $usr = GUICtrlRead($user)
    Local $pwd = GUICtrlRead($pass)

    Global $objSWbemLocator = ObjCreate("WbemScripting.SWbemLocator")
    Global $objWMIService = $objSWbemLocator.ConnectServer($hostname, "root\cimv2", $usr, $pwd)
    If @error  Then
            Msgbox(0, "Error", "Unable to connect to the Host with the supplied credentials")
            Return 0
    EndIf
    $hFlag = 1
    UpdateGraph()
EndFunc ;<== Connect WMI

Func UpdateGraph(); Update Graph (sets data points, calls create bar to draw the graph)
    $usage = _Processor_Usage()
    For $i = 1 to 19
        $graph[$i] = $graph[$i+1]
    Next
    $graph[20] = $usage

    For $i = 1 to 20
        CreateBar($bars[$i], $graph[$i])
    Next
    $timer = TimerInit()
EndFunc ;<== Update Graph

Func _Processor_Usage(); Processor Usage ==>  WMI Script to pull CPU usage each cycle
    Dim $Col_Items = $objWMIService.ExecQuery('SELECT * FROM Win32_PerfFormattedData_PerfOS_Processor', 'WQL')
    Local $Obj_Item
    For $Obj_Item In $Col_Items
        Return $Obj_Item.PercentProcessorTime
    Next
EndFunc ;<== Processor Usage


;==== GDI+ Functions =================================================

Func CreateBar($target, $value)
    Local $width=5, $height=2
    GUICtrlSetImage($target, "")

    $hImage = DLL_BitmapCreate($width, $height*50)
    $hGraphic = _GDIPlus_ImageGetGraphicsContext($hImage)
    _GDIPlus_GraphicsSetSmoothingMode($hGraphic, 2)

    ;100
    If $value > 98 Then
        $hBrush = _GDIPlus_BrushCreateSolid("0xFFF00F2F")
        _GDIPlus_GraphicsFillRect($hGraphic, 0, 0, $width, $height, $hBrush)
    EndIf
    ;98
    If $value > 96 Then
    $hBrush = _GDIPlus_BrushCreateSolid("0xFFF0182C")
    _GDIPlus_GraphicsFillRect($hGraphic, 0, 2, $width, $height, $hBrush)
    EndIf
    ;96
    If $value > 94 Then
    $hBrush = _GDIPlus_BrushCreateSolid("0xFFF0212A")
    _GDIPlus_GraphicsFillRect($hGraphic, 0, 4, $width, $height, $hBrush)
    EndIf
    ;94
    If $value > 92 Then
    $hBrush = _GDIPlus_BrushCreateSolid("0xFFF02B28")
    _GDIPlus_GraphicsFillRect($hGraphic, 0, 6, $width, $height, $hBrush)
    EndIf
    ;92
    If $value > 90 Then
    $hBrush = _GDIPlus_BrushCreateSolid("0xFFF03426")
    _GDIPlus_GraphicsFillRect($hGraphic, 0, 8, $width, $height, $hBrush)
    EndIf
    ;90
    If $value > 88 Then
    $hBrush = _GDIPlus_BrushCreateSolid("0xFFF03E24")
    _GDIPlus_GraphicsFillRect($hGraphic, 0, 10, $width, $height, $hBrush)
    EndIf
    ;88
    If $value > 86 Then
    $hBrush = _GDIPlus_BrushCreateSolid("0xFFF04722")
    _GDIPlus_GraphicsFillRect($hGraphic, 0, 12, $width, $height, $hBrush)
    EndIf
    ;86
    If $value > 84 Then
    $hBrush = _GDIPlus_BrushCreateSolid("0xFFF05120")
    _GDIPlus_GraphicsFillRect($hGraphic, 0, 14, $width, $height, $hBrush)
    EndIf
    ;84
    If $value > 82 Then
    $hBrush = _GDIPlus_BrushCreateSolid("0xFFF05A1D")
    _GDIPlus_GraphicsFillRect($hGraphic, 0, 16, $width, $height, $hBrush)
    EndIf
    ;82
    If $value > 80 Then
    $hBrush = _GDIPlus_BrushCreateSolid("0xFFF0641B")
    _GDIPlus_GraphicsFillRect($hGraphic, 0, 18, $width, $height, $hBrush)
    EndIf
    ;80
    If $value > 78 Then
    $hBrush = _GDIPlus_BrushCreateSolid("0xFFF06D19")
    _GDIPlus_GraphicsFillRect($hGraphic, 0, 20, $width, $height, $hBrush)
    EndIf
    ;78
    If $value > 76 Then
    $hBrush = _GDIPlus_BrushCreateSolid("0xFFF07717")
    _GDIPlus_GraphicsFillRect($hGraphic, 0, 22, $width, $height, $hBrush)
    EndIf
    ;76
    If $value > 74 Then
    $hBrush = _GDIPlus_BrushCreateSolid("0xFFF08015")
    _GDIPlus_GraphicsFillRect($hGraphic, 0, 24, $width, $height, $hBrush)
    EndIf
    ;74
    If $value > 72 Then
    $hBrush = _GDIPlus_BrushCreateSolid("0xFFF08A13")
    _GDIPlus_GraphicsFillRect($hGraphic, 0, 26, $width, $height, $hBrush)
    EndIf
    ;72
    If $value > 70 Then
    $hBrush = _GDIPlus_BrushCreateSolid("0xFFF09311")
    _GDIPlus_GraphicsFillRect($hGraphic, 0, 28, $width, $height, $hBrush)
    EndIf
    ;70
    If $value > 68 Then
    $hBrush = _GDIPlus_BrushCreateSolid("0xFFF09311")
    _GDIPlus_GraphicsFillRect($hGraphic, 0, 30, $width, $height, $hBrush)
    EndIf
    ;68
    If $value > 66 Then
    $hBrush = _GDIPlus_BrushCreateSolid("0xFFF09D0F")
    _GDIPlus_GraphicsFillRect($hGraphic, 0, 32, $width, $height, $hBrush)
    EndIf
    ;66
    If $value > 64 Then
    $hBrush = _GDIPlus_BrushCreateSolid("0xFFECA20F")
    _GDIPlus_GraphicsFillRect($hGraphic, 0, 34, $width, $height, $hBrush)
    EndIf
    ;64
    If $value > 62 Then
    $hBrush = _GDIPlus_BrushCreateSolid("0xFFE8A80F")
    _GDIPlus_GraphicsFillRect($hGraphic, 0, 36, $width, $height, $hBrush)
    EndIf
    ;62
    If $value > 60 Then
    $hBrush = _GDIPlus_BrushCreateSolid("0xFFE5AD0F")
    _GDIPlus_GraphicsFillRect($hGraphic, 0, 38, $width, $height, $hBrush)
    EndIf
    ;60
    If $value > 58 Then
    $hBrush = _GDIPlus_BrushCreateSolid("0xFFE1B30F")
    _GDIPlus_GraphicsFillRect($hGraphic, 0, 40, $width, $height, $hBrush)
    EndIf
    ;58
    If $value > 56 Then
    $hBrush = _GDIPlus_BrushCreateSolid("0xFFDEB80F")
    _GDIPlus_GraphicsFillRect($hGraphic, 0, 42, $width, $height, $hBrush)
    EndIf
    ;56
    If $value > 54 Then
    $hBrush = _GDIPlus_BrushCreateSolid("0xFFDABE0F")
    _GDIPlus_GraphicsFillRect($hGraphic, 0, 44, $width, $height, $hBrush)
    EndIf
    ;54
    If $value > 52 Then
    $hBrush = _GDIPlus_BrushCreateSolid("0xFFD6C30F")
    _GDIPlus_GraphicsFillRect($hGraphic, 0, 46, $width, $height, $hBrush)
    EndIf
    ;52
    If $value > 50 Then
    $hBrush = _GDIPlus_BrushCreateSolid("0xFFCFCE0F")
    _GDIPlus_GraphicsFillRect($hGraphic, 0, 48, $width, $height, $hBrush)
    EndIf
    ;50
    If $value > 48 Then
    $hBrush = _GDIPlus_BrushCreateSolid("0xFFCCD40F")
    _GDIPlus_GraphicsFillRect($hGraphic, 0, 50, $width, $height, $hBrush)
    EndIf
    ;48
    If $value > 46 Then
    $hBrush = _GDIPlus_BrushCreateSolid("0xFFC8D90F")
    _GDIPlus_GraphicsFillRect($hGraphic, 0, 52, $width, $height, $hBrush)
    EndIf
    ;46
    If $value > 44 Then
    $hBrush = _GDIPlus_BrushCreateSolid("0xFFC4DF0F")
    _GDIPlus_GraphicsFillRect($hGraphic, 0, 54, $width, $height, $hBrush)
    EndIf
    ;44
    If $value > 42 Then
    $hBrush = _GDIPlus_BrushCreateSolid("0xFFC1E40F")
    _GDIPlus_GraphicsFillRect($hGraphic, 0, 56, $width, $height, $hBrush)
    EndIf
    ;42
    If $value > 40 Then
    $hBrush = _GDIPlus_BrushCreateSolid("0xFFBDEA0F")
    _GDIPlus_GraphicsFillRect($hGraphic, 0, 58, $width, $height, $hBrush)
    EndIf
    ;40
    If $value > 38 Then
    $hBrush = _GDIPlus_BrushCreateSolid("0xFFBAF00F")
    _GDIPlus_GraphicsFillRect($hGraphic, 0, 60, $width, $height, $hBrush)
    EndIf
    ;38
    If $value > 36 Then
    $hBrush = _GDIPlus_BrushCreateSolid("0xFFBAF00F")
    _GDIPlus_GraphicsFillRect($hGraphic, 0, 62, $width, $height, $hBrush)
    EndIf
    ;36
    If $value > 34 Then
    $hBrush = _GDIPlus_BrushCreateSolid("0xFFADEF0F")
    _GDIPlus_GraphicsFillRect($hGraphic, 0, 64, $width, $height, $hBrush)
    EndIf
    ;34
    If $value > 32 Then
    $hBrush = _GDIPlus_BrushCreateSolid("0xFFA1EE0F")
    _GDIPlus_GraphicsFillRect($hGraphic, 0, 66, $width, $height, $hBrush)
    EndIf
    ;32
    If $value > 30 Then
    $hBrush = _GDIPlus_BrushCreateSolid("0xFF94ED0F")
    _GDIPlus_GraphicsFillRect($hGraphic, 0, 68, $width, $height, $hBrush)
    EndIf
    ;30
    If $value > 28 Then
    $hBrush = _GDIPlus_BrushCreateSolid("0xFF88ED0F")
    _GDIPlus_GraphicsFillRect($hGraphic, 0, 70, $width, $height, $hBrush)
    EndIf
    ;28
    If $value > 26 Then
    $hBrush = _GDIPlus_BrushCreateSolid("0xFF7CEC0F")
    _GDIPlus_GraphicsFillRect($hGraphic, 0, 72, $width, $height, $hBrush)
    EndIf
    ;26
    If $value > 24 Then
    $hBrush = _GDIPlus_BrushCreateSolid("0xFF6FEB0F")
    _GDIPlus_GraphicsFillRect($hGraphic, 0, 74, $width, $height, $hBrush)
    EndIf
    ;24
    If $value > 22 Then
    $hBrush = _GDIPlus_BrushCreateSolid("0xFF63EA0F")
    _GDIPlus_GraphicsFillRect($hGraphic, 0, 76, $width, $height, $hBrush)
    EndIf
    ;22
    If $value > 20 Then
    $hBrush = _GDIPlus_BrushCreateSolid("0xFF56EA0F")
    _GDIPlus_GraphicsFillRect($hGraphic, 0, 78, $width, $height, $hBrush)
    EndIf
    ;20
    If $value > 18 Then
    $hBrush = _GDIPlus_BrushCreateSolid("0xFF56EA0F")
    _GDIPlus_GraphicsFillRect($hGraphic, 0, 80, $width, $height, $hBrush)
    EndIf
    ;18
    If $value > 16 Then
    $hBrush = _GDIPlus_BrushCreateSolid("0xFF4AE90F")
    _GDIPlus_GraphicsFillRect($hGraphic, 0, 82, $width, $height, $hBrush)
    EndIf
    ;16
    If $value > 14 Then
    $hBrush = _GDIPlus_BrushCreateSolid("0xFF3EE80F")
    _GDIPlus_GraphicsFillRect($hGraphic, 0, 84, $width, $height, $hBrush)
    EndIf
    ;14
    If $value > 12 Then
    $hBrush = _GDIPlus_BrushCreateSolid("0xFF31E70F")
    _GDIPlus_GraphicsFillRect($hGraphic, 0, 86, $width, $height, $hBrush)
    EndIf
    ;12
    If $value > 10 Then
    $hBrush = _GDIPlus_BrushCreateSolid("0xFF25E70F")
    _GDIPlus_GraphicsFillRect($hGraphic, 0, 88, $width, $height, $hBrush)
    EndIf
    ;10
    If $value > 8 Then
    $hBrush = _GDIPlus_BrushCreateSolid("0xFF18E60F")
    _GDIPlus_GraphicsFillRect($hGraphic, 0, 90, $width, $height, $hBrush)
    EndIf
    ;8
    If $value > 6 Then
    $hBrush = _GDIPlus_BrushCreateSolid("0xFF0CE50F")
    _GDIPlus_GraphicsFillRect($hGraphic, 0, 92, $width, $height, $hBrush)
    EndIf
    ;6
    If $value > 4 Then
    $hBrush = _GDIPlus_BrushCreateSolid("0xFF00E510")
    _GDIPlus_GraphicsFillRect($hGraphic, 0, 94, $width, $height, $hBrush)
    EndIf
    ;4
    If $value > 2 Then
    $hBrush = _GDIPlus_BrushCreateSolid("0xFF00E509")
    _GDIPlus_GraphicsFillRect($hGraphic, 0, 96, $width, $height, $hBrush)
    EndIf
    ;2
    $hBrush = _GDIPlus_BrushCreateSolid("0xFF00E509")
    _GDIPlus_GraphicsFillRect($hGraphic, 0, 98, $width, $height, $hBrush)

    $hBitmap = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hImage)
    GUICtrlSendMsg($target, 0x172, 0, $hBitmap)
    _WinAPI_DeleteObject($hBitmap)

EndFunc   ;<==_CreateBar

Func DLL_BitmapCreate($width, $height)
    Local $aResult = DllCall($__g_hGDIPDll, "uint", "GdipCreateBitmapFromScan0", "int", $width, "int", $height, "int", 0, "int", 0x0026200A, "ptr", 0, "int*", 0)
    Return $aResult[6]
EndFunc   ;<==DLL_BitmapCreate

 

Edited by JoeWagner
Link to comment
Share on other sites

Try this:

#include <Date.au3>
#include <WindowsConstants.au3>
#include <GuiConstantsEX.au3>
#include <EditConstants.au3>
#include <ButtonConstants.au3>
#include <GDIPlus.au3>

Global $__g_hGDIPDll
Global $graph[21], $bars[21]
Global $timer, $timeout = 1000

Global $hFlag = 0
_GDIPlus_Startup()
Global $hBrush, $hGraphic, $hImage, $hTexture, $width=5, $height=2, $factor = 50
$hImage = _GDIPlus_BitmapCreateFromScan0($width, $height*$factor)
$hGraphic = _GDIPlus_ImageGetGraphicsContext($hImage)
_GDIPlus_GraphicsSetSmoothingMode($hGraphic, 2)

Global $aColors[Ceiling($height*$factor / 2)] = [0xFFF00F2F, 0xFFF0182C, 0xFFF0212A, 0xFFF02B28, 0xFFF03426, 0xFFF03E24, 0xFFF04722, 0xFFF05120, 0xFFF05A1D, 0xFFF0641B, 0xFFF06D19, 0xFFF07717, 0xFFF08015, 0xFFF08A13, 0xFFF09311, 0xFFF09311, 0xFFF09D0F, 0xFFECA20F, 0xFFE8A80F, 0xFFE5AD0F, 0xFFE1B30F, 0xFFDEB80F, 0xFFDABE0F, 0xFFD6C30F, 0xFFCFCE0F, 0xFFCCD40F, 0xFFC8D90F, 0xFFC4DF0F, 0xFFC1E40F, 0xFFBDEA0F, 0xFFBAF00F, 0xFFBAF00F, 0xFFADEF0F, 0xFFA1EE0F, 0xFF94ED0F, 0xFF88ED0F, 0xFF7CEC0F, 0xFF6FEB0F, 0xFF63EA0F, 0xFF56EA0F, 0xFF56EA0F, 0xFF4AE90F, 0xFF3EE80F, 0xFF31E70F, 0xFF25E70F, 0xFF18E60F, 0xFF0CE50F, 0xFF00E510, 0xFF00E509, 0xFF00E509]

Global $hBmp = _GDIPlus_BitmapCreateFromScan0($width, $height*$factor), $hGfx = _GDIPlus_ImageGetGraphicsContext($hBmp)
$hBrush = _GDIPlus_BrushCreateSolid()
For $i = 0 To $height*$factor - 1
    _GDIPlus_BrushSetSolidColor($hBrush, $aColors[Floor($i / 2)])
    _GDIPlus_GraphicsFillRect($hGfx, 0, $i * 2, $width, 2, $hBrush)
Next
$hTexture = _GDIPlus_TextureCreate($hBmp)
_GDIPlus_ImageDispose($hBmp)
_GDIPlus_GraphicsDispose($hGfx)
_GDIPlus_BrushDispose($hBrush)

;==================================================
$main = GUICreate("CPU Graph", 125, 220, Default, Default, Default, BitOR($WS_EX_TOOLWINDOW, $WS_EX_TOPMOST))
    For $i = 1 To 20
        $bars[$i] = GUICtrlCreatePic("", 10 + ($i - 1) * 5, 10, 5, $height*$factor)
    Next
    $host = GUICtrlCreateInput("LocalHost", 10, 115, 100, 20, $ES_AUTOHSCROLL)
    $user = GUICtrlCreateInput("User", 10, 140, 100, 20, $ES_AUTOHSCROLL)
        GUICtrlSetFont(-1, 8, 400)
    $pass = GUICtrlCreateInput("Pass", 10, 165, 100, 20, BitOR($ES_PASSWORD,$ES_AUTOHSCROLL))
        GUICtrlSetFont(-1, 8, 400)
    $start = GUICtrlCreateButton("Start", 10, 190, 100, 20, $BS_DEFPUSHBUTTON)
GUISetState() ;<== Draw Main GUI

;== Main Processing Loop===================================
While 1
    $msg = GUIGetMsg()
    If $msg = $GUI_EVENT_CLOSE Then ExitLoop
    If $msg = $start Then ConnectWMI() ;<== connect host
    If $hFlag = 1 Then ;<== If host is connected
        If TimerDiff($timer) > $timeout Then UpdateGraph()
    EndIf
WEnd

;== Pre Exit Cleanup ======================================
    _GDIPlus_BrushDispose($hTexture)
    _GDIPlus_GraphicsDispose($hGraphic)
    _GDIPlus_ImageDispose($hImage)
    _GDIPlus_Shutdown()
;==========================================================

Func ConnectWMI() ; Connect WMI ==> (Establishes a connection to the WMI data on the remote system)
    Global $hostname = GUICtrlRead($host)
        If Ping($hostname, 2000) = 0 Then
            Msgbox(0, "Error", "Unable to reach specified host")
            Return 0
        EndIf
    Local $usr = GUICtrlRead($user)
    Local $pwd = GUICtrlRead($pass)

    Global $objSWbemLocator = ObjCreate("WbemScripting.SWbemLocator")
    Global $objWMIService = $objSWbemLocator.ConnectServer($hostname, "\root\cimv2", $usr, $pwd, "", "", 128)
    
    ;for testing purposes on local machine
;~     Global $objWMIService =  ObjGet("winmgmts:{impersonationLevel=impersonate,authenticationLevel=pktPrivacy, (Debug)}!\\" & $hostname & "\root\cimv2")
    If @error  Then
            Msgbox(0, "Error", "Unable to connect to the Host with the supplied credentials")
            Return 0
    EndIf
    $hFlag = 1
    UpdateGraph()
EndFunc ;<== Connect WMI

Func UpdateGraph(); Update Graph (sets data points, calls create bar to draw the graph)
    $usage = _Processor_Usage()
    For $i = 1 to 19
        $graph[$i] = $graph[$i+1]
    Next
    $graph[20] = $usage

    For $i = 1 to 20
        CreateBar($bars[$i], $graph[$i])
    Next
    $timer = TimerInit()
EndFunc ;<== Update Graph

Func _Processor_Usage(); Processor Usage ==>  WMI Script to pull CPU usage each cycle
    Dim $Col_Items = $objWMIService.ExecQuery('SELECT * FROM Win32_PerfFormattedData_PerfOS_Processor', 'WQL')
    Local $Obj_Item
    For $Obj_Item In $Col_Items
        Return $Obj_Item.PercentProcessorTime
    Next
EndFunc ;<== Processor Usage


;==== GDI+ Functions =================================================

Func CreateBar($target, $value)
;~     GUICtrlSetImage($target, "") ; <- creates a memory leak!
    _GDIPlus_GraphicsClear($hGraphic, 0xFF000000)
    _GDIPlus_GraphicsFillRect($hGraphic, 0, $height * $factor - $value, $width, $value, $hTexture)

    Local Const $hBitmap = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hImage)
    _WinAPI_DeleteObject(GUICtrlSendMsg($target, 0x172, 0, $hBitmap))
    _WinAPI_DeleteObject($hBitmap)

EndFunc   ;<==_CreateBar

I've shorten the code a little bit!

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

Add this somewhere at the top of the script:

Global $o_Autoit_Object_Error_Handler = ObjEvent("AutoIt.Error", "_Autoit_Object_Error_Function") ; Initialize a COM error handler
Func _Autoit_Object_Error_Function()
    ConsoleWrite("! We intercepted a COM Error !" & @CRLF & "=======================" & @CRLF & _
            "err.description is: " & @TAB & $o_Autoit_Object_Error_Handler.description & @CRLF & _
            "err.windescription:" & @TAB & $o_Autoit_Object_Error_Handler.windescription & @CRLF & _
            "err.number is: " & @TAB & Hex($o_Autoit_Object_Error_Handler.number, 8) & @CRLF & _
            "err.lastdllerror is: " & @TAB & $o_Autoit_Object_Error_Handler.lastdllerror & @CRLF & _
            "err.scriptline is: " & @TAB & $o_Autoit_Object_Error_Handler.scriptline & @CRLF & _
            "err.source is: " & @TAB & $o_Autoit_Object_Error_Handler.source & @CRLF & _
            "err.helpfile is: " & @TAB & $o_Autoit_Object_Error_Handler.helpfile & @CRLF & _
            "err.helpcontext is: " & @TAB & $o_Autoit_Object_Error_Handler.helpcontext & @CRLF & @CRLF)
EndFunc   ;==>_Autoit_Object_Error_Function

 

Link to comment
Share on other sites

I'll experiment with the error handler... Thanks KaFu

I'm having some challenges with the GDI graph still - I'm missing something with the way you spawned the image and applied the texture.

  • Why doesn't  _GDIPlus_GraphicsClear($hGraphic, 0xFF000000clear the whole graphic - we didn't specify which to clear.
  • The Texture appears to only map the top half of the color spectrum (yellow to red) so that's all that shows, even though I traced how it works and the whole spectrum is part of the texture - its like the texture is too large (by double)

Thinking about this - Would it be possible to pre-draw 50 bars (2,4,6,8... 100)  and store them in an array then just assign them like images as needed?

  • GuiCtrlSetImage($bar[$i], $img[$value]  - Where $img[51] is an array of images with respective heights based on the index #

 

Link to comment
Share on other sites

Quote
  • Why doesn't  _GDIPlus_GraphicsClear($hGraphic, 0xFF000000clear the whole graphic - we didn't specify which to clear.

It clears only the graphic handle ($hGraphic) as it will be used to draw each graph bar within the loop in function UpdateGraph().

 

Quote

Thinking about this - Would it be possible to pre-draw 50 bars (2,4,6,8... 100)  and store them in an array then just assign them like images as needed?

  • GuiCtrlSetImage($bar[$i], $img[$value]  - Where $img[51] is an array of images with respective heights based on the index #

Sure, it is possible but it will consume more memory. As the draw function is called once per second the CPU usage of each calculation for each graph bar is negligible.

 

Quote
  • The Texture appears to only map the top half of the color spectrum (yellow to red) so that's all that shows, even though I traced how it works and the whole spectrum is part of the texture - its like the texture is too large (by double)

Here the updated version which uses HSL for calculation of the gradient from green over yellow to red:

#include <Date.au3>
#include <WindowsConstants.au3>
#include <GuiConstantsEX.au3>
#include <EditConstants.au3>
#include <ButtonConstants.au3>
#include <GDIPlus.au3>

Global $o_Autoit_Object_Error_Handler = ObjEvent("AutoIt.Error", "_Autoit_Object_Error_Function") ; Initialize a COM error handler

Global $__g_hGDIPDll
Global $graph[21], $bars[21]
Global $timer, $timeout = 1000

Global $hFlag = 0
_GDIPlus_Startup()
Global $hBrush, $hGraphic, $hImage, $hTexture, $width=5, $height=100
$hImage = _GDIPlus_BitmapCreateFromScan0($width, $height)
$hGraphic = _GDIPlus_ImageGetGraphicsContext($hImage)
_GDIPlus_GraphicsSetSmoothingMode($hGraphic, 2)

;create gradient from green over yellow to red using HSL values
Global $aColors[Ceiling($height)]
Local $iSteps = 120 / UBound($aColors), $i, $h
For $h = 120 To 0 Step -$iSteps
    $aColors[$i] = 0xFF000000 + HSLToRGB((120 + $h) / 360, 1, 0.5)
    $i += 1
Next

;create one texture which will used to draw the CPU usage accordingly
Global $hBmp = _GDIPlus_BitmapCreateFromScan0($width, $height), $hGfx = _GDIPlus_ImageGetGraphicsContext($hBmp)
$hBrush = _GDIPlus_BrushCreateSolid()
For $i = 0 To UBound($aColors) - 1
    _GDIPlus_BrushSetSolidColor($hBrush, $aColors[$i])
    _GDIPlus_GraphicsFillRect($hGfx, 0, $i, $width, $height, $hBrush)
Next

$hTexture = _GDIPlus_TextureCreate($hBmp)
_GDIPlus_ImageDispose($hBmp)
_GDIPlus_GraphicsDispose($hGfx)
_GDIPlus_BrushDispose($hBrush)

;==================================================
$main = GUICreate("CPU Graph", 125, 220, Default, Default, Default, BitOR($WS_EX_TOOLWINDOW, $WS_EX_TOPMOST))
    For $i = 1 To 20
        $bars[$i] = GUICtrlCreatePic("", 10 + ($i - 1) * 5, 10, 5, $height)
    Next
    $host = GUICtrlCreateInput("LocalHost", 10, 115, 100, 20, $ES_AUTOHSCROLL)
    $user = GUICtrlCreateInput("User", 10, 140, 100, 20, $ES_AUTOHSCROLL)
        GUICtrlSetFont(-1, 8, 400)
    $pass = GUICtrlCreateInput("Pass", 10, 165, 100, 20, BitOR($ES_PASSWORD,$ES_AUTOHSCROLL))
        GUICtrlSetFont(-1, 8, 400)
    $start = GUICtrlCreateButton("Start", 10, 190, 100, 20, $BS_DEFPUSHBUTTON)
GUISetState() ;<== Draw Main GUI

;== Main Processing Loop===================================
While 1
    $msg = GUIGetMsg()
    If $msg = $GUI_EVENT_CLOSE Then ExitLoop
    If $msg = $start Then ConnectWMI() ;<== connect host
    If $hFlag = 1 Then ;<== If host is connected
        If TimerDiff($timer) > $timeout Then UpdateGraph()
    EndIf
WEnd

;== Pre Exit Cleanup ======================================
    _GDIPlus_BrushDispose($hTexture)
    _GDIPlus_GraphicsDispose($hGraphic)
    _GDIPlus_ImageDispose($hImage)
    _GDIPlus_Shutdown()
;==========================================================

Func ConnectWMI() ; Connect WMI ==> (Establishes a connection to the WMI data on the remote system)
    Global $hostname = GUICtrlRead($host)
        If Ping($hostname, 2000) = 0 Then
            Msgbox(0, "Error", "Unable to reach specified host")
            Return 0
        EndIf
    Local $usr = GUICtrlRead($user)
    Local $pwd = GUICtrlRead($pass)

    Global $objSWbemLocator = ObjCreate("WbemScripting.SWbemLocator")
    Global $objWMIService = $objSWbemLocator.ConnectServer($hostname, "\root\cimv2", $usr, $pwd, "", "", 128)
    ;next line for testing purposes on local machine
;~     Global $objWMIService =  ObjGet("winmgmts:{impersonationLevel=impersonate,authenticationLevel=pktPrivacy, (Debug)}!\\" & $hostname & "\root\cimv2")
    If @error  Then
            Msgbox(0, "Error", "Unable to connect to the Host with the supplied credentials")
            Return 0
    EndIf
    $hFlag = 1
    UpdateGraph()
EndFunc ;<== Connect WMI

Func UpdateGraph(); Update Graph (sets data points, calls create bar to draw the graph)
    $usage = _Processor_Usage()
    For $i = 1 to 19
        $graph[$i] = $graph[$i+1]
    Next
    $graph[20] = $usage

    For $i = 1 to 20
        CreateBar($bars[$i], $graph[$i])
    Next
    $timer = TimerInit()
EndFunc ;<== Update Graph

Func _Processor_Usage(); Processor Usage ==>  WMI Script to pull CPU usage each cycle
    Dim $Col_Items = $objWMIService.ExecQuery('SELECT * FROM Win32_PerfFormattedData_PerfOS_Processor', 'WQL')
    Local $Obj_Item
    For $Obj_Item In $Col_Items
        Return $Obj_Item.PercentProcessorTime
    Next
EndFunc ;<== Processor Usage

Func HSLToRGB($h, $s, $l)
    If Not $s Then Return BitShift(0xFF * $l, -16) + BitShift(0xFF * $l, -8) + BitShift(0xFF * $l, 0)
    Local Const $q = $l < 0.5 ? $l * (1 + $s) : $l + $s - $l * $s
    Local Const $p = 2 * $l - $q
    Return BitShift(0xFF * HUEtoRGB($p, $q, $h - 0.33333333), -16) + BitShift(0xFF * HUEtoRGB($p, $q, $h), -8) + BitShift(0xFF * HUEtoRGB($p, $q, $h + 0.33333333), 0)
EndFunc

Func HUEtoRGB($p, $q, $t)
    If($t < 0) Then $t += 1
    If($t > 1) Then $t -= 1
    If($t < 0.16666666) Then Return $p + ($q - $p) * 6 * $t
    If($t < 0.5) Then Return $q
    If($t < 0.66666666) Then Return $p + ($q - $p) * (0.66666666 - $t) * 6
    Return $p
EndFunc


;==== GDI+ Functions =================================================

Func CreateBar($target, $value)
    _GDIPlus_GraphicsClear($hGraphic, 0xFF000000)
    _GDIPlus_GraphicsFillRect($hGraphic, 0, $height - $value, $width, $value, $hTexture)

    Local Const $hBitmap = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hImage)
    _WinAPI_DeleteObject(GUICtrlSendMsg($target, 0x172, 0, $hBitmap))
    _WinAPI_DeleteObject($hBitmap)

EndFunc   ;<==_CreateBar

Func _Autoit_Object_Error_Function()
    ConsoleWrite("! We intercepted a COM Error !" & @CRLF & "=======================" & @CRLF & _
            "err.description is: " & @TAB & $o_Autoit_Object_Error_Handler.description & @CRLF & _
            "err.windescription:" & @TAB & $o_Autoit_Object_Error_Handler.windescription & @CRLF & _
            "err.number is: " & @TAB & Hex($o_Autoit_Object_Error_Handler.number, 8) & @CRLF & _
            "err.lastdllerror is: " & @TAB & $o_Autoit_Object_Error_Handler.lastdllerror & @CRLF & _
            "err.scriptline is: " & @TAB & $o_Autoit_Object_Error_Handler.scriptline & @CRLF & _
            "err.source is: " & @TAB & $o_Autoit_Object_Error_Handler.source & @CRLF & _
            "err.helpfile is: " & @TAB & $o_Autoit_Object_Error_Handler.helpfile & @CRLF & _
            "err.helpcontext is: " & @TAB & $o_Autoit_Object_Error_Handler.helpcontext & @CRLF & @CRLF)
EndFunc   ;==>_Autoit_Object_Error_Function

 

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...