However, there are strategies you can use to minimize flickering in your Windows applications.
Do Not Overlap ControlsThe easiest thing you can do to prevent flickering is to not overlap any controls. Overlapped controls result in more requests to redraw the controls which results in flickering.
Use a CanvasFor best results, display your graphics using the Paint event of a Canvas control. Stay away from using the Window Paint event, the Canvas.Backdrop property or the ImageWell control. Although those techniques work fine in certain situations, they often lead to flickering in more complex window layouts.
On the Canvas, the first thing you want to do is enable the DoubleBuffer property and disable the EraseBackground property. The DoubleBuffer property allows the Canvas to do its updates offscreen to minimize flicker. The EraseBackground property prevents the Canvas from being erased (and showing as a white rectangle) before it is redrawn, which is a common source of flicker. Now you can do all your drawing in the Paint event using the supplied graphics object, g.
Note: Do not do any drawing directly to the Canvas.Graphics property. This will likely increase flickering and will definitely slow down graphics updates.
You can have separate methods that update the graphics, but they need to be called from the Paint event with the graphics object supplied to the methods as a parameter.
When you want to update the graphics in the Canvas, you call the Invalidate method:
You can also call the Refresh method:
The difference is that Invalidate tells the Canvas to update itself when it gets a redraw request from the operating system. The Refresh method tells the Canvas to update itself immediately. Generally, Invalidate is more efficient and should be your first choice.
Both of the above commands do not include the EraseBackground parameter, so it defaults to True. This means that the Canvas is erased before the contents are redrawn, which can result in flicker. Instead, you should draw the background yourself and disable EraseBackground by passing False instead.
Some Tricks to Make this EasierIn general, you do not want to use EraseBackground at all. Anywhere that it is specified or used, you should always set it to False (this incudes Canvas, canvas methods and ContainerControl)
One trick you can use to make this easier is to override the Refresh and Invalidate methods to ignore the EraseBackground flag. For example:
Sub Invalidate(EraseBackground As Boolean = True)
As mentioned above, you also do not want to access Graphics directly. In order to catch situations where you might do this accidentally, you can add a Graphics method that returns Nil. This way if you mistakenly call Canvas.Graphics, you will get a NilObjectException:
Function Graphics() As Graphics
If you are creating a cross-platform application, Canvas.DoubleBuffer is not needed on OS X or Linux. For maximum performance, you should disable it on those platforms using conditional compilation:
#If TargetWin32 Then
Me.DoubleBuffer = True
Me.DoubleBuffer = False
I hope these tips help you to create great-looking Windows applications in Real Studio.