#Include "windows.bi"
#Include "win/gdiplus.bi"
Using GDIPLUS
Dim wc As WNDCLASSEX
Dim  msg As MSG
Dim ULONG_PTR_01 As ULONG_PTR
Dim GDIPLUSSTARTUPINPUT_01 As GDIPLUSSTARTUPINPUT
GDIPLUSSTARTUPINPUT_01.GdiplusVersion = 1
If (GdiplusStartup(@ULONG_PTR_01, @GDIPLUSSTARTUPINPUT_01, NULL) <> 0) Then
	Print "FAIL"
EndIf
Sub OnPaint(compHdc As HDC)
	Dim As PVOID GpGraphics,blackPen,_
	blueBrush,redBrush,myFont,fontFamily,strFormat
	Dim As GPrectF layoutRect_A,layoutRect_B,layoutRect_C
	layoutRect_A.x=20:layoutRect_A.y=20:layoutRect_A.width=130:layoutRect_A.height=130
	layoutRect_B.x=160:layoutRect_B.y=20:layoutRect_B.width=165:layoutRect_B.height=130
	layoutRect_C.x=335:layoutRect_C.y=20:layoutRect_C.width=165:layoutRect_C.height=130
   Dim As CharacterRange charRanges(2)
   CharRanges(0).First=3:CharRanges(0).Length=5
   CharRanges(1).First=15:CharRanges(1).Length=2
   CharRanges(2).First=30:CharRanges(2).Length=15
   Dim As Integer i,count
   Dim As String string_="The quick, brown fox easily jumps over the lazy dog."
	GdipCreateFromHDC(compHdc,@GpGraphics)
	GdipGraphicsClear(GpGraphics,&hffffffff)
	GdipCreateSolidFill(&h64FF0000,@redBrush)
	GdipCreateSolidFill(&hFF0000FF,@blueBrush)
	GdipCreateFontFamilyFromName("Times New Roman",NULL,@fontFamily)
   GdipCreateFont(fontFamily, 16, FontStyleRegular, UnitPoint, @myfont)
   GdipCreateStringFormat(NULL,LANG_NEUTRAL,@strFormat)
	GdipSetStringFormatMeasurableCharacterRanges(strFormat,3,@charRanges(0))
	GdipGetStringFormatMeasurableCharacterRangeCount(strFormat,@count)
   ReDim pCharRangeRegions(count) As PVOID
   If count > 0 Then
   	For i = 0 To count-1
   		Dim t As PVOID
   		GdipCreateRegion(@t)
   		pCharRangeRegions(i)=t
   	Next
   EndIf	
	GdipMeasureCharacterRanges(GpGraphics,WStr(string_),-1,myFont, @layoutRect_A, strFormat, count, @pCharRangeRegions(0))
   GdipDrawString(GpGraphics, WSTR(string_), LEN(string_), myfont, @layoutRect_A, strFormat, bluebrush)
   GdipCreatePen1(&hff000000,1,Unitpixel,@blackpen)
   GdipDrawRectangles(GpGraphics,blackpen, @layoutRect_A,1)
   For i = 0 To count -1 
   	GdipFillRegion(GpGraphics,redBrush, pCharRangeRegions(i))
   Next
	GdipMeasureCharacterRanges(GpGraphics,WStr(string_),-1,myFont, @layoutRect_B, strFormat, count, @pCharRangeRegions(0))
   GdipDrawString(GpGraphics, WSTR(string_), LEN(string_), myfont, @layoutRect_B, strFormat, bluebrush)
   GdipCreatePen1(&hff000000,1,Unitpixel,@blackpen)
   GdipDrawRectangles(GpGraphics,blackpen, @layoutRect_B,1)
   For i = 0 To count -1 
   	GdipFillRegion(GpGraphics,redBrush, pCharRangeRegions(i))
   Next   
   GdipSetStringFormatFlags(strFormat,StringFormatFlagsMeasureTrailingSpaces)
	GdipMeasureCharacterRanges(GpGraphics,WStr(string_),-1,myFont, @layoutRect_C, strFormat, count, @pCharRangeRegions(0))
   GdipDrawString(GpGraphics, WSTR(string_), LEN(string_), myfont, @layoutRect_C, strFormat, bluebrush)
   GdipCreatePen1(&hff000000,1,Unitpixel,@blackpen)
   GdipDrawRectangles(GpGraphics,blackpen, @layoutRect_C,1)
   For i = 0 To count -1 
   	GdipFillRegion(GpGraphics,redBrush, pCharRangeRegions(i))
   Next       
   if count > 0 Then
   	for i = 0 to count-1
   		GdipDeleteRegion(pCharRangeRegions(i))
   	Next
   EndIf
   GdipDeleteStringFormat(strFormat)
	GdipDeleteBrush(blueBrush)
	GdipDeleteBrush(redBrush)
	GdipDeletePen(blackPen)
	GdipDeleteFontFamily(fontFamily)
	GdipDeleteFont(myfont)
	GdipDeleteGraphics(GpGraphics)

End Sub
Function WndProc(hWnd As HWND,uMsg As UINT,wParam As WPARAM,lParam As LPARAM) As Integer
	Dim ps As PAINTSTRUCT
	Static As HDC compHdc,hdc
	Select Case uMsg
		Case WM_CLOSE
			DeleteDC(compHdc)
			PostQuitMessage(0)
		Case WM_CREATE
			hdc=GetDC(GetDesktopWindow)
			compHdc=CreateCompatibleDC(hdc)
			Var bitmap = CreateCompatibleBitmap(hdc,GetSystemMetrics(SM_CXSCREEN),GetSystemMetrics(SM_CYSCREEN))
			SelectObject(compHdc,bitmap)
			DeleteObject(bitmap)
			ReleaseDC(GetDesktopWindow,hdc)
			OnPaint(compHdc)
			InvalidateRect(hwnd,0,0)
		Case WM_PAINT
			BeginPaint(hWnd,@ps)
			BitBlt(ps.hdc,ps.rcPaint.left,ps.rcPaint.top,ps.rcPaint.right-ps.rcPaint.left,ps.rcPaint.bottom-ps.rcPaint.top,compHdc,ps.rcPaint.left,ps.rcPaint.top,SRCCOPY)
			EndPaint(hWnd,@ps)
			Return 0
		Case Else
			Return DefWindowProc(hWnd,uMsg,wParam,lParam)
	End Select
End Function

With wc
	.hInstance=GetModuleHandle(0)
	.cbSize=SizeOf(WNDCLASSEX)
	.style=CS_HREDRAW Or CS_VREDRAW
	.lpfnWndProc=@WndProc
	.lpszClassName=StrPtr("class")
	.hCursor=LoadCursor(NULL,IDC_ARROW)
End With
RegisterClassEx(@wc)
CreateWindowEx(0,wc.lpszClassName,"DrawGDI+",WS_OVERLAPPEDWINDOW Or WS_VISIBLE,200,200,535,205,0,0,wc.hInstance,0)

While GetMessage(@msg,0,0,0)
	TranslateMessage(@msg)
	DispatchMessage(@msg)
Wend