.net DrawString / StringFormat Error

I am developing a .net 3.5 Win Forms program, and I ran into an “interesting” problem with text drawing.

I implement a text editing control and draws text using DrawString () and StringFormat.GenericTypographic (hereinafter referred to as GT). As the user types, entire words (or groups of words) earlier in the line move subtly left and right in random order.

Most likely, these are rounding errors - the affected block is trying to shift one pixel in the horizontal plane, as the letters are added to the end of the line. If I use StringFormat.GenericDefault (GD), this will not happen, but the characters are displayed less accurately, and this is unacceptable.

I reasoned that I can slowly change GD to GT (settings for each of them are discussed in MSDN and, obviously, can be considered in the debugger) and see which FormatFlags or other settings caused this from there. However, if I take a copy of GD, change all the properties so that it is identical to GT, the character placement is completely different - the behavior of these two supposedly identical objects is clearly not the same.

Like too many IMHO things, .net Reflector shows that StringFormat is just a wrapper for an object without control, and I can only assume that not all of its properties are exposed to .net software.

Can anyone suggest anything that can help me? I know that TextRenderer can provide an alternative method for rendering, but I abandoned this earlier in my development process (although I can’t remember what the problem was now ...).

Edit

The code I use for rendering is as follows:

sForm = new StringFormat(StringFormat.GenericTypographic);
sForm.FormatFlags |= StringFormatFlags.MeasureTrailingSpaces;

using (SolidBrush brush = new SolidBrush(frmt.ForegroundColour))
  context.DrawString(line.Text, frmt.DisplayFont, brush, new PointF(horizontal, height), sForm);
+5
source share
1 answer

Ok, I solved the problem. This is not the way I wanted to do this, but for reference, I did it like this:

, ( ), , , .

DrawString() ( ), .

, , , , ...

0

All Articles