Centering Text when Printing
Marco,
You can use Graphics.MeasureString to check the length of a string
during printing and then adjust the starting location accordingly as in:
SizeF sz = g.MeasureString(s, f, new PointF(x1, y1), m_Fmt);
Where g is the devrived graphics, s the string, f the Font, x1/y1 the
starting location, and m_Fmt is StringFormat.GenericTypographic.
This will return the bounding box for the string and you can retrieve
the Width. This version won't include measurement of trailing spaces and
you should do the DrawString using the same StringFormat. If you use the
default StringFormat by not specifying it the box is widened in both width
and height by IIRC 1/6em. If you need to measure trailing spaces as well
search the framework.windowsforms group for MeasureString as someone has
managed to derive a custom StringFormat that will do this.
Ron Allen
Quote:
> Im trying to center some text in the printout. Assuming that this text is
> inside a string variabe and it can be anything when you go to check out
the
> print preview how could you make sure that its always centered? Ive tried
> couting how many letters there are in the string and then multipying by
the
> font size but that only works if the font uses the exact same size for all
> the characters which most fonts do not. Can anyone help me with this
> problem?