rtrussell
Dec 14, 2013
- "Added comment by Richard Russell"
=How To Accurately Position Text <span style="font-size: 13px; font-weight: normal; line-height: 19px;">[[user:lbjoseph]]</span>=
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.
[[code format="lb"]]
WindowWidth = 300
WindowHeight = 350
NoMainWin
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
Wait
[quit]
close #g
End
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
[[code]]
//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!