Winform 屏幕右下角彈出提示窗口

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


using System.Runtime.InteropServices;


namespace CommonApp
{
    /// <summary>
    /// 枚舉,描述消息窗口加載的形式
    /// </summary>
    public enum LoadMode
    {
        /// <summary>
        /// 警告
        /// </summary>
        Warning,


        /// <summary>
        /// 錯誤
        /// </summary>
        Error,


        /// <summary>
        /// 提示
        /// </summary>
        Prompt
    }


    /// <summary>
    /// 消息提示窗口
    /// </summary>
    public partial class FormMessageBox : Form
    {
        /// <summary>
        /// 構造方法
        /// </summary>
        public FormMessageBox()
        {
            InitializeComponent();
        }




        #region ***********************字 段***********************


        /// <summary>
        /// 窗體加載模式
        /// </summary>
        private static LoadMode FormMode = LoadMode.Prompt;


        /// <summary>
        /// 顯示的消息正文
        /// </summary>
        private static string ShowMessage = null;


        /// <summary>
        /// 關閉窗口的定時器
        /// </summary>
        private Timer Timer_Close = new Timer();




        [DllImportAttribute("user32.dll")]
        private static extern bool AnimateWindow(IntPtr hwnd, int dwTime, int dwFlags);   // 該函數可以實現窗體的動畫效果


        /// <summary>
        /// 自左向右顯示窗口。該標誌可以在滾動動畫和滑動動畫中使用。當使用AW_CENTER標誌時,該標誌將被忽略 
        /// </summary>
        public const Int32 AW_HOR_POSITIVE = 0x00000001;
        /// <summary>
        ///  自右向左顯示窗口。當使用了 AW_CENTER 標誌時該標誌被忽略
        /// </summary>
        public const Int32 AW_HOR_NEGATIVE = 0x00000002;
        /// <summary>
        /// 自頂向下顯示窗口。該標誌可以在滾動動畫和滑動動畫中使用。當使用AW_CENTER標誌時,該標誌將被忽略
        /// </summary>
        public const Int32 AW_VER_POSITIVE = 0x00000004;   // 
        /// <summary>
        /// 自下向上顯示窗口。該標誌可以在滾動動畫和滑動動畫中使用。當使用AW_CENTER標誌時,該標誌將被忽略
        /// </summary>
        public const Int32 AW_VER_NEGATIVE = 0x00000008;   // 
        /// <summary>
        /// 若使用了AW_HIDE標誌,則使窗口向內重疊;若未使用AW_HIDE標誌,則使窗口向外擴展
        /// </summary>
        public const Int32 AW_CENTER = 0x00000010;         // 
        /// <summary>
        /// 隱藏窗口,缺省則顯示窗口
        /// </summary>
        public const Int32 AW_HIDE = 0x00010000;           // 
        /// <summary>
        /// 激活窗口。在使用了AW_HIDE標誌後不要使用這個標誌
        /// </summary>
        public const Int32 AW_ACTIVATE = 0x00020000;       // 
        /// <summary>
        /// 使用滑動類型。缺省則爲滾動動畫類型。當使用AW_CENTER標誌時,這個標誌就被忽略
        /// </summary>
        public const Int32 AW_SLIDE = 0x00040000;          // 
        /// <summary>
        /// 使用淡入效果。只有當hWnd爲頂層窗口的時候纔可以使用此標誌
        /// </summary>
        public const Int32 AW_BLEND = 0x00080000;          // 




        #endregion*************************************************




        #region ***********************方 法***********************


        /// <summary>
        /// 構造方法
        /// </summary>
        /// <param name="loadMode">加載模式</param>
        /// <param name="message">消息正文</param>


        public static void Show(LoadMode loadMode, string message)
        {
            FormMode = loadMode;
            ShowMessage = message;


            FormMessageBox box = new FormMessageBox();
            box.Show();
        }






        #endregion*************************************************




        #region ***********************事 件***********************


        /// <summary>
        /// 窗體加載事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FormMessageBox_Load(object sender, EventArgs e)
        {
            this.lblTitle.Text = "提示";
            if (FormMode == LoadMode.Error)
            {
                this.lblTitle.Text = "錯誤";
                this.plShow.BackgroundImage = global::CommonApp.Properties.Resources.error;    // 更換背景
            }
            else if (FormMode == LoadMode.Warning)
            {
                this.lblTitle.Text = "警告";
                this.plShow.BackgroundImage = global::CommonApp.Properties.Resources.warning;  // 更換背景
            }
            else
            {
                this.plShow.BackgroundImage = global::CommonApp.Properties.Resources.Prompt;   // 更換背景
            }


            this.lblMessage.Text = ShowMessage;


            int width = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width;
            int height = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height;
            int top = height - 35 - this.Height;
            int left = width - this.Width - 5;
            this.Top = top;
            this.Left = left;
            this.TopMost = true;


            AnimateWindow(this.Handle, 500, AW_SLIDE + AW_VER_NEGATIVE);//開始窗體動畫


            this.ShowInTaskbar = false;


            Timer_Close.Interval = 4000;
            Timer_Close.Tick += new EventHandler(Timer_Close_Tick);
            Timer_Close.Start();
        }


        /// <summary>
        /// 關閉窗口的定時器響應事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Timer_Close_Tick(object sender, EventArgs e)
        {
            Timer_Close.Stop();


            this.Close();
        }


        /// <summary>
        /// 窗口已經關閉
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FormMessageBox_FormClosed(object sender, FormClosedEventArgs e)
        {
            AnimateWindow(this.Handle, 1000, AW_SLIDE + AW_VER_POSITIVE + AW_HIDE);


            Timer_Close.Stop();
            Timer_Close.Dispose();
        }


        /// <summary>
        /// 鼠標移動在消息框上
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void plShow_MouseMove(object sender, MouseEventArgs e)
        {
            this.Timer_Close.Stop();
        }


        /// <summary>
        /// 鼠標移動離開消息框上
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void plShow_MouseLeave(object sender, EventArgs e)
        {
            this.Timer_Close.Start();
        }


        #endregion*************************************************


    }
}


          


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章