Very Simple Round Gauge Discussion Thread
#4
Patrice,  in reference to your PlgBlt() suggestion,  I can't seem to get my needle to draw correctly, does anything pop out at you in this snippet?  

Code:
    '=== draw needle using PlgBlt ===
    angle = (225 - (value * 2.7!)) * cherryPi / 180

    '-memory DC and bitmap for the needle
    LOCAL hMemDC, hBitmap, hOldBitmap AS DWORD
    LOCAL hMaskDC, hMaskBitmap, hOldMaskBitmap AS DWORD
    LOCAL needleHeight AS LONG

    needleWidth = 6
    needleHeight = radius * 0.75

    hMemDC = CreateCompatibleDC(hDC)
    hBitmap = CreateCompatibleBitmap(hDC, needleWidth, needleHeight)
    hOldBitmap = SelectObject(hMemDC, hBitmap)

    '-draw a triangular needle in the source bitmap
    hBrush = CreateSolidBrush(RGB(255, 0, 0))
    hOldBrush = SelectObject(hMemDC, hBrush)
    hPen = CreatePen(%PS_SOLID, 1, RGB(0, 0, 0))
    hOldPen = SelectObject(hMemDC, hPen)

    '-triangle points
    DIM bmpPts(0 TO 2) AS POINTAPI
    bmpPts(0).x = needleWidth \ 2 : bmpPts(0).y = needleHeight  '-tip   *bottom center
    bmpPts(1).x = 0               : bmpPts(1).y = 0             '-baseL *top left
    bmpPts(2).x = needleWidth     : bmpPts(2).y = 0             '-baseR *top right

    POLYGON hMemDC, bmpPts(0), 3

    SelectObject hMemDC, hOldBrush
    DeleteObject hBrush
    SelectObject hMemDC, hOldPen
    DeleteObject hPen

    '-a 1-bit mask for transparency
    hMaskDC = CreateCompatibleDC(hDC)
    hMaskBitmap = CreateBitmap(needleWidth, needleHeight, 1, 1, BYVAL %NULL)
    hOldMaskBitmap = SelectObject(hMaskDC, hMaskBitmap)

    hBrush = CreateSolidBrush(RGB(255, 255, 255))
    hOldBrush = SelectObject(hMaskDC, hBrush)
    POLYGON hMaskDC, bmpPts(0), 3
    SelectObject hMaskDC, hOldBrush
    DeleteObject hBrush

    SetStretchBltMode hDC, %HALFTONE  '<-set stretch mode for better quality

    DIM pts(0 TO 2) AS POINTAPI
    pts(0).x = cx + (radius * 0.75) * COS(angle)
    pts(0).y = cy - (radius * 0.75) * SIN(angle)
    offsetRad = 90 * cherryPi / 180
    pts(1).x = cx + needleWidth * COS(angle + offsetRad)
    pts(1).y = cy - needleWidth * SIN(angle + offsetRad)
    pts(2).x = cx + needleWidth * COS(angle - offsetRad)
    pts(2).y = cy - needleWidth * SIN(angle - offsetRad)

    PlgBlt hDC, pts(0), hMemDC, 0, 0, needleWidth, needleHeight, hMaskBitmap, 0, 0

    SelectObject hMemDC, hOldBitmap
    DeleteObject hBitmap
    DeleteDC hMemDC
    SelectObject hMaskDC, hOldMaskBitmap
    DeleteObject hMaskBitmap
    DeleteDC hMaskDC

    '=== 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

             
Reply


Messages In This Thread
RE: Very Simple Round Gauge Discussion Thread - by Jules Marchildon - 30.09.2025, 02:55 AM

Forum Jump:


Users browsing this thread: 1 Guest(s)