65.9K
CodeProject 正在变化。 阅读更多。
Home

.Notifier

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.36/5 (9投票s)

2008年9月2日

CPOL

1分钟阅读

viewsIcon

40497

downloadIcon

797

Notifier:一个类似 Outlook 的通知窗口

引言

本文提供了一个无弹出窗口的通知窗口,并避免了用户“点击确定按钮继续”的困扰。

背景

在使用 Microsoft Outlook 时,你会遇到一个缓慢出现并消失的邮件通知窗口。但是,当你将鼠标移回窗口时,它会变得不透明,然后当鼠标移开时,它会再次消失,这个过程会持续下去。最终,它要么消失,要么你忽略该窗口,或者你关闭它,或者你点击它来阅读邮件。本文描述了如何使用 C# 构建这种窗口。

逻辑

涉及的步骤如下:

  1. 初始透明度设置为 0。
  2. 显示窗口。
  3. 启动计时器 T1。
  4. 在 T1 中逐渐增加透明度,直到达到最大值 1.0。
  5. 停止计时器 T1。
  6. 启动隐藏计时器 T2。
  7. 在 T2 中降低透明度,直到达到最小值 0.01。
  8. 停止计时器 T2。
  9. 清除消息。
  10. 关闭窗口。

Using the Code

实现该逻辑的基本类是 KNotifio

隐藏通知窗口计时器

隐藏计时器的事件如下:

privatevoid 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
   }
}

显示通知窗口计时器

显示计时器的事件如下:

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
   }
}

关闭通知窗口

窗口关闭方式如下:

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) { }
}

窗口显示方式

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);
}

Show() 方法

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)
   {
      ;
   }
}

初始化/构造函数

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);
}

圆角

我们调用一个 API 来使角变圆。

/// <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
   );
....

关注点

可以设置用户特定的窗口动画。 同样,例如,消息类型至关重要,并且可以使框闪烁等。 可以使控件更具动画效果、以用户为中心,我们只需要设置 AnimationTypes.Warning 标签。

© . All rights reserved.