I am working on a custom control and I ran into a problem when TextRenderer is a bit surprising. In my OnPaint event, I apply a transform to a Graphics object to compensate for the scroll position as follows:
e.Graphics.Transform = new System.Drawing.Drawing2D.Matrix(1, 0, 0, 1, this.AutoScrollPosition.X, this.AutoScrollPosition.Y);
Then I pass the graphic to all the sub-elements of the control so that they draw on it. One of these elements is to draw a text string on the graphics surface. And here I have a problem. This line works correctly when scrolling:
e.Graphics.DrawString(this.Text, this.Font, brush, new PointF(this.Rectangle.X, this.Rectangle.Y));
But when I use TextRenderer, I get a completely different result. Here is the text line that should draw the text:
TextRenderer.DrawText(e.Graphics, this.Text, this.Font, this.Rectangle, this.TextColor, TextFormatFlags.PreserveGraphicsClipping | TextFormatFlags.PreserveGraphicsTranslateTransform);
I thought that these two lines should give the same result. But for some reason, the second one uses the graphic transform in different ways, and as a result, when I look at the control, all text lines move at different speeds than the rest of the elements on the drawing surface. Can someone explain to me why this is happening?
c # gdi +
LEO May 24 '11 at 7:49 2011-05-24 07:49
source share