場景
效果


注:
博客主頁:
https://blog.csdn.net/badao_liumang_qizhi
關注公眾號
霸道的程序猿
獲取編程相關電子書、教程推送與免費下載。
實現
新建一個form窗體,並拖拽一個按鈕,作為出現彈窗的觸發按鈕


然后再新建一個頁面作為彈窗時的顯示頁面,並添加一個Timer


為了美觀,設置其背景圖片與關閉按鈕照片


然后進入其代碼
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 Popup.Controls { partial class Frm_Popup : System.Windows.Forms.Form { #region 變量 private InformStyle InfoStyle = InformStyle.Vanish;//定義變量為隱藏 private System.Drawing.Rectangle Rect;//定義一個存儲矩形框的數組 private bool isMouseMove = false;//是否在窗體中移動 static private Frm_Popup F_Popup = new Frm_Popup();//實例化當前窗體 #endregion #region 內置屬性 /// <summary> /// 定義一個任務通知器的枚舉值 /// </summary>//InformStyle protected enum InformStyle { /// <summary> /// 隱藏 /// </summary> Vanish = 0, /// <summary> /// 顯視 /// </summary> Display = 1, /// <summary> /// 顯視中 /// </summary> Displaying = 2, /// <summary> /// 隱藏中 /// </summary> Vanishing = 3 } /// <summary> /// 獲取或設置當前的操作狀態 /// </summary> protected InformStyle InfoState { get { return this.InfoStyle; } set { this.InfoStyle = value; } } #endregion public Frm_Popup() { this.InitializeComponent(); this.timer1.Stop();//停止計時器 //初始化工作區大小 System.Drawing.Rectangle rect = System.Windows.Forms.Screen.GetWorkingArea(this); this.Rect = new System.Drawing.Rectangle( rect.Right - this.Width - 1, rect.Bottom - this.Height - 1, this.Width, this.Height ); } #region 返回當前窗體的實例化 /// <summary> /// 返回此對象的實例 /// </summary> /// <returns></returns> static public Frm_Popup Instance() { return F_Popup; } #endregion #region 聲明WinAPI /// <summary> /// 顯示窗體 /// </summary> /// <param name="hWnd"></param> /// <param name="nCmdShow"></param> /// <returns></returns> [DllImport("user32.dll")] private static extern Boolean ShowWindow(IntPtr hWnd, Int32 nCmdShow); #endregion #region 方法 /// <summary> /// 顯示窗體 /// </summary> public void Show() { switch (this.InfoState) { case InformStyle.Vanish://窗體隱藏 this.InfoState = InformStyle.Displaying;//設置窗體的操作狀態為顯示中 this.SetBounds(Rect.X, Rect.Y + Rect.Height, Rect.Width, 0);//顯示Popup窗體,並放置到屏幕的底部 ShowWindow(this.Handle, 4);//顯示窗體 this.timer1.Interval = 100;//設置時間間隔為100 this.timer1.Start();//啟動計時器 break; case InformStyle.Display://窗體顯示 this.timer1.Stop();//停止計時器 this.timer1.Interval = 5000;//設置時間間隔為5000 this.timer1.Start();//啟動記時器 break; } } #endregion #region 事件 private void timer1_Tick(object sender, System.EventArgs e) { switch (this.InfoState) { case InformStyle.Display://顯示當前窗體 this.timer1.Stop();//停止計時器 this.timer1.Interval = 100;//設置時間間隔為100 if (!(this.isMouseMove))//如果鼠標不在窗體中移動 this.InfoState = InformStyle.Vanishing;//設置當前窗體的操作狀態為隱藏中 this.timer1.Start();//啟動計時器 break; case InformStyle.Displaying://當前窗體顯示中 if (this.Height <= this.Rect.Height - 12)//當窗體沒有完全顯示時 this.SetBounds(Rect.X, this.Top - 12, Rect.Width, this.Height + 12);//使窗體不斷上移 else { this.timer1.Stop();//停止計時器 this.SetBounds(Rect.X, Rect.Y, Rect.Width, Rect.Height);//設置當前窗體的邊界 this.InfoState = InformStyle.Display;//設置當前窗體的操作狀態為顯示 this.timer1.Interval = 5000;//設置時間間隔為5000 this.timer1.Start();//啟動計時器 } break; case InformStyle.Vanishing://隱藏當前窗體 if (this.isMouseMove)//如果鼠標在窗體中移動 this.InfoState = InformStyle.Displaying;//設置窗體的操作狀態為顯示 else { if (this.Top <= this.Rect.Bottom - 12)//如果窗體沒有完全隱藏 this.SetBounds(Rect.X, this.Top + 12, Rect.Width, this.Height - 12);//使窗體不斷下移 else { this.Hide();//隱藏當前窗體 this.InfoState = InformStyle.Vanish;//設置窗體的操作狀態為隱藏 } } break; } } private void Frm_Popup_MouseMove(object sender, MouseEventArgs e) { this.isMouseMove = true;//當鼠標移入時,設為true } private void Frm_Popup_MouseLeave(object sender, EventArgs e) { this.isMouseMove = false;//當鼠標移出時,設為false } private void pictureBox1_Click(object sender, EventArgs e) { if (this.InfoState != InformStyle.Vanish)//如果窗體的狀態不是隱藏 { this.timer1.Stop();//停止計時器 this.InfoState = InformStyle.Vanish;//設置窗體的操作狀態為隱藏 base.Hide();//隱藏當前窗體 } } private void pictureBox1_MouseEnter(object sender, EventArgs e) { pictureBox1.Image = null; pictureBox1.Image = Image.FromFile("Close1.bmp"); } private void pictureBox1_MouseLeave(object sender, EventArgs e) { pictureBox1.Image = null; pictureBox1.Image = Image.FromFile("Close2.bmp"); } #endregion } }
代碼下載
https://download.csdn.net/download/BADAO_LIUMANG_QIZHI/12240463
