June 14, 2024, 02:37:03 AM


IonicWind Snippit Manager 2.xx Released!  Install it on a memory stick and take it with you!  With or without IWBasic!

Centering Text

Started by Bruce Peaslee, January 22, 2006, 05:26:20 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Bruce Peaslee

Here's a routine that prints text in a window, centered between two x-coordinates:

myWindow::WriteCenteredText(int x1, int x2, int y, string s),void
ÂÃ,  // x1 is the leftmost limit, x2 the rightmost; y the distance from top
ÂÃ,  int offset;
ÂÃ,  point myPoint;

ÂÃ,  myPoint = GetTextSize(s);ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  // width of string
ÂÃ,  offset = (x2 - x1 - myPoint.x) / 2;ÂÃ,  // offset from x1
ÂÃ,  WriteText(x1 + offset, y, s);
ÂÃ,  return;

You can use GetClientRect() to find x1 and x2 for the window itself. I use this routine to center text under an image and to redraw text centered in a window when the window is resized.
Bruce Peaslee
"Born too loose."
iTired (There's a nap for that.)
Well, I headed for Las Vegas
Only made it out to Needles