keco
keco

Reputation: 146

Control.Invalidate() or Control.Invalidate(Rectangle)

Should I use Control.Invalidate() or preferably Control.Invalidate(Rectangle)? MSDN is a bit short on information for those methods.

In both cases the same OnPaint method is called and the same lines of code are executed. So what is the difference here, except that I have to calculate the rectangle structure to tell windows which area should be redrawn?

I feel like I'm much safer just using Control.Invalidate() all the time, but at the same time I feel like I wrote bad code and lost performance.

Upvotes: 0

Views: 451

Answers (1)

jmcilhinney
jmcilhinney

Reputation: 54427

You should always invalidate the smallest area possible, so you should pass a Rectangle or Region unless the entire control area has or may have changed. The reason for that is that it is the actual painting of pixels to the scree that is the slowest part of the whole operation and every pixel within the invalidated area will be repainted. The fewer pixels that is, the faster the repainting will be.

You may not always be able to see the difference but, especially if you're repainting often, there can be a visible performance difference. It is generally much quicker to perform a mildly complex calculation to determine what the smallest Rectangle or Region would be than to repaint the extra pixels.

Upvotes: 2

Related Questions