I finally figured it out.
I will post it so other people can see it.
What I was trying to do is fade in a form using the opacity property,
and Fade out a form using the opacity property.
I had a timer that increased or decreased the opacity property.
When the form was being shown and the opacity property was increased it
was very smooth.
But when I began to fade out the form, lowering the opacity property
initially from 1, I got real bad flicker.
This only occurs if the form has
a lot of controls on it. After the opacity property had been lowered
from the initial 1, and then lowered after that, the fading went smoothly.
Here is how to fix this.
I am not exactly sure why this works as I am new to VB.NET and
these API calls.
Since I was fading in the form, I put the
SetLayeredWindowAttributes(Me.Handle, 0, 255, LWA_ALPHA)
not in the load event, but in the code in the timer that increased
the opacity. I executed the SetLayeredWindowAttributes line above
after the opacity had reached 1.
Note, I also increased the third parameter to 255.
I did this because the form was coming up still transparent
even after the opacity of the form had reached 1.
Executing the above SetLayeredWindowAttributes caused the form
to be completely opaque.
Here is the code:
Public Class Form1
Private Declare Function SetLayeredWindowAttributes Lib "user32" (ByVal hWnd As IntPtr, ByVal crKey As Integer, ByVal
bAlpha As Byte, ByVal dwFlags As Integer) As Integer
Private Const WS_EX_LAYERED As Integer = &H80000
Private Const LWA_ALPHA As Integer = &H2
Protected Overrides ReadOnly Property CreateParams() As System.Windows.Forms.CreateParams
Get
Dim cp As CreateParams = MyBase.CreateParams
cp.ExStyle = cp.ExStyle Or WS_EX_LAYERED
Return cp
End Get
End Property
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' 0 = Completely Transparent
' 255 = Completely Opaque
SetLayeredWindowAttributes(Me.Handle, 0, 128, LWA_ALPHA)
End Sub
End Class