Jump to content

How to properly create a UI element/control


CYBRIX
 Share

Recommended Posts

So I am a person who has learned programming off the internet, without structured courses, and I'm trying to create a UI element that can be used like "GUICtrlCreate...".

I'd like to get some kind of insight on how to improve the code of a Graph element that I have created.
I am unfamiliar on the standards for creating such UI elements, and assume I'll get some valuable insight here.

 

Here's some sample code: (I tried to make it look a bit less upsetting to those who know better, but be warned: not pretty.)

#include <WinAPISys.au3>
#include <WinAPI.au3>
#include <WinAPIGdi.au3>
#include <Array.au3>
#include <WindowsConstants.au3>
#include <GUIConstants.au3>

Local $hWnd = GUICreate("Test", 200, 200)
GUISetState(@SW_SHOW, $hWnd)
Local $hGraph = _GraphStartUp($hWnd, 10, 30, 180, 180)
Local $nData, $nDataMax = 1000, $hDataTimer = TimerInit()


While GUIGetMsg() <> $GUI_EVENT_ClOSE

    If TimerDiff($hDataTimer) >= 150 Then
        $nData = Random(0, $nDataMax)
        _UpdateGraph($nData)
        $hDataTimer = TimerInit()
    EndIf

    Sleep(20)
WEnd

_GraphShutDown($hWnd, $hGraph)
Exit



Func _GraphStartUp($hWnd, $x, $y, $w, $h, $nUpdateTimes = 1000, $nResolution = 60)
    Global $hBluePen = _WinAPI_CreatePen($PS_SOLID, 2, _WinAPI_RGB(220, 0, 0))
    Global $hGreyPen = _WinAPI_CreatePen($PS_DASH, 1, _WinAPI_RGB(100, 100, 100))
    Global $hBlackPen = _WinAPI_CreatePen($PS_SOLID, 1, _WinAPI_RGB(0, 0, 0))
    Global $hWhitePen = _WinAPI_CreatePen($PS_SOLID, 1, _WinAPI_RGB(255, 255, 255))

    Global $nGraphRes = $nResolution

    Global $aGraphData[$nGraphRes + 1]
    For $i = 2 To $nGraphRes
        $aGraphData[$i] = 0
    Next
    Global $hWinDC = _WinAPI_GetWindowDC($hWnd)
    Global $nGraphX = $x
    Global $nGraphY = $y
    Global $nGraphWidth = $w
    Global $nGraphHeight = $h

    Global $nGraphXUnit = $nGraphWidth / ($nGraphRes - 1)
    Global $nGraphBottomY = $nGraphY + $nGraphHeight
    Global $nGraphMaxX = $nGraphX + $nGraphWidth

    Global $aUpdateArea[4][2] = [[$nGraphX - 1, $nGraphY - 1], [$nGraphX - 1, $nGraphBottomY + 1], [$nGraphMaxX + 1, $nGraphBottomY + 1], [$nGraphMaxX + 1, $nGraphY - 1]]
    Global $pUpdateAreaRgn = _WinAPI_CreatePolygonRgn($aUpdateArea)
    Global $pTextRect = _WinAPI_CreateRectEx($nGraphX + 1, $nGraphY + 1, $nGraphWidth / 2, $nGraphHeight / 4)

    Global $nGreatestValue = 1
    GLobal $pGraphArea = _WinAPI_CreateRectEx($nGraphX - 7, $nGraphY - 26, $nGraphWidth + 14, $nGraphHeight + 2)

    Global $pDrawCall = DllCallbackRegister('_DrawGraph', 'none', '')
    Global $pGraphTimer = _WinAPI_SetTimer($hWnd, 567891234, $nUpdateTimes, DllCallbackGetPtr($pDrawCall))

    _DrawGraph()

    Return $pGraphTimer
EndFunc

Func _DrawGraph()

    Global $hPen

    $nGreatestValue = _ArrayMax($aGraphData, 1, 1)

    $hPen = _WinAPI_SelectObject($hWinDC, $hWhitePen)
    _WinAPI_PaintRgn($hWinDC, $pUpdateAreaRgn)

    $hPen = _WinAPI_SelectObject($hWinDC, $hGreyPen)
    _WinAPI_DrawLine($hWinDC, $nGraphX, $nGraphY, $nGraphMaxX, $nGraphY)
    _WinAPI_DrawLine($hWinDC, $nGraphX, $nGraphY + ($nGraphHeight / 2), $nGraphMaxX, $nGraphY + ($nGraphHeight / 2))
    _WinAPI_DrawLine($hWinDC, $nGraphMaxX, $nGraphY, $nGraphMaxX, $nGraphBottomY)


    $hPen = _WinAPI_SelectObject($hWinDC, $hBluePen)
    For $i = 1 To $nGraphRes - 1
        _WinAPI_DrawLine($hWinDC, $nGraphX + (($i - 1) * $nGraphXUnit), $nGraphBottomY - ($aGraphData[$i] / $nGreatestValue * $nGraphHeight),  $nGraphX + ($i * $nGraphXUnit), $nGraphBottomY - ($aGraphData[$i + 1] / $nGreatestValue * $nGraphHeight))
    Next


    $hPen = _WinAPI_SelectObject($hWinDC, $hBlackPen)
    _WinAPI_DrawLine($hWinDC, $nGraphX, $nGraphBottomY, $nGraphMaxX, $nGraphBottomY)
    _WinAPI_DrawLine($hWinDC, $nGraphX, $nGraphY, $nGraphX, $nGraphBottomY)

    _WinAPI_DrawText($hWinDC, Round($aGraphData[$nGraphRes], 2), $pTextRect, $DT_LEFT)

EndFunc

Func _UpdateGraph($nData)
    _ArrayAdd($aGraphData, $nData)
    _ArrayDelete($aGraphData, 1)
EndFunc

Func _GraphShutDown($hWnd, $pGraphTimer)
    _WinAPI_SelectObject($hWinDC, $hPen)
    _WinAPI_DeleteObject($hBlackPen)
    _WinAPI_DeleteObject($hGreyPen)
    _WinAPI_DeleteObject($hBluePen)
    _WinAPI_DeleteObject($hWhitePen)
    _WinAPI_ReleaseDC($hWnd, $hWinDC)
    _WinAPI_KillTimer($hWnd, $pGraphTimer)
    DllCallbackFree($pDrawCall)
    _WinAPI_RedrawWindow($hWnd, $pGraphArea)
EndFunc

 

Edited by CYBRIX
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...