Imagine you want to animate some object on a WinForm. You setup a timer to update the state or model, and override the paint event of the Form. But from there, what’s the best way to continually repaint the Form for the animation?
- Invalidate the Form as soon as you are done drawing?
- Setup a second timer and invalidate the form on a regular interval?
- Perhaps there is a common pattern for this thing?
- Are there any useful .NET classes to help out?
Each time I need to do this I discover a new method with a new drawback. What are the experiences and recommendations from the SO community?
In some situations, it’s faster and more convenient to not draw using the paint event, but getting the Graphics object from the control/form and painting ‘on’ that. This may give some troubles with opacity/anti aliasing/text etc, but could be worth the trouble in terms of not having to repaint the whole shabang. Something along the lines of:
I use this in some Controls myself, and have buffered images to ‘clear’ the background with, when the object of interest moves or need to be removed.