Question about PaintEventArgs.ClipRectangle

  • Thread starter Thread starter Terry
  • Start date Start date
T

Terry

When overriding OnPaint, is it the responsibility of the programmer to not
attempt to draw outside of the ClipRectangle supplied by the PaintEventArgs
parameter? Or does the framework automatically take care of clipping
anything that is drawn outside of that?

Thanks!
 
In my experience the cliprectangle is the area to be updated and is provided
as a performance enhancement. You may simply find it better to blt over the
whole control, it depends on how you are painting the control. If you are
drawing from your own backbuffer then use the rectangle, otherwise you may
have to redraw all your code again which may lead to performance problems.
You could apply a region to the graphics object based on the ClipRectangle
and then you'll be sure that the updates are contained to that rectangle
only.
 
I'm trying to override the default selected item painting in a ListView in
Details mode. So, I'm overriding OnPaint and then checking to see which
items are selected and *trying* to draw my own selection border. When I
click around on the three items in my test list control, and examine the
clipping rect that's passed in and compare with the bounds of the item I
just selected, they usually overlap except for the very first item.

So, what I see is that I usually get a paint message with a clipping rect
that includes the previous item *and* the new item. But, when I click on
the first item in the list, the clipping rect does not include the first
item in the list for some reason. (??)

Below is some tracing code from the paint handler. The number in the square
brackets is the index of the item in the list that is marked as selected
when the OnPaint is received. The "Drawing border..." message will only be
generated if there are selected items and specifies the bounds of the
selected item. What confuses me the most is why the first item is excluded
from the clipping region. It seems to me, that if I just clicked on it that
it would need to be repainted. Is this a bug in the .Net framework?
OnPaint received. ClipRect = {X=0,Y=19,Width=120,Height=28}
Drawing border rect:[1] {X=0,Y=33,Width=120,Height=14}

OnPaint received. ClipRect = {X=0,Y=33,Width=120,Height=14}
Drawing border rect:[0] {X=0,Y=19,Width=120,Height=14}

OnPaint received. ClipRect = {X=0,Y=19,Width=120,Height=28}
Drawing border rect:[1] {X=0,Y=33,Width=120,Height=14}

OnPaint received. ClipRect = {X=0,Y=33,Width=120,Height=28}
Drawing border rect:[2] {X=0,Y=47,Width=120,Height=14}

OnPaint received. ClipRect = {X=0,Y=47,Width=120,Height=14}
Drawing border rect:[0] {X=0,Y=19,Width=120,Height=14}

Terry
 
Back
Top