Introduction
This article will show you how you can add fading to your forms. The code can be used at any time regardless of what the current opacity of the form is.
Using the code
The code can be implemented by either adding Fade.cs to your project, or by adding a reference to Fade.dll.
To use the code, add a using
statement to include the Fader namespace.
using Fader;
Calling the method is then a simple task. The syntax for the fading methods is:
Fade.direction(form);
The direction is either In
or Out
, and the form is the one to fade. We will now look at the In
and Out
methods in more detail.
While both methods can be called in the same way, by just supplying the target form, each is overloaded allowing their behavior to be modified. The In
method can be called by passing one property as such:
Fade.In(this);
or it can be called with its overloaded method which is called with an additional boolean value.
Fade.In(this, true);
The boolean value in this overload is FromHidden
which sets the opacity of the form to 0 before fading in. This allows the method to be used in the Form_Load
event without having to manually set the opacity to 0.
As with the In
method, the Out
method can also be called using either one or two properties. Calling the method using only the Form
property is done in the same way as the In
method:
Fade.Out(this);
The overloaded method accepts the Exit
boolean value which closes the application once the fade out has finished.
Fade.Out(this, true);
Development
I originally attempted to carry out the fading process within a loop using System.Threading.Thread.Sleep()
. This worked perfectly for fading forms out, however it did not work for fading the form in. I then turned to using the Windows Forms timers. The timers used are created programmatically within the In
and Out
methods and disposed once the fading is complete.
The Fade
class three private global variables which are used to pass the properties from the public In
and Out
methods to the private FadeIn
and FadeOut
methods which are used as the event handlers for the timers.
Using timers worked well, however there was an issue with the fading in. Due to Windows adding its own effect to forms when they are shown, the fading is somewhat overshadowed. To overcome this, there is a 1 second delay before the fade in begins to allow the built in effect to pass. The form's opacity is set to 0 before the delay to hide it during the built in effect. This is the code for the In
method accepting only the form
public static void In(Form Form)
{
frmTarget = Form;
Form.Opacity = Form.Opacity - (Form.Opacity % 0.05);
Timer newTimer = new Timer();
newTimer.Interval = 1;
if (blnFromHidden) { newTimer.Interval = 1000; }
newTimer.Tick += new EventHandler(FadeIn);
newTimer.Start();
}
The fading works by adding 0.05 to the opacity on the Tick
event of the timer. Because of this, the opacity needs to be a multiple of 0.05. This is achieved by subtracting the remainder of the opacity divided by 0.05. The timer is then created with an interval of 1 millisecond. The code then checks to see if blnFromHidden
is set to true. This is set in the overloaded method which also sets the form's opacity to 0.
public static void In(Form Form, bool FromHidden)
{
if (FromHidden)
{
Form.Opacity = 0;
blnFromHidden = true;
}
In(Form);
}
If blnFromHidden
is true, the timer interval is set to 1000. This is where the 1 second delay comes from. Once the timer interval is set, the Tick
event is set to the FadeIn
method and the timer is started.
private static void FadeIn(object sender, EventArgs e)
{
(sender as Timer).Interval = 1;
if (frmTarget.Opacity != 1)
{
frmTarget.Opacity += 0.05;
}
else
{
(sender as Timer).Stop();
(sender as Timer).Dispose();
}
}
The FadeIn
method first sets the interval to 1. This ensures that the second long delay is only applied once. If the opacity isn't 1, it is increased by 0.05, if it is 1, the timer is stopped and, disposed.
The Out
method works in a similar way to the In
method but without the delay.
public static void Out(Form Form)
{
frmTarget = Form;
Form.Opacity = Form.Opacity - (Form.Opacity % 0.05);
Timer newTimer = new Timer();
newTimer.Interval = 1;
newTimer.Tick += new EventHandler(FadeOut);
newTimer.Start();
}
public static void Out(Form Form, bool Exit)
{
blnExit = Exit;
Out(Form);
}
When the form has been hidden and the timer has been stopped, if blnExit
is true, the application will exit.
private static void FadeOut(object sender, EventArgs e)
{
if (frmTarget.Opacity != 0)
{
frmTarget.Opacity -= 0.05;
}
else
{
(sender as Timer).Stop();
(sender as Timer).Dispose();
if (blnExit)
{
Application.Exit();
}
}
}
Plans for improvement
To further improve this feature, I plan to add the ability to change the speed of the fade to allow the fading to be customised slightly.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.