Code: (Select All)
_Title "Centering text inside a box of X's" ' b+ 2023-04-08
Screen _NewImage(800, 600, 32)
f& = _LoadFont("c:\WINDOWS\Fonts\arial.ttf", 24, "bold")
_Font f&
_PrintMode _KeepBackground
MessageBox "Centering text inside a box of X's, ...zzz", "X", &HFF0000FF, &HFFFFFFFF
Sleep
Cls
' OK generalize this for any meessage, character block and font size
f& = _LoadFont("c:\WINDOWS\Fonts\cambriab.ttf", 30, "Monospace")
f& = _LoadFont("c:\WINDOWS\Fonts\cambriab.ttf", 40)
_Font f&
MessageBox "bplus was here, ...zzz", "b", &HFFFF0000, &HFFBBBBFF
Sleep
Cls
f& = _LoadFont("c:\WINDOWS\Fonts\inkfree.ttf", 50)
_Font f&
MessageBox "bplus was here, ...zzz", "+", &HFFFF0000, &HFFBBBBFF
Sleep
' assuming font is loaded and _font handle activated AND _PrintMode _KeepBackground
' assuming message will fit on one line inside screen plus 4 extra Box characters
Sub MessageBox (message$, boxCharacter$, boxColor~&, PrintColor~&)
' note: "X" was the original Box Character used
pwMessage = _PrintWidth(message$) ' print width of message$
pwX = _PrintWidth(boxCharacter$) ' print width of 1 X
addxs = pwMessage + 4 * pwX ' width of X box
' X + space + message + space + X AND spaces same width as X = 4 * pwX
fh = _FontHeight
y = (_Height - 5 * fh) / 2 ' y start x's top corner of box
' how many x's fit in add2x
nx = (addxs + .5 * pwX) \ pwX ' round up to convert message line to a number of X's to print
pwNX = _PrintWidth(String$(nx, boxCharacter$))
x = (_Width - pwNX) / 2 ' x start x's left side of box
diff = pwNX - pwMessage ' what is difference between n printed X's and message line?
Line (x, y)-Step(pwNX, 5 * fh), boxColor~&, BF
Color PrintColor~&
For i = 0 To 4
If i = 0 Or i = 4 Then
_PrintString (x, y + (i * fh)), String$(nx, boxCharacter$)
Else
_PrintString (x, y + (i * fh)), boxCharacter$
_PrintString (x + pwNX - pwX, y + (i * fh)), boxCharacter$
End If
Next
_PrintString (x + .5 * diff, y + 2 * fh), message$ ' for perfect center, add .5*difference
End Sub
b = b + ...