How to stop excess flickering implementing gradient shading on a form?
Posted on 2011-09-11
I have implemented gradient shading on the forms in my program using a method I have found on a number of sites:
Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
If My.Settings.Gradient_Shading = False Then Exit Sub
Dim formGraphics As Graphics = e.Graphics
Dim gradientBrush As New LinearGradientBrush(New Point(0, 0), New Point(0, Height), My.Settings.GradientColor_1, My.Settings.GradientColor_2)
This worked, but I found that in order to have the same shading on pages within a tab control on that form, I had to implement the following code:
Private Sub tbpOptimizer_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) _
Dim rect As New System.Drawing.Rectangle(0, 0, sender.Width, sender.Height)
Dim g As Graphics = e.Graphics
Dim gradientBrush As New LinearGradientBrush(New Point(0, 0), New Point(0, sender.Height), My.Settings.GradientColor_1, My.Settings.GradientColor_2)
This does work, but at times, the form flickers for a few seconds before the painting is done. In trying to figure out what is happening, I found that the Paint event for the tab above fired about 80 times when that tab was selected! I assume that is the cause of the flickering, because other tabs on the same Tab Control have their Paint event fired fewer times, and they flicker less. But why would a tab page have to fire so many times? And, more importantly, is there a better way for me to implement gradient shading onto my program screens?
Thank you in advance.