The MeasureTrailingSpaces format flag is used by MeasureString.
The poor fit of text in a rectangle is due to the inaccurate way GDI+
renders text to speed up output. If you slow things down you can get a
pretty good rendering.
Here is an altered version that renders as best GDI+ can...
private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
g.TextRenderingHint = TextRenderingHint.AntiAlias;
StringFormatFlags flags = StringFormatFlags.FitBlackBox;
Rectangle r = new Rectangle(10, 10, 100, 100);
StringFormat sf = (StringFormat)StringFormat.GenericTypographic.Clone();
sf.FormatFlags |= flags;
sf.Alignment = StringAlignment.Far;
g.DrawString("This is\na string\non more lines",
this.Font, Brushes.Blue, r, sf);
g.DrawRectangle(Pens.Red, r);
}
Note the use of the GenericTypographic format and the text rendering hint
set to it's most accurate setting.
--
Bob Powell [MVP]
Visual C#, System.Drawing
Ramuseco Limited .NET consulting
http://www.ramuseco.com Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
[quoted text, click to view] "Fabio" <znt.fabio@virgilio.it> wrote in message
news:%23Vpv44BzGHA.1252@TK2MSFTNGP04.phx.gbl...
> Hi all!
>
> I have a problem with the DrawString method.
>
> I wrote this little code that reproduce the problem:
>
> private void Form1_Paint(object sender, PaintEventArgs e)
>
> {
>
> Graphics g = e.Graphics;
>
> StringFormatFlags flags = StringFormatFlags.MeasureTrailingSpaces;
>
> Rectangle r = new Rectangle(10, 10, 100, 100);
>
> StringFormat sf = new StringFormat(flags);
>
> sf.Alignment = StringAlignment.Far;
>
> g.DrawString("This is\na string\non more lines",
>
> this.Font, Brushes.Blue, r, sf);
>
> g.DrawRectangle(Pens.Red, r);
>
> }
>
>
> The problem is: why if I set the MeasureTrealingSpaces flag the text is
> not aligned correctly to the right?
> The text in this sample does not have trailing spaces!
> I know I can remove that flag, but if a single line has trailing spaces
> (note that the last line is drawn correctly!) I want that them are drawn.
>
> Thanks for the help :)
>
> --
>
> Free .Net Reporting Tool -
http://www.neodatatype.net >