I have been searching for a way to reduce the irritating flicker on my controls when opening a form for a while and the only solution I have found that works is using this Extended Window Style WS_EX_COMPSITED
from the below documentation:
https://learn.microsoft.com/en-us/windows/win32/winmsg/extended-window-styles
Protected Overrides ReadOnly Property CreateParams() As CreateParams
Get
Dim cp As CreateParams = MyBase.CreateParams
cp.ExStyle = cp.ExStyle Or &H2000000
Return cp
End Get
End Property
Adding this onto the class stops the controls flickering perfectly (albeit with a slight reduction in performance, but it's negligible in my case) - most likely because of the double-buffered form.
My problem is that when this Property is applied to the class, Graphics
and Drawing
seem to stop working in any capacity in the Class. I have tried the below code with the property added and removed, and it only works when it's removed:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim myPen As Pen
Dim myBrush As Brush
myPen = New Pen(Drawing.Color.FromArgb(53, 64, 82), 1)
myBrush = New SolidBrush(Color.FromArgb(180, 204, 112))
Dim myGraphics As Graphics = Me.Panel1.CreateGraphics
myGraphics.DrawEllipse(myPen, 28, 28, 12, 12)
myGraphics.FillEllipse(myBrush, 28, 28, 12, 12)
End Sub
I assume this has something to do with the way the WS_EX_COMPOSITED
style works in terms of not refreshing/repainting correctly etc, but Me.Refresh()
or Me.Panel1.Refresh()
doesn't seem to repaint it in anyway.
Is there a workaround for this that I can use? Is this a bug or is this a limitation of this particular Extended Window Style?