Centering text inside char bax
#5
(04-08-2023, 11:44 PM)bplus Wrote: This looks much sharper!
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
And less code! Smile

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.) Big Grin
Reply


Messages In This Thread
Centering text inside char bax - by bplus - 04-08-2023, 05:05 PM
RE: Centering text inside char bax - by mnrvovrfc - 04-08-2023, 06:05 PM
RE: Centering text inside char bax - by bplus - 04-08-2023, 11:44 PM
RE: Centering text inside char bax - by PhilOfPerth - 04-09-2023, 12:25 AM
RE: Centering text inside char bax - by bplus - 04-09-2023, 12:25 AM
RE: Centering text inside char bax - by mnrvovrfc - 04-10-2023, 12:27 PM
RE: Centering text inside char bax - by bplus - 04-10-2023, 03:01 PM
RE: Centering text inside char bax - by bplus - 04-10-2023, 03:10 PM
RE: Centering text inside char bax - by SMcNeill - 04-11-2023, 05:38 PM
RE: Centering text inside char bax - by OldMoses - 04-11-2023, 05:22 PM
RE: Centering text inside char bax - by mnrvovrfc - 04-11-2023, 07:10 PM
RE: Centering text inside char bax - by SMcNeill - 04-11-2023, 11:31 PM
RE: Centering text inside char bax - by OldMoses - 04-12-2023, 12:44 AM



Users browsing this thread: 1 Guest(s)