Skip to content

Instantly share code, notes, and snippets.

@KubaO
Last active January 12, 2026 19:12
Show Gist options
  • Select an option

  • Save KubaO/411ef011e31ced673faa0d7c4993f89f to your computer and use it in GitHub Desktop.

Select an option

Save KubaO/411ef011e31ced673faa0d7c4993f89f to your computer and use it in GitHub Desktop.
twinBASIC minimal GDI+ example
' Based on https://www.vbforums.com/showthread.php?598771-VB6-GDI-Usage-amp-Samples
' Dependencies: None other than the default packages in a new project.
[FormDesignerId("C5DA6B58-D856-43E1-A32A-56C86E5BAA59")]
Class frmMain
Option Explicit
Dim token As LongPtr
Dim usedHDC As LongPtr
Sub Form_Load()
ScaleMode = vbPixels
Dim GSI As GdiplusStartupInput
GSI.GdiplusVersion = 1&
GdiplusStartup(token, GSI)
cboSample.AddItem "Diagonal Gradient"
cboSample.AddItem "Vertical Gradient"
cboSample.AddItem "Horizontal Gradient"
cboSample.ListIndex = 0
cboSample.SetFocus
usedHDC = hDC
'PaintExample
End Sub
Sub Form_Unload(cancel%)
GdiplusShutdown(token)
End Sub
Sub PaintExample() Handles Form.Resize, Form.Paint, cboSample.Click
Dim hBrush As LongPtr, hGraphics As LongPtr
Dim p1 As POINT, p2 As POINT
Select Case cboSample.ListIndex
Case 0
p1.x = 0
p1.y = 0
p2.x = ScaleWidth
p2.y = ScaleHeight
GdipCreateLineBrushI(p1, p2, RGBtoARGB(vbWhite), RGBtoARGB(vbBlue), WrapModeTile, hBrush)
Case 1
p1.x = ScaleWidth \ 2
p1.y = 0
p2.x = p1.x
p2.y = ScaleHeight
GdipCreateLineBrushI(p1, p2, RGBtoARGB(vbYellow), RGBtoARGB(vbBlue), WrapModeTile, hBrush)
Case 2
p1.x = 0
p1.y = ScaleHeight \ 2
p2.x = ScaleWidth
p2.y = p1.y
GdipCreateLineBrushI(p1, p2, RGBtoARGB(vbRed), RGBtoARGB(vbBlue), WrapModeTile, hBrush)
End Select
GdipCreateFromHDC(hDC, hGraphics)
GdipFillRectangleI(hGraphics, hBrush, 0, 0, ScaleWidth, ScaleHeight)
GdipDeleteBrush(hBrush)
GdipDeleteGraphics(hGraphics)
End Sub
End Class
Module Helper
Public Function RGBtoARGB(ByVal RGBcolor&, Optional ByVal Opacity As Byte = 255) As Long
RGBtoARGB = (RGBcolor And &HFF) * &H10000 Or ((RGBcolor And &HFFFF&) \ &H100&) * &H100& Or ((RGBcolor And &HFFFFFF) \ &H10000)
RGBtoARGB = RGBtoARGB Or ((Opacity - 128) * &H1000000) Or &H80000000
End Function
Public Type GdiplusStartupInput
GdiplusVersion As Long
DebugEventCallback As LongPtr
SuppressBackgroundThread As BOOL
SuppressExternalCodecs As BOOL
End Type
Public Type POINT
x As Long
y As Long
End Type
Public Enum GpWrapMode
WrapModeTile ' // 0
WrapModeTileFlipX ' // 1
WrapModeTileFlipY ' // 2
WrapModeTileFlipXY ' // 3
WrapModeClamp ' // 4
End Enum
Public Enum GpStatus
Ok = 0
GenericError = 1
InvalidParameter = 2
OutOfMemory = 3
ObjectBusy = 4
InsufficientBuffer = 5
NotImplemented = 6
Win32Error = 7
WrongState = 8
Aborted = 9
FileNotFound = 10
ValueOverflow = 11
AccessDenied = 12
UnknownImageFormat = 13
FontFamilyNotFound = 14
FontStyleNotFound = 15
NotTrueTypeFont = 16
UnsupportedGdiplusVersion = 17
GdiplusNotInitialized = 18
PropertyNotFound = 19
PropertyNotSupported = 20
ProfileNotFound = 21
End Enum
Public Enum BOOL
CFALSE
CTRUE
API_TRUE = 1
API_FALSE = 0
End Enum
Public Declare PtrSafe Function GdiplusStartup Lib "gdiplus" (token As LongPtr, LInput As GdiplusStartupInput, Optional ByVal lOutPut As LongPtr = 0) As GpStatus
Public Declare PtrSafe Function GdiplusShutdown Lib "gdiplus" (ByVal token As LongPtr) As GpStatus
Public Declare PtrSafe Function GdipCreateLineBrushI Lib "gdiplus" (point1 As POINT, point2 As POINT, ByVal color1 As Long, ByVal color2 As Long, ByVal wrapMode As GpWrapMode, lineGradient As LongPtr) As GpStatus
Public Declare PtrSafe Function GdipCreateFromHDC Lib "gdiplus" (ByVal hDC As LongPtr, Graphics As LongPtr) As GpStatus
Public Declare PtrSafe Function GdipFillRectangleI Lib "gdiplus" (ByVal Graphics As LongPtr, ByVal Brush As LongPtr, ByVal X As Long, ByVal Y As Long, ByVal Width As Long, ByVal Height As Long) As GpStatus
Public Declare PtrSafe Function GdipDeleteBrush Lib "gdiplus" (ByVal Brush As LongPtr) As GpStatus
Public Declare PtrSafe Function GdipDeleteGraphics Lib "gdiplus" (ByVal Graphics As LongPtr) As GpStatus
End Module
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment