前言

消息通知在应用程序中,是一种非常有用的功能,实现对一些重要信息、提醒或警告及时向用户展示。我们在使用软件时,通常会收到一种从桌面右下角弹出的(提示信息或广告)信息框。本文将介绍使用 C# 实现此种方式的信息通知窗口。

实现

1、使用 API 的 AnimateWindow 函数

定义 AnimateWindows

using System;
using System.Runtime.InteropServices;
namespace Fountain.WinForm.MessageBoxDemo
{
    public class Win32
    {
        /// <summary>
        /// 自左向右显示窗口,该标记可以在迁移转变动画和滑动动画中应用。应用AW_CENTER标记时忽视该标记
        /// </summary>
        public const int AW_HOR_POSITIVE = 0x0001;
        /// <summary>
        /// 自右向左显示窗口,该标记可以在迁移转变动画和滑动动画中应用。应用AW_CENTER标记时忽视该标记
        /// </summary>
        public const int AW_HOR_NEGATIVE = 0x0002;
        /// <summary>
        /// 自顶向下显示窗口,该标记可以在迁移转变动画和滑动动画中应用。应用AW_CENTER标记时忽视该标记
        /// </summary>
        public const int AW_VER_POSITIVE = 0x0004;
        /// <summary>
        /// 自下向上显示窗口,该标记可以在迁移转变动画和滑动动画中应用。应用AW_CENTER标记时忽视该标记该标记
        /// </summary>
        public const int AW_VER_NEGATIVE = 0x0008;
        /// <summary>
        /// 若应用了AW_HIDE标记,则使窗口向内重叠;不然向外扩大
        /// </summary>
        public const int AW_CENTER = 0x0010;
        /// <summary>
        /// 隐蔽窗口
        /// </summary>
        public const int AW_HIDE = 0x10000;
        /// <summary>
        /// 激活窗口,在应用了AW_HIDE标记后不要应用这个标记
        /// </summary>
        public const int AW_ACTIVE = 0x20000;
        /// <summary>
        /// 滑动类型动画结果,默认为迁移转变动画类型,当应用AW_CENTER标记时,这个标记就被忽视
        /// </summary>
        public const int AW_SLIDE = 0x40000;
        /// <summary>
        /// 淡入淡出结果
        /// </summary>
        public const int AW_BLEND = 0x80000;
        /// <summary>
        /// 窗体动画函数
        /// </summary>
        /// <param name="hwnd"></param>
        /// <param name="dwTime"></param>
        /// <param name="dwFlags"></param>
        /// <returns></returns>
        [DllImport("user32")]
        public static extern bool AnimateWindow(IntPtr hwnd, int dwTime, int dwFlags);
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.

定义显示消息窗体

using System;
using System.Drawing;
using System.Windows.Forms;
namespace Fountain.WinForm.MessageBoxDemo
{
    public partial class FormMessageBox : Form
    {
        /// <summary>
        /// 关闭窗口的定时器
        /// </summary>
        private Timer formCloseTime = new Timer();
        /// <summary>
        /// 构造方法
        /// </summary>
        public FormMessageBox()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 窗体加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FormMessageBox_Load(object sender, EventArgs e)
        {
            // 手动设置起始位置
            this.StartPosition = FormStartPosition.Manual;
            // 计算屏幕尺寸并将窗体放置在右下角
            Rectangle screenRectangle = Screen.PrimaryScreen.WorkingArea;
            int x = screenRectangle.Width - this.Width;
            int y = screenRectangle.Height - this.Height;
            this.Location = new Point(x, y);
            this.TopMost = true;
            Win32.AnimateWindow(this.Handle, 1000, Win32.AW_SLIDE + Win32.AW_VER_NEGATIVE);
            this.ShowInTaskbar = false;
            formCloseTime.Interval = 5000;
            formCloseTime.Tick += new EventHandler(formCloseTime_Tick);
            formCloseTime.Start();
        }
        /// <summary>
        /// 关闭窗口的定时器响应事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void formCloseTime_Tick(object sender, EventArgs e)
        {
            formCloseTime.Stop();
            this.Close();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FormMessageBox_FormClosed(object sender, FormClosedEventArgs e)
        {
            formCloseTime.Stop();
            formCloseTime.Dispose();
            Win32.AnimateWindow(this.Handle, 1000, Win32.AW_SLIDE + Win32.AW_VER_POSITIVE + Win32.AW_HIDE);
        }
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.

主界面调用

FormMessageBox formMessageBox = new FormMessageBox();
formMessageBox.Show();
  • 1.
  • 2.

2、控制窗体显示

定义显示消息窗体

using System;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;
namespace Fountain.WinForm.MessageBoxDemo
{
    public partial class FormNotifyBox : Form
    {
        /// <summary>
        /// 关闭窗口的定时器
        /// </summary>
        private System.Windows.Forms.Timer formCloseTime = new System.Windows.Forms.Timer();
        /// <summary>
        /// 
        /// </summary>
        private Point formPoint;
        /// <summary>
        /// 
        /// </summary>
        public FormNotifyBox()
        {
            InitializeComponent();
            this.formPoint = new Point(Screen.PrimaryScreen.WorkingArea.Width - this.Width, Screen.PrimaryScreen.WorkingArea.Height);
            // 设置窗体在屏幕右下角显示
            this.Location = formPoint;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FormNotifyBox_Load(object sender, EventArgs e)
        {
            try
            {
                formCloseTime.Interval = 5000;
                formCloseTime.Tick += new EventHandler(formCloseTime_Tick);
                formCloseTime.Start();
                this.TopMost = false;
                this.BringToFront();
                this.TopMost = true;
                this.PointToClient(this.formPoint);
                this.Location = this.formPoint;
                this.Show();
                for (int i = 0; i < this.Height; i++)
                {
                    this.Location = new Point(formPoint.X, formPoint.Y - i);
                    // 消息框弹出速度,数值越大越慢
                    Thread.Sleep(1);
                }
            }
            catch (Exception exception)
            {   
            }
        }
        /// <summary>
        /// 关闭窗口的定时器响应事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void formCloseTime_Tick(object sender, EventArgs e)
        {
            formCloseTime.Enabled = false;
            for (int i = 0; i <= this.Height; i++)
            {
                //弹出框向下移动消失
                Point point = new Point(this.Location.X, this.Location.Y + i);
                this.PointToScreen(point);
                //即时转换成屏幕坐标
                this.Location = point;
                //下降速度调节,数字越小消失的速度越快,建议不大于10
                Thread.Sleep(8);
            }
            this.Close();
            this.Dispose();
        }
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.

主界面调用

FormNotifyBox notifyForm = new FormNotifyBox();
notifyForm.Show();
  • 1.
  • 2.