-
Oct 9th, 2008, 12:56 PM
#1
Thread Starter
Hyperactive Member
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.
Last edited by samtheman; Oct 9th, 2008 at 01:08 PM.
If you found any of my posts helpful then please rate them.
CodeBank
Form Fading Effects in VB.NET and C#
-
Oct 9th, 2008, 02:41 PM
#2
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
Last edited by JuggaloBrotha; Oct 13th, 2008 at 08:10 AM.
-
Oct 16th, 2008, 08:46 AM
#3
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.
Last edited by JuggaloBrotha; Oct 16th, 2008 at 08:51 AM.
-
Oct 24th, 2008, 05:46 AM
#4
Lively Member
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
Last edited by ahostbr; Oct 24th, 2008 at 05:50 AM.
-
Oct 24th, 2008, 08:55 AM
#5
Re: Nice fading in and out form effect
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)
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.
-
Nov 10th, 2008, 02:26 PM
#6
Hyperactive Member
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
"Don't try to be a great man. Just be a man and let history make its own judgement."
-
Nov 20th, 2008, 12:09 AM
#7
Lively Member
Re: Nice fading in and out form effect
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
-
Nov 20th, 2008, 07:35 AM
#8
Hyperactive Member
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?
"Don't try to be a great man. Just be a man and let history make its own judgement."
-
Nov 20th, 2008, 12:14 PM
#9
Lively Member
Re: Nice fading in and out form effect
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
-
Nov 20th, 2008, 12:50 PM
#10
Hyperactive Member
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?
"Don't try to be a great man. Just be a man and let history make its own judgement."
-
Aug 18th, 2009, 06:28 AM
#11
Member
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
-
Aug 18th, 2009, 07:07 AM
#12
Hyperactive Member
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
"Don't try to be a great man. Just be a man and let history make its own judgement."
-
Aug 18th, 2009, 07:39 AM
#13
Member
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.
-
Aug 18th, 2009, 07:41 AM
#14
Hyperactive Member
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.
"Don't try to be a great man. Just be a man and let history make its own judgement."
-
Aug 18th, 2009, 07:46 AM
#15
Member
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
-
Aug 18th, 2009, 07:49 AM
#16
Hyperactive Member
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
"Don't try to be a great man. Just be a man and let history make its own judgement."
-
Aug 18th, 2009, 11:09 AM
#17
Member
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
-
Aug 18th, 2009, 11:30 AM
#18
Re: Nice fading in and out form effect
ChildForm1.ShowDialog(); <-- remove that, vb doesn't use semicolon line breaks
-
Aug 18th, 2009, 12:01 PM
#19
Hyperactive Member
Re: Nice fading in and out form effect
Originally Posted by JuggaloBrotha
ChildForm1.ShowDialog(); <-- remove that, vb doesn't use semicolon line breaks
Yeah sorry about that. I've been coding a C# app lately...
"Don't try to be a great man. Just be a man and let history make its own judgement."
-
Aug 18th, 2009, 01:44 PM
#20
Member
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?
-
Aug 18th, 2009, 02:22 PM
#21
Hyperactive Member
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.
"Don't try to be a great man. Just be a man and let history make its own judgement."
-
Aug 18th, 2009, 03:07 PM
#22
Member
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.
-
Aug 18th, 2009, 03:16 PM
#23
Hyperactive Member
Re: Nice fading in and out form effect
"Don't try to be a great man. Just be a man and let history make its own judgement."
-
Aug 18th, 2009, 03:26 PM
#24
Member
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
-
Aug 18th, 2009, 03:43 PM
#25
Hyperactive Member
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.
"Don't try to be a great man. Just be a man and let history make its own judgement."
-
Aug 18th, 2009, 03:49 PM
#26
Member
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.
-
Aug 18th, 2009, 03:53 PM
#27
Hyperactive Member
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.
"Don't try to be a great man. Just be a man and let history make its own judgement."
-
Aug 18th, 2009, 04:09 PM
#28
Member
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.
-
Aug 19th, 2009, 07:11 AM
#29
Hyperactive Member
Re: Nice fading in and out form effect
I copied your code in a new project and it is working fine...
"Don't try to be a great man. Just be a man and let history make its own judgement."
-
Aug 19th, 2009, 09:02 AM
#30
Member
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.
-
Aug 19th, 2009, 09:03 AM
#31
Hyperactive Member
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?
"Don't try to be a great man. Just be a man and let history make its own judgement."
-
Aug 19th, 2009, 09:07 AM
#32
Member
Re: Nice fading in and out form effect
Yes thats correct it just appears CenterParent.
-
Aug 19th, 2009, 09:14 AM
#33
Hyperactive Member
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.
"Don't try to be a great man. Just be a man and let history make its own judgement."
-
Aug 19th, 2009, 09:16 AM
#34
Member
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
-
Aug 19th, 2009, 09:42 AM
#35
Hyperactive Member
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.
"Don't try to be a great man. Just be a man and let history make its own judgement."
-
Aug 19th, 2009, 09:46 AM
#36
Member
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
-
Aug 19th, 2009, 10:07 AM
#37
Member
Re: Nice fading in and out form effect
Another question, is it possible to fade a button out after clicked? if so how lol.
-
Aug 19th, 2009, 11:04 AM
#38
Re: Nice fading in and out form effect
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.
-
Aug 19th, 2009, 11:58 AM
#39
Member
Re: Nice fading in and out form effect
How about just having the button disappear on click then lol
-
Aug 19th, 2009, 12:16 PM
#40
Re: Nice fading in and out form effect
Button.Visible = False 'To hide it
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|