How To Accurately Position Text - lbjoseph lbjoseph

You might have noticed that Liberty BASIC doesn't do a very a good job positioning text where you specify in a graphics box. The following sample demonstrates a few simple API calls necessary to fix this behavior.

WindowWidth = 300
WindowHeight = 350
Open "Font Problems" For Graphics_NF_NSB As #g
#g "Down; Fill Blue; Flush"
#g "Color Black; Backcolor White"
#g "font arial 100"
dc = GetDC(hwnd(#g))
CallDLL #gdi32, "SetTextAlign", dc As uLong, 0 As uLong, Result As uLong
#g "Place 100 100"
#g "|X"
#g "posxy x y"
temp$ = "X"
#g "stringwidth? temp$ width"
fontRoughHeight = y-100
#g "color red; size 10; set ";x;" ";y-fontRoughHeight
#g "color green; size 10; set ";x;" ";y
#g "color yellow; size 10; set ";x+width;" ";y
#g "trapclose [quit]"
Call ReleaseDC hwnd(#g), dc
close #g
Function GetDC(hWnd)
    CallDLL #user32, "GetDC",hWnd As uLong,GetDC As uLong
End Function
Sub ReleaseDC hWnd, hDC
    CallDLL#user32,"ReleaseDC",hWnd As uLong,hDC As uLong,result As Long
End Sub

Comment by Richard Russell:
If you change the text alignment like this it doesn't 'stick'. Add a 'flush' command to the above code and then force the window to redraw by covering and uncovering it, or by minimizing and restoring it: the text will be redrawn in the default alignment!

No, it does not stick. LB does not know about api calls used by the programmer. It redraws the native graphic commands it has received. To cause api graphics to stick, one must do the getbmp-drawbmp-flush procedure that we have recommended for 15 years. It is used in many example programs and documented in many articles and tutorials. You will find it in the help file topic flushing sprite graphics as well. - Alyce Alyce