03.10.2025, 03:06 AM
Patrice, I'm not getting any better results, perhaps in this case. It's OK, I do have a custom GDI+ round gauge to finish working on later. For now, this simple gauge is good enough for the testing PCB application at work.
Code:
'=== draw needle using PlgBlt with corrected points ===
angle = (225 - (value * 2.7!)) * cherryPi / 180
'-create a memory DC and bitmap for the needle
LOCAL hMemDC AS DWORD, hBitmap AS DWORD, hOldBitmap AS DWORD
LOCAL hMaskDC AS DWORD, hMaskBitmap AS DWORD, hOldMaskBitmap AS DWORD
LOCAL needleHeight AS LONG
needleWidth = 14
needleHeight = radius * 0.80
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)) '-red fill
hOldBrush = SelectObject(hMemDC, hBrush)
hPen = CreatePen(%PS_SOLID, 1, RGB(0, 0, 0)) '-black outline
hOldPen = SelectObject(hMemDC, hPen)
'-define triangle points in the bitmap, *base at top, tip at bottom
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 '-base, left top left
bmpPts(2).x = needleWidth : bmpPts(2).y = 0 '-base, right top right
POLYGON hMemDC, bmpPts(0), 3
SelectObject hMemDC, hOldBrush
DeleteObject hBrush
SelectObject hMemDC, hOldPen
DeleteObject hPen
' -create 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)) '-white = opaque
hOldBrush = SelectObject(hMaskDC, hBrush)
POLYGON hMaskDC, bmpPts(0), 3 '-same triangle for mask
SelectObject hMaskDC, hOldBrush
DeleteObject hBrush
'-set stretch mode for better quality
'SetStretchBltMode hDC, %HALFTONE
'-define destination points using unit vectors (per Patrice)
DIM pts(0 TO 2) AS POINTAPI
LOCAL ux, uy, vx, vy, halfW, baseX, baseY AS SINGLE
ux = COS(angle) : uy = -SIN(angle) '-forward, toward tip
vx = COS(angle + cherryPi / 2) : vy = -SIN(angle + cherryPi / 2) '-left normal
halfW = needleWidth / 2
LOCAL h AS LONG
h = needleHeight
baseX = cx : baseY = cy
'-parallelogram corners for PlgBlt
' UL (maps source 0,0) base - halfW * v
pts(0).x = baseX - halfW * vx
pts(0).y = baseY - halfW * vy
' UR (maps source w,0) base + halfW * v
pts(1).x = baseX + halfW * vx
pts(1).y = baseY + halfW * vy
' LL (maps source 0,h) base + h * u - halfW * v
pts(2).x = baseX + h * ux - halfW * vx
pts(2).y = baseY + h * uy - halfW * vy
'-do the PlgBlt with mask
PlgBlt hDC, pts(0), hMemDC, 0, 0, needleWidth, needleHeight, hMaskBitmap, 0, 0
'-clean up
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