TextRenderer与图形变换 [英] TextRenderer with Graphics transform

查看:176
本文介绍了TextRenderer与图形变换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直工作在一个自定义的控制和我碰到与TextRenderer演戏有点令人惊讶的问题。在我的OnPaint事件我申请转换为图形对象,以弥补这样滚动条的位置:

I've been working on a custom control and I've run into an issue with TextRenderer acting a bit surprisingly. In my OnPaint event I apply transform to the Graphics object to compensate for the scroll position like this:

e.Graphics.Transform = new System.Drawing.Drawing2D.Matrix(1, 0, 0, 1, this.AutoScrollPosition.X, this.AutoScrollPosition.Y);



然后我通过图形对象来控制的所有子元素,让他们画自己到它。这其中一个元素应该引起文本字符串到图形表面。而这正是我有一个问题。这条线似乎正常工作时滚动:

Then I pass the graphic object to all sub elements of the control so that they paint themselves onto it. One of this elements should draw text string onto the graphics surface. And this is where I've got an issue. This line seems to work correctly when scrolling:

 e.Graphics.DrawString(this.Text, this.Font, brush, new PointF(this.Rectangle.X, this.Rectangle.Y));



但是当我使用TextRenderer我得到完全不同的结果。那继承人应该绘制文本的文本行:

But when I use TextRenderer I get a completely different result. Heres the text line that supposed to 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 produce the same result. But for some reason the second one applies the graphics transform differently and as a result, when I scroll the control all the text lines move around with different speed than the rest of the elements on the drawing surface. Could someone explain me why this is happening?

推荐答案

下面是我最好的猜测是:的TextRenderer.DrawText 是GDI基和因此的分辨率有关。 Graphics.DrawString 是GDI +,因此分辨率无关。另请参见这篇文章

Here's my best guess at this: TextRenderer.DrawText is GDI-based and therefore resolution-dependant. Graphics.DrawString is GDI+ and therefore resolution-independant. See also this article.

既然你说的那个文本走动用不同的速度,大概是什么情况是,GDI调用使用不同的默认的第一个比你Graphics对象了。这会意味着你必须调整你的AutoScrollCoordinates尊重你的图形对象之间的分辨率和默认GDI分辨率的区别。

Since you say that the texts "move around with different speed", probably what happens is that the GDI call uses a different "default" resolution than the one your Graphics object has. That'd mean that you'd have to adjust your AutoScrollCoordinates to respect the difference between your Graphics object resolution and the "default" GDI resolution.

这篇关于TextRenderer与图形变换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆