Fading in a windows form

sc247

Member
Joined
Jan 10, 2008
Messages
8
Programming Experience
Beginner
Hello,

Could someone tell me how I could fade in a windows form and then it fades out again when the user clicks on "close" button?

Thank you :)
 
Start with setting Opacity property for form to 0 in designer.
VB.NET:
Private Sub frmDiv_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown
    For i As Integer = 1 To 100
        Me.Opacity = i / 100
        Threading.Thread.Sleep(10)
        Application.DoEvents()
    Next
End Sub
Do the opposite in FormClosing event. (For 100 To 1 Step -1)
 
Start with setting Opacity property for form to 0 in designer.
VB.NET:
Private Sub frmDiv_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown
    For i As Integer = 1 To 100
        Me.Opacity = i / 100
        Threading.Thread.Sleep(10)
        Application.DoEvents()
    Next
End Sub
Do the opposite in FormClosing event. (For 100 To 1 Step -1)

Actually, the Windows API has the ability to do this. JMcIlhinney gave me some code ages ago.. Lets see can i dig it out
 
Here it is.. and here is the code to use it:

VB.NET:
FormAnimation.FormAnimator fa = New FormAnimation.FormAnimator(YOUR_FORM_OBJECT, FormAnimation.FormAnimator.AnimationMethod.Blend, 250)

Thats all you need to do to attach the animator to your form. Now every time the form is shown or hidden, it will display the chosen animation (Blend, 250 millis.. if you want other effects, change the options!)

Credits to jmc
 

Attachments

  • FormAnimation.zip
    13.1 KB · Views: 32
the Windows API has the ability to do this
There are different API functions that can be used to make same effect, but you may be thinking of AnimateWindow Function ? This also has other effects.

FormAnimator is a wrapper class for this? I can't find anything about it on internet...
edit: ok, found it at VBForums.
 
Last edited:
Back
Top