【C#】Winform 仿Toast彈出
當前位置:點晴教程→知識管理交流
→『 技術文檔交流 』
導讀 在Winform中,彈窗提示基本都是使用MessageBox.Show(),樣式美觀度暫且不論,這是一個必須要交互的消息提示框,所以很多時候就會無形之中增加操作的繁瑣度。如果開發過Web或者安卓就會知道有一個Toast的消息提示,即短暫提示后就消失,無需操作反饋,在很多情況下是著實好用。。。 本篇在Winform中封裝一個類似Toast效果的彈出框,可以設置彈出位置、顏色、自動關閉時間等。 開發環境:.NET Framework版本:4.8 開發工具:Visual Studio 2022
public static void Show(string msg, Color backColor, Color foreColor, ShowLocation location = ShowLocation.Center, int autoColseTime = 2000) { Toast toast = new Toast(); toast.StartPosition = FormStartPosition.CenterScreen; toast.ShowInTaskbar = false; toast.BackColor = backColor; toast.SetProperty(msg, foreColor); Rectangle rect = Screen.PrimaryScreen.WorkingArea; switch (location) { case ShowLocation.Top: toast.Location = new Point((rect.Width - toast.Width) / 2, 10); break; case ShowLocation.Bottom: toast.Location = new Point((rect.Width - toast.Width) / 2, rect.Height - toast.Height - 10); break; case ShowLocation.RightBottom: toast.Location = new Point(rect.Width - toast.Width - 10, rect.Height - toast.Height - 10); break; default: } System.Timers.Timer timer = new System.Timers.Timer(autoColseTime); timer.Elapsed += delegate { timer.Stop(); toast?.Invoke(new Action(() => { toast.Close(); })); }; timer.Start(); }
public static void Success(string msg, ShowLocation location = ShowLocation.Center, int autoColseTime = 2000) { Show(msg, Color.fromArgb(103, 194, 58), Color.White, location, autoColseTime); }
public static void Warning(string msg, ShowLocation location = ShowLocation.Center, int autoColseTime = 2000) { Show(msg, Color.fromArgb(230, 162, 60), Color.White, location, autoColseTime); }
public static void Error(string msg, ShowLocation location = ShowLocation.Center, int autoColseTime = 2000) { Show(msg, Color.fromArgb(245, 108, 108), Color.White, location, autoColseTime); }
private void button1_Click(object sender, EventArgs e) { Toast.Success("上", ShowLocation.Top); Toast.Error("下", ShowLocation.Bottom); Toast.Warning("右下", ShowLocation.RightBottom); Toast.Show("中", Color.fromArgb(200, 0, 0, 0), Color.White); } 5、實現的效果 6、下載地址: https://pan.baidu.com/s/1Fgq875Fx1h1q00IQtH6W_Q?pwd=1lma 該文章在 2023/9/18 11:49:40 編輯過 |
關鍵字查詢
相關文章
正在查詢... |