Click here to Skip to main content
15,868,077 members
Articles / Desktop Programming / Windows Forms
Article

Form appearance effect and notification window

Rate me:
Please Sign up or sign in to vote.
4.35/5 (14 votes)
11 Jul 20052 min read 98.1K   7.7K   106   13
This article contains a form that can be used to give fade in/out effect to any form and specifically to notification/alert windows.

Sample Image

Introduction

Background processes, applications that run from tray and many other types of applications frequently need to show notifications/alerts to the user. One of the common examples is Outlook 2003 which shows an email notification just over the system tray. This article is about a base class TransDialog that derives from Form and adds the fade in/out effect to any form. It also contains a notification form that derives from this TransDialog and shows notification over the system tray.

Using the code

The project contains the following three forms/classes:

  1. TransDialog - Derives from System.Windows.Forms.Form and adds the fade in effect.
  2. Notification - Derives from TransDialog and actually shows the notification.
  3. Form1 - Driver form just used for the demo.

If you are just interested in adding the fade in/out effect, you can derive any form from TransDialog as follows:

C#
public class Notification : TransDialog
{
    #region Ctor, init code and dispose
    public Notification()
       : base(true)
    {
        InitializeComponent();
    }
    /* ... */
}

Passing true to the base class will ensure that when you call Close on the Notification form, TransDialog will call Dispose and do the cleanup.

How does TransDialog work

TransDialog uses the layering (opacity) property of the form to add the effect of fade in/out. At the Form Load event, the opacity of the form is set to 0 (completely transparent or invisible) and a timer m_clock is started. The variable m_bShowing is set to true. The timer is set to tick every 100 ms.

C#
private void TransDialog_Load(object sender, EventArgs e)
{
    this.Opacity = 0.0;
    m_bShowing = true;
    m_clock.Start();
}

On every Tick event as long as m_bShowing is true, the opacity is increased until it reaches 1 (completely opaque).

C#
if (m_bShowing)
{
    if (this.Opacity < 1)
    {
        this.Opacity += 0.1;
    }
    else
    {
       m_clock.Stop();
    }
}

This gives the fade in effect.

On form closing event, the m_bShowing is set to false, the form closing is canceled and the timer is started again. However since this time m_bShowing is false, the opacity is decreased until 0 is reached (completely transparent). This gives the fade out effect.

C#
private void TransDialog_Closing(object sender, CancelEventArgs e)
{
    /* ... */
    m_origDialogResult = this.DialogResult;
    e.Cancel = true;
    m_bShowing = false;
    m_clock.Start();
    /* ... */
}

How does Notification work

The fade in/out effect on the notification works just by deriving from TransDialog. To show the form at the correct location over the system tray, the following code is used in the Load event handler.

C#
private void Notification_Load(object sender, System.EventArgs e)
{
    /* ... */
    int screenWidth = Screen.PrimaryScreen.WorkingArea.Width;
    int screenHeight = Screen.PrimaryScreen.WorkingArea.Height;
    this.Left = screenWidth - this.Width;
    this.Top = screenHeight - this.Height;
    /* ... */
}

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
I just love coding. I started programming in 1995 with BASIC and then moved through Cobol, Pascal, Prolog, C, C++, VB, VC++ and now C#/.NET.

I received a Bachelor of Technology degree in Computer Science from University of Calcutta in 2001.

I worked for some time in Texas Instruments, Adobe Systems and now in Microsoft India Development Center in the Visual Studio Team Systems.

I am from the City of Joy, Kolkata in India, but now live and code Hyderabad.

Comments and Discussions

 
QuestionPlease license Pin
Member 1412598525-Nov-20 20:23
Member 1412598525-Nov-20 20:23 
SuggestionGreat Dear ... Its awesome Pin
Zia Ul Mustafa Kaleem17-Mar-16 5:02
professionalZia Ul Mustafa Kaleem17-Mar-16 5:02 
Thanks for posting a fully working example. I was searching for this for more than one week; now I got and I am successful now Smile | :) Very glad .. 100% Vote for u.. Keep it up Smile | :)
GeneralMy vote of 3 Pin
Gil727-Jan-12 23:02
Gil727-Jan-12 23:02 
Questionany updates in 2011 ?? Pin
kiquenet.com7-Feb-11 10:57
professionalkiquenet.com7-Feb-11 10:57 
GeneralI want it in jsp or css Pin
bindupriya19917-Feb-10 23:14
bindupriya19917-Feb-10 23:14 
GeneralWindow Flashes Pin
carlmalden11-Jun-08 9:47
carlmalden11-Jun-08 9:47 
GeneralNot working if the window is not active Pin
SudhakarReddi23-May-07 22:34
SudhakarReddi23-May-07 22:34 
GeneralRe: Not working if the window is not active Pin
jbshaler10-Jul-07 5:54
jbshaler10-Jul-07 5:54 
GeneralSystem tray location Pin
junrau14-Dec-06 9:16
junrau14-Dec-06 9:16 
GeneralGood God Pin
kryzchek16-Nov-06 5:55
kryzchek16-Nov-06 5:55 
GeneralA little mod to make the transition more dynamic Pin
_marsim_3-Apr-06 23:51
_marsim_3-Apr-06 23:51 
GeneralStealing focus Pin
Anonymous1-Aug-05 3:21
Anonymous1-Aug-05 3:21 
GeneralRe: Stealing focus Pin
Anonymous2-Aug-05 1:33
Anonymous2-Aug-05 1:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.