-
Nice fading in and out form effect
Hi,
A while ago I designed a small subroutine that would allow me to fade forms in and out. The effect is very nice and adds a bit of sleekness to your application. There is also a C# version, this can be found in the C# codebank here
Code:
Sub FormFade(ByVal FType)
Select Case FType
Case ("in")
Dim FadeCount As Integer
For FadeCount = 10 To 90 Step 10
Me.Opacity = FadeCount / 100
Me.Refresh()
Threading.Thread.Sleep(50)
Next
Case ("out")
Dim FadeCount As Integer
For FadeCount = 90 To 10 Step -10
Me.Opacity = FadeCount / 100
Me.Refresh()
Threading.Thread.Sleep(50)
Next
End Select
Me.Opacity = 99
End Sub
This subroutine can be called like this:
FormFade("in") for fading your form in
FormFade("out") for fading your form out
The form fade in can be implemented on your Form Load event handler like so:
Code:
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
FormFade("in")
End Sub
The form fade out can be implemented on your Form Closing event handler as it will catch all attempts to close, not just ones you might add to your Command Button. This can be implemented like this:
Code:
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
FormFade("out")
End Sub
Additional Information
This should work with all versions of .net
As you have seen I have set Me.Opacity = 99. This is for two reasons. One being that the fade in effect does not fully reach the 100/99% so the form does not look solid. The second is that if fading out with 100% opacity the form will glitch slightly. You will notice no difference between 99% and 100% opacity though.
I'd love to know what you think of this, yes it's not original, but for those looking for something like this, I think it is a good little script.
-
Re: Nice fading in and out form effect
To be more specific the glitch happens between 0.96 and 100.0, so to avoid the form flicker thing, have it fade in from 0.0 to 0.95, then from 0.95 back to 0.0 when fading out. Also, when avoiding the form flicker and setting the opacity to 0.95 the form is semi-transparent still.
I'm working on a game that fades in when ran and fades out when closed, except I used an enumeration for the direction (In/Out) and a timer for a smoother effect.
Edit: This flicker thing doesn't happen in .Net 1.1
-
Re: Nice fading in and out form effect
Here's how I do fading in and out using the form's opacity property and a Timer:
Code:
Option Explicit On
Option Strict On
Friend Class Form1
Private m_UseFormFade As Boolean = False
Private m_FadeSpeed As Integer = 30I
#Region " FormFade Stuff "
Private m_FormFadeDir As FadeDir = FadeDir.FadeIn
Private m_FormHasFadedOut As Boolean = False
Private WithEvents m_Timer As Timer
Private Enum FadeDir
FadeIn = 0I
FadeOut = 1I
End Enum
#End Region
#Region " Main Form: FormClosing, Load, Shown "
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
If Not (m_Timer Is Nothing) AndAlso m_FormHasFadedOut = False Then
e.Cancel = True
m_Timer.Start()
End If
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If m_UseFormFade = True Then
Me.Opacity = 0.0R
m_Timer = New Timer()
m_Timer.Interval = m_FadeSpeed
End If
End Sub
Private Sub Form1_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown
If Not (m_Timer Is Nothing) Then m_Timer.Start()
End Sub
#End Region
#Region " FormFadeTimer "
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub FormFadeTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles m_Timer.Tick
Select Case m_FormFadeDir
Case FadeDir.FadeIn
Me.Opacity += 0.05R
Case Else
Me.Opacity -= 0.05R
End Select
If Me.Opacity <= 0.0R OrElse Me.Opacity >= 1.0R Then
m_Timer.Stop()
If m_FormFadeDir = FadeDir.FadeIn Then
m_FormFadeDir = FadeDir.FadeOut
m_FormHasFadedOut = False
Else
m_FormHasFadedOut = True
Me.Close()
End If
End If
End Sub
#End Region
Private Sub CloseButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CloseButton.Click
Me.Close()
End Sub
End Class
This works for all versions of .Net and the .net 2.0 form flicker still happens with my code. Hopefully MS will fix that flicker in a future hotfix.
-
Re: Nice fading in and out form effect
shouldnt this
vb Code:
Private WithEvents m_Timer As Timer
be this ?
vb Code:
Private WithEvents m_Timer As New Timer
and how would i change the speed that it fades out in?
edit you change it in this line....
Me.Opacity -= 0.05R
i change it to
Me.Opacity += 0.15R
and it was alot faster, thank you
-
Re: Nice fading in and out form effect
Quote:
Originally Posted by ahostbr
shouldnt this
vb Code:
Private WithEvents m_Timer As Timer
be this ?
vb Code:
Private WithEvents m_Timer As New Timer
No, the form's Load event is what handles that. I have it instantiated in the Load event to make use of the 'm_UseFormFade' variable, which determines whether the fade happens or not (easy way to turn the fade feature on and off)
Quote:
Originally Posted by ahostbr
and how would i change the speed that it fades out in?
edit you change it in this line....
Me.Opacity -= 0.05R
i change it to
Me.Opacity += 0.15R
and it was alot faster, thank you
Or change the speed of the timer which would have it fade in / out much more smoothly. To change the speed of the timer change the 'm_FadeSpeed' variable, the smaller the number the faster the timer does the fade effect.
-
Re: Nice fading in and out form effect
Nice code, but why not use the AnimateWindow API?
Code:
Public Declare Auto Function AnimateWindow Lib "user32" (ByVal hwnd As IntPtr, ByVal time As Integer, ByVal flags As Integer) As Boolean
Public Enum AnimateStyles
Slide = 262144
Activate = 131072
Blend = 524288
Hide = 65536
Center = 16
HOR_Positive = 1
HOR_Negative = 2
VER_Positive = 4
VER_Negative = 8
End Enum
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
AnimateWindow(Me.Handle, 1000, AnimateStyles.HOR_Negative Or AnimateStyles.Blend)
End Sub
-
Re: Nice fading in and out form effect
Quote:
Originally Posted by .NetNinja
Nice code, but why not use the AnimateWindow API?
Code:
Public Declare Auto Function AnimateWindow Lib "user32" (ByVal hwnd As IntPtr, ByVal time As Integer, ByVal flags As Integer) As Boolean
Public Enum AnimateStyles
Slide = 262144
Activate = 131072
Blend = 524288
Hide = 65536
Center = 16
HOR_Positive = 1
HOR_Negative = 2
VER_Positive = 4
VER_Negative = 8
End Enum
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
AnimateWindow(Me.Handle, 1000, AnimateStyles.HOR_Negative Or AnimateStyles.Blend)
End Sub
using .net 3.5 sp1 with visual studio 2008 pro SP1 that does nothing ...
any help please
-
Re: Nice fading in and out form effect
I just tested it on 3.5 SP1 VS2008 on windows xp and windows vista and it worked. Are you using that exact code or did you modify it any?
-
Re: Nice fading in and out form effect
Quote:
Originally Posted by .NetNinja
I just tested it on 3.5 SP1 VS2008 on windows xp and windows vista and it worked. Are you using that exact code or did you modify it any?
yes exact code... with a bran new blank form... idk what the problem could be... i also downloaded your animatewindow control... and cant seem to get that to work... maybe its my .net or something
-
Re: Nice fading in and out form effect
Shouldn't have anything to do with .Net. Its just a DLL call. Can you post your full form class?
-
Re: Nice fading in and out form effect
Im wanting to fade in a child form after button click on parent form, is this possible if so could i have a hint towards the code please :D
-
Re: Nice fading in and out form effect
Just do it like my previous post states:
Code:
Public Declare Auto Function AnimateWindow Lib "user32" (ByVal hwnd As IntPtr, ByVal time As Integer, ByVal flags As Integer) As Boolean
Public Enum AnimateStyles
Slide = 262144
Activate = 131072
Blend = 524288
Hide = 65536
Center = 16
HOR_Positive = 1
HOR_Negative = 2
VER_Positive = 4
VER_Negative = 8
End Enum
Private Sub Button_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
AnimateWindow(ChildForm.Handle, 1000, AnimateStyles.HOR_Negative Or AnimateStyles.Blend)
ChildForm.ShowDialog();
End Sub
-
Re: Nice fading in and out form effect
Name "AnimateWindow" is not declared, Sorry im still new to VB im using 2008 if that helps.
-
Re: Nice fading in and out form effect
You have to declare it... You have to use all the code I posted.
If you are having an issue post your code and I will help you debug it.
-
Re: Nice fading in and out form effect
This is my current code, i think its a bit mssed up lol.
Code:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Sub FormFade(ByVal FType)
Select Case FType
Case ("in")
Dim FadeCount As Integer
For FadeCount = 10 To 90 Step 10
Me.Opacity = FadeCount / 100
Me.Refresh()
Threading.Thread.Sleep(50)
Next
Case ("out")
Dim FadeCount As Integer
For FadeCount = 90 To 10 Step -10
Me.Opacity = FadeCount / 100
Me.Refresh()
Threading.Thread.Sleep(50)
Next
End Select
Me.Opacity = 99
End Sub
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
AnimateWindow(ChildForm1.Handle, 1000, AnimateStyles.HOR_Negative Or AnimateStyles.Blend)
ChildForm1.ShowDialog();
End Sub
Public Enum AnimateStyles
Slide = 262144
Activate = 131072
Blend = 524288
Hide = 65536
Center = 16
HOR_Positive = 1
HOR_Negative = 2
VER_Positive = 4
VER_Negative = 8
End Enum
End Class
-
Re: Nice fading in and out form effect
You are missing the declaration for AnimateWindow.
Here is your code corrected:
Code:
Public Class Form1
Public Declare Auto Function AnimateWindow Lib "user32" (ByVal hwnd As IntPtr, ByVal time As Integer, ByVal flags As Integer) As Boolean
Public Enum AnimateStyles
Slide = 262144
Activate = 131072
Blend = 524288
Hide = 65536
Center = 16
HOR_Positive = 1
HOR_Negative = 2
VER_Positive = 4
VER_Negative = 8
End Enum
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
AnimateWindow(ChildForm1.Handle, 1000, AnimateStyles.HOR_Negative Or AnimateStyles.Blend)
ChildForm1.ShowDialog();
End Sub
End Class
-
Re: Nice fading in and out form effect
Sorry had to go out, only getting one error now and it saying charcter is invalid and underlining this symbol ;
EDIT: Deleted the symbol and now it works perfect, Thanks alot NetNinja :D
-
Re: Nice fading in and out form effect
ChildForm1.ShowDialog(); <-- remove that, vb doesn't use semicolon line breaks
-
Re: Nice fading in and out form effect
Quote:
Originally Posted by
JuggaloBrotha
ChildForm1.ShowDialog(); <-- remove that, vb doesn't use semicolon line breaks
Yeah sorry about that. :o I've been coding a C# app lately...
-
Re: Nice fading in and out form effect
You guys have been awesome, wonder if you could help me some more lol.
i have my child form fade in on button click from parent form, but when 2nd form is visible i have the application bar thing at the bottom (Start bar where all programmes are open) well i have 1 for each form, any idea if i can have one for both?
-
Re: Nice fading in and out form effect
There is an option in the properties bar for a form in Visual Studio called ShowInTaskbar set that to true or false.
-
Re: Nice fading in and out form effect
Yet again you sieze to amaze me, think im going to be a regular on this forum lol.
Ok Apps been working fine but then when i run a debug to try it out i get this warning.
A first chance exception of type 'System.ComponentModel.Win32Exception' occurred in System.Windows.Forms.dll
And my Fade in doesnt work :S what i done wrong now lol.
-
Re: Nice fading in and out form effect
-
Re: Nice fading in and out form effect
Blue bit is were the fault it.
Code:
Public Class ChildForm1
Sub FormFade(ByVal FType)
Select Case FType
Case ("in")
Dim FadeCount As Integer
For FadeCount = 10 To 90 Step 10
Me.Opacity = FadeCount / 95
Me.Refresh()
Threading.Thread.Sleep(50)
Next
Case ("out")
Dim FadeCount As Integer
For FadeCount = 90 To 10 Step -10
Me.Opacity = FadeCount / 100
Me.Refresh()
Threading.Thread.Sleep(50)
Next
End Select
Me.Opacity = 99
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
FormFade("in")
End Sub
End Class
-
Re: Nice fading in and out form effect
Are you using the FormFade or are you using AnimateWindow API? The examples I gave you were for the AnimateWindow API. I will have to do some testing if you are going to use the FormFade sub.
-
Re: Nice fading in and out form effect
Im using the 1st code you gave me when you re-wrote it for me, it has been working fine then just stopped.
-
Re: Nice fading in and out form effect
The first code I gave you uses the AnimateWindow API. The code you posted uses samtheman code. So you shouldn't have the FormFade sub with my code.
-
Re: Nice fading in and out form effect
Sorry, just remembered i have 2 forms :P lol
This is the code i have in my parent form.
Code:
Public Class Form1
Public Declare Auto Function AnimateWindow Lib "user32" (ByVal hwnd As IntPtr, ByVal time As Integer, ByVal flags As Integer) As Boolean
Public Enum AnimateStyles
Slide = 262144
Activate = 131072
Blend = 524288
Hide = 65536
Center = 16
HOR_Positive = 1
HOR_Negative = 2
VER_Positive = 4
VER_Negative = 8
End Enum
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
AnimateWindow(ChildForm1.Handle, 1000, AnimateStyles.HOR_Negative Or AnimateStyles.Blend)
ChildForm1.ShowDialog()
End Sub
End Class
On the button click i wan t it to fade in form2 but atm its just appearing.
-
Re: Nice fading in and out form effect
I copied your code in a new project and it is working fine...
-
Re: Nice fading in and out form effect
I cant get it to fade in, it just appears, then Child form that is on button click.
-
Re: Nice fading in and out form effect
So if I understand you correctly, when you press the button, the child form is NOT fading in?
-
Re: Nice fading in and out form effect
Yes thats correct it just appears CenterParent.
-
Re: Nice fading in and out form effect
I ran the code you posted and the ChildForm blends correctly... Can you post your code again from your IDE to make sure I'm not missing something.
-
Re: Nice fading in and out form effect
Code from ParentForm
Code:
Public Class Form1
Public Declare Auto Function AnimateWindow Lib "user32" (ByVal hwnd As IntPtr, ByVal time As Integer, ByVal flags As Integer) As Boolean
Public Enum AnimateStyles
Slide = 262144
Activate = 131072
Blend = 524288
Hide = 65536
Center = 16
HOR_Positive = 1
HOR_Negative = 2
VER_Positive = 4
VER_Negative = 8
End Enum
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
AnimateWindow(ChildForm1.Handle, 1000, AnimateStyles.HOR_Negative Or AnimateStyles.Blend)
ChildForm1.ShowDialog()
End Sub
Private Sub ToolTip1_Popup(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PopupEventArgs)
End Sub
End Class
Child Form
Code:
Public Class ChildForm1
Private Sub ChildForm1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
End Class
Aint done alot to child form lol
-
Re: Nice fading in and out form effect
Can you paste the designer code for childform? I used the above code and it worked fine.
-
Re: Nice fading in and out form effect
Thats all code i have.
It maybe just my Laptop lagging, Dont have very good spec. ill just continue with project and see how it goes at the end lol, thanks so much :D
-
Re: Nice fading in and out form effect
Another question, is it possible to fade a button out after clicked? if so how lol.
-
Re: Nice fading in and out form effect
Quote:
Originally Posted by
CimSlunt
Another question, is it possible to fade a button out after clicked? if so how lol.
*waits for chris to jump in with his WPF bandwagon*
Fading a single control on the form is going to be extremely tricky, winforms wasn't designed with that in mind. WPF on the other hand has all of those things in mind.
-
Re: Nice fading in and out form effect
How about just having the button disappear on click then lol
-
Re: Nice fading in and out form effect
Button.Visible = False 'To hide it