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?
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.