.Notifier: Outlook-Like Notifier

.Notifier: Outlook-Like Notification Window

When using Microsoft Outlook, you come across a mail notification window that appears slowly and fades off. But, when you drop your mouse back on the window, it becomes opaque, and then again on mouse away it fades off, and this process continues so on. So, eventually it either fades away or you ignore the window, or you close it or you click it to read the mail. This article describes how to build that kind of window using C#.

Let me know if you encounter problems or have a suggestion for improvement.

The Logic

Following is the program flow:

  1. Make opacity level 0 initially.
  2. Display window.
  3. Start show timer T1.
  4. Gradually increase the opacity in T1 until it reaches max 1.0 level.
  5. Stop timer T1.
  6. Start the hiding timer T2.
  7. Decrease the opacity level in T2 until it reaches min level 0.01.
  8. Stop the timer T2.
  9. Clean message.
  10. Close the window.

Using the Code

Following is how you use the code. If you would like to change the notification window display rates, use the following:

KNotifier.KNotifio.Show(txtMessage.Text,    //the message itself
                        nShow,              //Time to show the window
                        nHide);             //Time to hide the window

Otherwise, to just send the message, use the following:

KNotifier.KNotifio.Show("This is a test message.");    //Simple call

Hide Notification Window Timer

The hide timer event goes as follows:

private void tmrHide_Tick(object sender, EventArgs e)
{
   //Decrease the opacity level until its greater than zero
   if (this.Opacity > 0.00)
   {
      this.Opacity -= 0.01;
   }
   else
   {
      //Window has gone completely transparent
      tmrHide.Stop();    //Stop the timer
      CloseWnd();        //Close window
   }
}

Show Notification Window Timer

The show timer event goes as follows:

private void tmrShow_Tick(object sender, EventArgs e)
{
   //Increase opacity until it reaches maximum
   if (this.Opacity < 0.99)
   {
      this.Opacity += 0.01;
   }
   else
   {
      //Window already opaque
      tmrShow.Stop();     //Let's stop the timer

      tmrHide.Start();    //Start hiding the window
   }
}

Closing the Notification Window

Following is how the window is closed.

private void CloseWnd()
{
   try
   {
      //Stop timers
      g_Fio.tmrHide.Stop();
      g_Fio.tmrShow.Stop();

      g_Fio.Close();                          //Close the form
      m_strPreviousMessage = string.Empty;    //Clean message
   }
   catch (Exception exec) { }
}

How the Window Is Shown

public static void Show(string strMessage, i nShowTime, int nHideTime)
{
   //Set the time it should take to show the window
   m_nShowTime = nShowTime - 5;
   //Set the time it would take to hide the window
   m_nHideTime = nHideTime;
   Show(strMessage);
}

The Show() Method

public static void Show(string strMessage)
{
   try
   {
      if (m_strPreviousMessage == strMessage)
         return;
      else
         m_strPreviousMessage = strMessage;
         KNotifio theNotifio = new KNotifio();
         g_Fio = theNotifio;
         theNotifio.txtMessage.Text = strMessage;
         theNotifio.Show();
         theNotifio.panel1.Focus();
   }
   catch (Exception exc)
   {
      ;
   }
}

Initialization/Constructor

public KNotifio()
{
   InitializeComponent();
   tmrHide.Interval = m_nHideTime;    //Set timers
   tmrShow.Interval = m_nShowTime;

   try
   {
      //Set round rects
   } catch (Exception exc) { }

   //Move window close to system tray (above the clock)
   Location = new Point(Screen.PrimaryScreen.Bounds.Width -
      this.Width, Screen.PrimaryScreen.Bounds.Height -
      this.Height - 50);
}

Rounded Corners

We call an API to make the corners round.

/// <summary>
/// Provides MS Outlook styled dynamic notification window
/// </summary>
public partial class KNotifio : Form
{
   //Need to make our window a bit rounded.
   [DllImport("Gdi32.dll", EntryPoint = "CreateRoundRectRgn")]
   private static extern IntPtr CreateRoundRectRgn
   (
      int nLeftRect,        // x-coordinate of upper-left corner
      int nTopRect,         // y-coordinate of upper-left corner
      int nRightRect,       // x-coordinate of lower-right corner
      int nBottomRect,      // y-coordinate of lower-right corner
      int nWidthEllipse,    // height of ellipse
      int nHeightEllipse    // width of ellipse
   );
....

Feedback

Let me about the reason in case you don't like it and the ways I can be of help. I would love that.



About the Author

KMan KMan

http://izlooite.blogspot.com

Downloads

Comments

  • !

    Posted by jusstujoo on 03/13/2009 01:39am

    !

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

  • Live Event Date: November 20, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT Are you wanting to target two or more platforms such as iOS, Android, and/or Windows? You are not alone. 90% of enterprises today are targeting two or more platforms. Attend this eSeminar to discover how mobile app developers can rely on one IDE to create applications across platforms and approaches (web, native, and/or hybrid), saving time, money, and effort and introducing apps to market faster. You'll learn the trade-offs for gaining long …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds