Winform中实现右下角Popuo弹窗提醒效果(附代码下载)
生活随笔
收集整理的這篇文章主要介紹了
Winform中实现右下角Popuo弹窗提醒效果(附代码下载)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
場景
效果
?
注:
博客主頁:
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>//InformStyleprotected 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; }}#endregionpublic 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;//設置時間間隔為100this.timer1.Start();//啟動計時器break;case InformStyle.Display://窗體顯示this.timer1.Stop();//停止計時器this.timer1.Interval = 5000;//設置時間間隔為5000this.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;//設置時間間隔為100if (!(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;//設置時間間隔為5000this.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
總結
以上是生活随笔為你收集整理的Winform中实现右下角Popuo弹窗提醒效果(附代码下载)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: VMware10.0中安装CentOS8
- 下一篇: Winform中使用用户控件实现带行数和