c# - 如何将PointF传递给DrawString?

时间:2014-07-03 03:31:01

标签: c#

c# - 如何将PointF传递给DrawString?

我尝试将PointF传递给DrawString,但它没有提供给我。这是代码,但是我不知道怎么办?

string txt = "A long line we're trying to fit inside a rectangle with custom line spacing";

          Font font = new Font("Microsoft Sans Serif", 10, FontStyle.Bold);
          SizeF fit = new SizeF(200, font.Height);
          StringFormat fmt = StringFormat.GenericTypographic;
          PointF pointf = new PointF(55,21);

          //Rectangle ss =  new Rectangle(new PointF(155.0F, 225.0F),txt);

          int spacing = (int)(1.5 * font.Height);
          int line = 0;

          for (int ix = 0; ix < txt.Length; ) {
            int chars, lines;

            e.Graphics.MeasureString(txt.Substring(ix), font, fit, fmt, out chars, out lines);
            e.Graphics.DrawString(txt.Substring(ix, chars), font, Brushes.Black, pointf, spacing * line);
            ++line;
            ix += chars;

          }

请按代码告诉我!

1 个答案:

答案 0 :(得分:1)

我认为你想要的是:

e.Graphics.DrawString(txt.Substring(ix, chars), font, Brushes.Black, pointf);
++line;
ix += chars;
// Set up for the next line.
pointf.Y = pointf.Y + (lines*font.Height) + spacing;

请注意,我删除了DrawString来电的最后一个参数,并添加了更新pointf.Y的代码。