Very Simple Round Gauge 0-100%
#4
Kevin, I like your color ring better than mine.  Wink

Code:
'--------------------------------------------------------------------------------------------------
'  Kevin Diggins BCX translation to PowerBASIC, Kevin's color ring looks better than mine.
'--------------------------------------------------------------------------------------------------
SUB DrawGauge2(hDC AS DWORD, cx AS LONG, cy AS LONG, radius AS LONG, value AS LONG)

    LOCAL angle, rad, offsetRad, aDeg, percent, needleWidth AS SINGLE
    LOCAL hPen, hOldPen, hFont, hOldFont, hBrush, hOldBrush AS DWORD
    LOCAL i, x1, y1, x2, y2, xTip, yTip, r, g, b AS LONG
    LOCAL xBase1, yBase1, xBase2, yBase2 AS LONG
    LOCAL sVal AS STRING, lsize AS SIZE, cherryPi AS EXT

    cherryPi = 4*ATN(1)

    '=== Arc background with green-yellow-red segments ===
    FOR i = 0 TO 100 STEP 1    'draw every degree for smoother arc
        aDeg = 225 - (i * 2.7) 'map 0-100 as 225 to -45 degrees
        rad = aDeg * cherryPi / 180
        x1 = cx + radius * 0.85 * COS(rad)
        y1 = cy - radius * 0.85 * SIN(rad)
        x2 = cx + radius * 0.95 * COS(rad)
        y2 = cy - radius * 0.95 * SIN(rad)

        'calculate color based on position (0~100)
        IF i <= 33 THEN
            ' Green to yellow (0-33)
            r = INT(i * 255 / 33)
            g = 255
            b = 0
        ELSEIF i <= 67 THEN
            ' Yellow to red (33-67)
            r = 255
            g = INT(255 - ((i - 33) * 255 / 34))
            b = 0
        ELSE
            ' Red (67-100)
            r = 255
            g = 0
            b = 0
        END IF

        hPen = CreatePen(%PS_SOLID, 3, RGB(r, g, b))
        hOldPen = SelectObject(hDC, hPen)
        MoveToEx hDC, x1, y1, BYVAL %NULL
        LineTo hDC, x2, y2
        SelectObject hDC, hOldPen
        DeleteObject hPen
    NEXT


    #IF 1 '=== Optional chintzy bezel ring ===
        hPen = CreatePen(%PS_SOLID, 4, RGB(190, 190, 190))
        hOldPen = SelectObject(hDC, hPen)
        LOCAL ltweak AS LONG
        ltweak = -2 '-tweak gap to your own taste
        ARC hDC, cx - radius - ltweak, cy - radius - ltweak, cx + radius + ltweak, cy + radius + ltweak, 0, 0, 0, 0
        SelectObject hDC, hOldPen
        DeleteObject hPen
    #ENDIF

    '=== tick marks and labels every 5 units ===
     hPen = CreatePen(%PS_SOLID, 1, RGB(0, 0, 0))
     hOldPen = SelectObject(hDC, hPen)

    'create font for labels
    LOCAL hLabelFont AS DWORD
    hLabelFont = CreateFont(22, 0, 0, 0, %FW_BOLD, 0, 0, 0, _
    %ANSI_CHARSET, %OUT_DEFAULT_PRECIS, %CLIP_DEFAULT_PRECIS, _
    %DEFAULT_QUALITY, %DEFAULT_PITCH OR %FF_DONTCARE, "Segoe UI")
    hOldFont = selectObject(hDC, hLabelFont)

    SetBkMode(hDC, %TRANSPARENT)
    SetTextColor(hDC, RGB(40, 140, 240))


    FOR i = 0 TO 100 STEP 5
        angle = (225 - (i * 2.7!)) * cherryPi / 180
        '-major tick length for labels every 10 units, short for 5 untis
        IF (i MOD 10) = 0 THEN
            x1 = cx + radius * 0.78 * COS(angle)
            y1 = cy - radius * 0.78 * SIN(angle)
            x2 = cx + radius * 0.88 * COS(angle)
            y2 = cy - radius * 0.88 * SIN(angle)
            'draw label
            LOCAL stext AS STRING
            sText = FORMAT$(i)
            LOCAL tx AS LONG, ty AS LONG
            GetTextExtentPoint32(hDC, BYVAL STRPTR(sText), LEN(sText), lsize)
            tx = cx + radius * 0.65 * COS(angle) - lsize.cx / 2
            ty = cy - radius * 0.65 * SIN(angle) - lsize.cy / 2
            TextOut(hDC, tx, ty, BYVAL STRPTR(sText), LEN(sText))
        ELSE
            x1 = cx + radius * 0.82 * COS(angle)
            y1 = cy - radius * 0.82 * SIN(angle)
            x2 = cx + radius * 0.88 * COS(angle)
            y2 = cy - radius * 0.88 * SIN(angle)
        END IF

        MoveToEx (hDC, x1, y1, BYVAL %NULL)
        LineTo (hDC, x2, y2)
    NEXT

    SelectObject(hDC, hOldFont)
    DeleteObject(hLabelFont)
    SelectObject(hDC, hOldPen)
    DeleteObject(hPen)

    '=== draw polygon needle ===
    angle = (225 - (value * 2.7!)) * cherryPi / 180

    '-tip of the needle
    xTip = cx + radius * 0.75 * COS(angle)
    yTip = cy - radius * 0.75 * SIN(angle)

    '-base width and angle offset for polygon
    needleWidth = 6.0  '<-adjust width here
    offsetRad = 90 * cherryPi / 180  '-perpendicular

    '-two base corners *perpendicular to needle angle
    xBase1 = cx + needleWidth * COS(angle + offsetRad)
    yBase1 = cy - needleWidth * SIN(angle + offsetRad)
    xBase2 = cx + needleWidth * COS(angle - offsetRad)
    yBase2 = cy - needleWidth * SIN(angle - offsetRad)

    '-define needle polygon points
    DIM pts(0 TO 2) AS POINTAPI
    pts(0).x = xTip   : pts(0).y = yTip
    pts(1).x = xBase1 : pts(1).y = yBase1
    pts(2).x = xBase2 : pts(2).y = yBase2

    '-draw needle
    hBrush = CreateSolidBrush(RGB(255, 0, 0))    '-fill color
    hOldBrush = SelectObject(hDC, hBrush)
    hPen = CreatePen(%PS_SOLID, 1, RGB(0, 0, 0)) '-outline
    hOldPen = SelectObject(hDC, hPen)
    POLYGON hDC, pts(0), 3
    SelectObject hDC, hOldBrush
    DeleteObject hBrush
    SelectObject hDC, hOldPen
    DeleteObject hPen

    '-center cap
    hPen = CreatePen(%PS_SOLID, 1, RGB(0, 0, 0)) '-outline
    hOldPen = SelectObject(hDC, hPen)
    ELLIPSE hDC, cx - 8, cy - 8, cx + 8, cy + 8
    SelectObject hDC, hOldPen
    DeleteObject hPen

    '=== digital value display in the center ===
    sVal = FORMAT$(value) + "%"
    hFont = CreateFont(28, 0, 0, 0, %FW_BOLD, 0, 0, 0, _
                %ANSI_CHARSET, %OUT_DEFAULT_PRECIS, %CLIP_DEFAULT_PRECIS, _
                %DEFAULT_QUALITY, %DEFAULT_PITCH OR %FF_DONTCARE, "Segoe UI")
    hOldFont = SelectObject(hDC, hFont)
    SetBkMode hDC, %TRANSPARENT
    SetTextColor hDC, RGB(0, 102, 204)
    '-measure text size to center it
    GetTextExtentPoint32 hDC, BYVAL STRPTR(sVal), LEN(sVal), lsize
    TextOut hDC, cx - lsize.cx \ 2, cy - lsize.cy \ 2 +60, BYVAL STRPTR(sVal), LEN(sVal)
    SelectObject hDC, hOldFont
    DeleteObject hFont

END SUB


                       
Reply


Messages In This Thread
RE: Very Simple Round Gauge 0-100% - by Jules Marchildon - 30.09.2025, 02:00 AM

Forum Jump:


Users browsing this thread: 1 Guest(s)