Introduction
This article provides a popup-less notification window, and saves the user from the "Click-on-the-OK-button-to-continue" pain.
Background
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, 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 a window using C#.
The Logic
The following are the steps involved:
- Make opacity level 0 initially.
- Display window.
- Start to show timer T1.
- Gradually increase the opacity in T1 until it reaches a maximum level 1.0.
- Stop timer T1.
- Start the hiding timer T2.
- Decrease the opacity level in T2 until it reaches the minimum level 0.01.
- Stop timer T2.
- Clean the message.
- Close the window.
Using the Code
The basic class that implements the logic is KNotifio
.
Hide the Notification Window Timer
The hide timer event goes as follows:
privatevoid tmrHide_Tick(object sender, EventArgs e)
{
if (this.Opacity > 0.00)
{
this.Opacity -= 0.01;
}
else
{
tmrHide.Stop();
CloseWnd();
}
}
Show the Notification Window Timer
The show timer event goes as follows:
private void tmrShow_Tick(object sender, EventArgs e)
{
if (this.Opacity < 0.99)
{
this.Opacity += 0.01;
}
else
{
tmrShow.Stop();
tmrHide.Start();
}
}
Closing the Notification Window
Following is how the window is closed:
private void CloseWnd()
{
try
{
g_Fio.tmrHide.Stop();
g_Fio.tmrShow.Stop();
g_Fio.Close();
m_strPreviousMessage = string.Empty;
}
catch (Exception exec) { }
}
How the Window is Shown
public static void Show(string strMessage, i nShowTime, int nHideTime)
{
m_nShowTime = nShowTime - 5;
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;
tmrShow.Interval = m_nShowTime;
try
{
} catch (Exception exc) { }
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.
public partial class KNotifio : Form
{
[DllImport("Gdi32.dll", EntryPoint = "CreateRoundRectRgn")]
private static extern IntPtr CreateRoundRectRgn
(
int nLeftRect,
int nTopRect,
int nRightRect,
int nBottomRect,
int nWidthEllipse,
int nHeightEllipse
);
....
Points of Interest
User specific window animations can be set. Similarly, for instance, the message type is critical information, and the box can made blinking, etc. The control can be made more animated, user focused, and we only would need to set the AnimationTypes.Warning
tags.