04-09-2023, 12:25 AM
(04-08-2023, 11:44 PM)bplus Wrote: This looks much sharper!
And less code!Code: (Select All)_Title "Centering text inside a box" ' 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", &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", &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$, boxColor~&, PrintColor~&)
' note: "X" was the original Box Character used
pwMessage = _PrintWidth(message$) ' print width of message$
pwX = _PrintWidth("X") ' print width of 1 X
addxs = pwMessage + 4 * pwX ' width of X box
fh = _FontHeight
y = (_Height - 5 * fh) / 2 ' y start x's top corner of box
x = (_Width - addxs) / 2 ' x start x's left side of box
Line (x, y)-Step(addxs, 5 * fh), boxColor~&, BF
Color PrintColor~&
Line (x + 2, y + 2)-Step(addxs - 4, 5 * fh - 4), , B
Line (x + 4, y + 4)-Step(addxs - 8, 5 * fh - 8), , B
_PrintString (x + 2 * pwX, y + 2 * fh), message$ ' for perfect center, add .5*difference
End Sub
Thanks bplus. You've hit the nail on the head! That's the exact function I needed!
Now, I just have to digest it (yes, mixed metaphore: iron in my diet!), and adjust it for my prog. Thanks again.
Of all the places on Earth, and all the planets in the Universe, I'd rather live here (Perth, W.A.)