PaintEventArgs 中 Graphics 对象的绘图表面是相对于 ClipRectangle 的吗?
我正在覆盖我的控件中的 OnPaint
。有时,我的控件会因剪切矩形 (e.ClipRectangle
) 而失效,该矩形小于控件的大小。
在我的 OnPaint 方法中,当我绘制到 GDI+ 表面时(Graphics
来自 PaintEventArgs
),我将相对于 PaintEventArgs
开始。 Painteventargs.cliprectangle.aspx" rel="nofollow">ClipRectangle
,还是控件?
例如,假设我的控件是 800 x 600 像素。如果我调用:
Invalidate(new Rectangle(42, 0, 100, 600));
然后在 OnPaint 中绘制一个字符串:
e.Graphics.DrawString("Don't panic.", new Font("Arial", 12), new Brush(Color.Red), 0, 0);
该字符串会显示在左侧的控件上,还是距左侧 42 个像素处?
I'm overriding OnPaint
in my control. At times, my control gets invalidated with a clipping rectangle (e.ClipRectangle
), which is smaller than the size of the control.
In my OnPaint
method, when I draw to the GDI+ surface (the Graphics
object from PaintEventArgs
), will I start relative to the ClipRectangle
, or to the control?
For example, let's say my control is 800 x 600 pixels. If I call:
Invalidate(new Rectangle(42, 0, 100, 600));
And then in OnPaint
I draw a string:
e.Graphics.DrawString("Don't panic.", new Font("Arial", 12), new Brush(Color.Red), 0, 0);
Will the string show up on my control on the left side, or 42 pixels in from the left side?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
绘图必须始终与控件的客户矩形相关。这是一个常见的错误,使其与 e.ClipRectangle 相关,在 Aero 上也工作了相当长一段时间,因为当您拖动另一个窗口穿过它时,它不再使窗口失效。不过,当 Aero 关闭时,会产生非常有趣的图形效果,看起来就像回声的图形效果。
您应该只使用 e.ClipRectangle 来优化您的绘图代码,当您绘制的部分完全超出剪切区域时,跳过该部分。图形在自动剪辑方面已经做得非常好,要让这样的剪辑测试获得回报并不那么容易。当并非所有内容都被剪辑时,就非常困难,永远不要对其进行优化。启用 Aero 就没有什么意义了。
Drawing must always be relative from the control's client rectangle. It is a common bug to make it relative from e.ClipRectangle, works for quite a while too on Aero since it doesn't invalidate a window anymore when you drag another window across it. Produces very interesting graphics effects when Aero is turned off though, looks like the graphics equivalent of an echo.
You should only ever use e.ClipRectangle to optimize your drawing code, skipping parts of what you draw when it is completely outside of the clipping area. Graphics already does a very good job of automatic clipping, making such a clip test pay off isn't that easy. And very hard when not everything is clipped, never optimize that. There's little point left with Aero enabled.