What is the difference between PaintEventArgs.ClipRectangle and PaintEventArgs.Graphics.ClipBounds

China☆狼群 提交于 2021-02-18 21:06:49

问题


When overriding an OnPaint or OnPaintBackground method in an inheritor of System.Windows.Forms.Control, one argument is always a System.Windows.Forms.PaintEventArgs object. The partial object structure:

  • PaintEventArgs
    • ClipRectangle (System.Drawing.Rectangle)
    • Graphics (System.Drawing.Graphics)
      • Clip (System.Drawing.Region)
      • ClipBounds (System.Drawing.RectangleF)
      • VisibleClipBounds (System.Drawing.RectangleF)

Graphics.Clip appears to be a 1-bit map of applicable pixels to influence in subsequent paint operations.

MSDN: "Gets or sets a Region that limits the drawing region of this Graphics."

Graphics.ClipBounds is a read only rectangle that appears to be the minimum rectangle to fully contain the Clip region.

MSDN: "Gets a RectangleF structure that bounds the clipping region of this Graphics."

Graphics.VisibleClipBounds seems to be a more 'intelligent' clip, though I cannot understand how it operates.

MSDN: "Gets the bounding rectangle of the visible clipping region of this Graphics."

ClipRectangle appears to in all cases duplicate the Graphics.ClipBounds property.

MSDN: "Gets the rectangle in which to paint."

Can someone please answer the main question and potentially shed light on what all the various boundary objects are for?

Edit: added MSDN's descriptions as per Dan-o's answer.


回答1:


PaintEventArgs.ClipRectangle is the rectangle that actually needs to be drawn by your Paint event. Commonly equal to the size of the control's client area. It will be smaller when only a portion of it was overlapped by another window. Or when you call its Invalidate(Rectangle) method. You can use it to skip drawing expensive objects that fall outside of that rectangle. Which is pretty rare, Windows itself already does a very good job of clipping what you draw.

Graphics.Clip is a region that you can assign in your drawing code to clip what you draw yourself. It allows for various kinds of effects, like drawing an image clipped by a circle. Or really intricate clipping effects that use a GraphicsPath converted to a region.

Graphics.ClipBounds is the rectangle around Graphics.Clip, it makes the math faster to get a first order estimate whether or not a point is outside of Graphics.Clip. You'll get a meaningless "infinite" clip bounds if you never assigned the Clip property (X=-4194304,Y=-4194304,Width=8388608,Height=8388608)

Graphics.VisibleClipBounds is the same as Graphics.ClipBounds but clipped also by the control window. So useful to test if a point might be inside the Clip region and visible to the user. If you never assign the Clip property then you'll just get the size of the window.

So it is Windows that sets PaintEventArgs.ClipRectangle. The rest is set by you by assigning the Graphics.Clip property.



来源:https://stackoverflow.com/questions/12062019/what-is-the-difference-between-painteventargs-cliprectangle-and-painteventargs-g

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!