10
votes

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
Dan-o perhaps I'm missing something, but to me they sound like rehashings of the verbs in the property names themselves. I know how to use MSDN (Both links were already visited) and can read IntelliSense help (these exact phrases), but surely the question makes evident that more information is required. If there were one region and one rectangle things would make sense but there are no less than four potentially different areas. I would expect to see a paragraph on each to make this situation properly understood.J Collins

1 Answers

15
votes

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.