Re: Graphics.DrawString can't draw last space chars?
It's probably just measuring what's actually rendered and spaces aren't rendered. You might get the result you want with a different overload of MeasureString or maybe MeasureCharacterRanges instead.
Re: Graphics.DrawString can't draw last space chars?
Originally Posted by jmcilhinney
It's probably just measuring what's actually rendered and spaces aren't rendered. You might get the result you want with a different overload of MeasureString or maybe MeasureCharacterRanges instead.
Graphics.DrawString just omit last all of Space chars. MS intend to do so has any purpose? How to solve the problem? I need to draw Space chars because SPACE is printable ASCII.
Code:
protected override void OnPaint(PaintEventArgs e)
{
e.Graphics.DrawString("Using Graphics.DrawString:", Font, new SolidBrush(SystemColors.ControlText), 20, 70);
e.Graphics.DrawString("Hello World ", Font, new SolidBrush(SystemColors.ControlText), 20, 100);
SizeF dpipSizeF = e.Graphics.MeasureString("Hello World ", Font);
e.Graphics.DrawRectangle(new Pen(Color.Lime), 20, 100, dpipSizeF.Width, dpipSizeF.Height);
TextRenderer.DrawText(e.Graphics, "Using TextRenderer.DrawText:", Font, new Point(20, 170), SystemColors.ControlText);
TextRenderer.DrawText(e.Graphics,"Hello World ", Font, new Point(20, 200), SystemColors.ControlText);
dpipSizeF = TextRenderer.MeasureText("Hello World ", Font);
e.Graphics.DrawRectangle(new Pen(Color.Lime), 20, 200, dpipSizeF.Width, dpipSizeF.Height);
base.OnPaint(e);
}
Last edited by DaveDavis; Dec 2nd, 2022 at 03:03 AM.
Re: Graphics.DrawString can't draw last space chars?
I doubt that it's about DrawString but rather MeasureString. When you're using DrawText you are also using MeasureText, so that's different. Have you tried to see what the other overloads of MeasureString do and whether MeasureCharacterRanges behaves any differently?
Re: Graphics.DrawString can't draw last space chars?
Originally Posted by jmcilhinney
I doubt that it's about DrawString but rather MeasureString. When you're using DrawText you are also using MeasureText, so that's different. Have you tried to see what the other overloads of MeasureString do and whether MeasureCharacterRanges behaves any differently?
For verification, I measure "Hello World" width, then I Print "Hello World" only, then I print "2nd string " with 20 SPACE to override, but not cover the "Hello World", means DrawString omit all of SPACE chars.
Code:
For verification, I measure "Hello World" width, then I Print "Hello World" only, then I print "2nd string " with 20 SPACE to override, but not cover the "Hello World", means DrawString omit all of SPACE chars.
Re: Graphics.DrawString can't draw last space chars?
I see no evidence of any attempt to test other overloads of MeasureString or MeasureCharacterRanges so I appear to be wasting my time here. I don't know whether they would work or not but you definitely won't find out of you don't try. I'm out.
Re: Graphics.DrawString can't draw last space chars?
Originally Posted by jmcilhinney
I see no evidence of any attempt to test other overloads of MeasureString or MeasureCharacterRanges so I appear to be wasting my time here. I don't know whether they would work or not but you definitely won't find out of you don't try. I'm out.
You try the code in my last post, I didn't use any MeasureString.
Code:
protected override void OnPaint(PaintEventArgs e)
{
Rectangle rct = new Rectangle(20, 100, 200, 20);
e.Graphics.DrawRectangle(new Pen(Color.Lime), rct);
e.Graphics.DrawString("Hello World ", Font, new SolidBrush(SystemColors.ControlText), rct, new StringFormat() { Alignment = StringAlignment.Far});
base.OnPaint(e);
}
Re: [RESOLVED] Graphics.DrawString can't draw last space chars?
MeasureTrailingSpaces
Includes the trailing space at the end of each line. By default the boundary rectangle returned by the MeasureString method excludes the space at the end of each line. Set this flag to include that space in measurement.
I am confused by MS doc grammar in English. If the doc said "... the trailing SPACE chars at the end of ..." then I could understand better.